Skip to content

Commit

Permalink
Update zio-logging (#2392)
Browse files Browse the repository at this point in the history
  • Loading branch information
deusaquilus authored Jan 21, 2022
1 parent fb05e05 commit a43964a
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 83 deletions.
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,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(
Expand Down
85 changes: 3 additions & 82 deletions quill-core/src/main/scala/io/getquill/util/QueryLogger.scala
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit a43964a

Please sign in to comment.