From cb3c3b1c5ba49bd04a02f933af8330fd3480c81a Mon Sep 17 00:00:00 2001 From: Alexander Ioffe Date: Fri, 21 Jan 2022 00:30:31 -0500 Subject: [PATCH] Update zio-logging --- build.sbt | 2 +- .../scala/io/getquill/util/QueryLogger.scala | 85 +------------------ 2 files changed, 4 insertions(+), 83 deletions(-) diff --git a/build.sbt b/build.sbt index 2760194c1d..10aec99766 100644 --- a/build.sbt +++ b/build.sbt @@ -265,7 +265,7 @@ lazy val `quill-core` = .settings(mimaSettings: _*) .settings(libraryDependencies ++= Seq( "com.typesafe" % "config" % "1.4.1", - "dev.zio" %% "zio-logging" % "0.5.13", + "dev.zio" %% "zio-logging" % "0.5.14", "com.typesafe.scala-logging" %% "scala-logging" % "3.9.4" )) .jvmSettings( diff --git a/quill-core/src/main/scala/io/getquill/util/QueryLogger.scala b/quill-core/src/main/scala/io/getquill/util/QueryLogger.scala index c17c2cd48d..fed14235b1 100644 --- a/quill-core/src/main/scala/io/getquill/util/QueryLogger.scala +++ b/quill-core/src/main/scala/io/getquill/util/QueryLogger.scala @@ -1,96 +1,17 @@ package io.getquill.util -import io.getquill.util.Messages.{ LogToFile, quillLogFile } +import io.getquill.util.Messages.LogToFile import zio._ -import zio.clock.Clock -import zio.console.Console -import zio.logging.LogAppender.Service -import zio.logging.Logging.{ addTimestamp, modifyLoggerM } import zio.logging._ -import java.io.{ BufferedWriter, FileOutputStream, OutputStreamWriter, Writer } -import java.nio.charset.{ Charset, StandardCharsets } -import java.nio.file.{ Path, Paths } +import java.nio.file.Paths import java.time.ZonedDateTime -class AppendingLogWriter( - destination: Path, - charset: Charset, - autoFlushBatchSize: Int, - bufferedIOSize: Option[Int] -) extends Writer { - private val writer: Writer = { - val output = new OutputStreamWriter(new FileOutputStream(destination.toFile, true), charset) - bufferedIOSize match { - case Some(bufferSize) => new BufferedWriter(output, bufferSize) - case None => output - } - } - - private var entriesWritten: Long = 0 - - def write(buffer: Array[Char], offset: Int, length: Int): Unit = - writer.write(buffer, offset, length) - - def writeln(line: String): Unit = { - writer.write(line) - writer.write(System.lineSeparator) - - entriesWritten += 1 - - if (entriesWritten % autoFlushBatchSize == 0) - writer.flush() - } - - def flush(): Unit = writer.flush() - - def close(): Unit = writer.close() -} - class QueryLogger(logToFile: LogToFile) { - def appendFile[A]( - destination: Path, - charset: Charset, - autoFlushBatchSize: Int, - bufferedIOSize: Option[Int], - format0: LogFormat[A] - )(implicit tag: Tag[LogAppender.Service[A]]): ZLayer[Any, Throwable, Appender[A]] = - ZManaged - .fromAutoCloseable(UIO(new AppendingLogWriter(destination, charset, autoFlushBatchSize, bufferedIOSize))) - .zip(ZRef.makeManaged(false)) - .map { - case (writer, hasWarned) => - new Service[A] { - override def write(ctx: LogContext, msg: => A): UIO[Unit] = - Task(writer.writeln(format0.format(ctx, msg))).catchAll { t => - UIO { - System.err.println( - s"Logging to file $destination failed with an exception. Further exceptions will be suppressed in order to prevent log spam." - ) - t.printStackTrace(System.err) - }.unlessM(hasWarned.getAndSet(true)) - } - } - } - .toLayer[LogAppender.Service[A]] - - def logFile( - destination: Path, - charset: Charset = StandardCharsets.UTF_8, - autoFlushBatchSize: Int = 1, - bufferedIOSize: Option[Int] = None, - logLevel: LogLevel = LogLevel.Info, - format: LogFormat[String] = LogFormat.SimpleConsoleLogFormat((_, s) => s) - ): ZLayer[Console with Clock, Throwable, Logging] = - (ZLayer.requires[Clock] ++ - appendFile[String](destination, charset, autoFlushBatchSize, bufferedIOSize, format) - .map(appender => Has(appender.get.filter((ctx, _) => ctx.get(LogAnnotation.Level) >= logLevel))) - >+> Logging.make >>> modifyLoggerM(addTimestamp[String])) - def produceLoggerEnv(file: String) = ZEnv.live >>> - logFile( + Logging.file( logLevel = LogLevel.Info, format = LogFormat.fromFunction((ctx, str) => { str