Skip to content

Commit

Permalink
LoggerPattern config
Browse files Browse the repository at this point in the history
  • Loading branch information
justcoon committed Mar 2, 2023
1 parent 0c4f057 commit 4c88b58
Show file tree
Hide file tree
Showing 8 changed files with 127 additions and 78 deletions.
48 changes: 48 additions & 0 deletions core/jvm/src/test/scala/zio/logging/ConsoleLoggerConfigSpec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package zio.logging

import zio.test._
import zio.{ Config, ConfigProvider, LogLevel }

object ConsoleLoggerConfigSpec extends ZIOSpecDefault {

val spec: Spec[Environment, Any] = suite("ConsoleLoggerConfig")(
test("load valid config") {

val logPattern =
"%highlight{%timestamp{yyyy-MM-dd'T'HH:mm:ssZ} %fixed{7}{%level} [%fiberId] %name:%line %message %cause}"

val configProvider: ConfigProvider = ConfigProvider.fromMap(
Map(
"logger/pattern" -> logPattern,
"logger/filter/rootLevel" -> LogLevel.Info.label,
"logger/filter/mappings/zio.logging.example.LivePingService" -> LogLevel.Debug.label
),
"/"
)

configProvider.load(ConsoleLoggerConfig.config.nested("logger")).map { _ =>
assertTrue(true)
}
},
test("fail on invalid config") {

val logPattern =
"%highlight{%timestamp{yyyy-MM-dd'T'HH:mm:ssZ} %fixed{7}{%level} [%fiberId] %name:%line %message %cause}"

val configProvider: ConfigProvider = ConfigProvider.fromMap(
Map(
"logger/pattern" -> logPattern,
"logger/filter/rootLevel" -> "INVALID_LOG_LEVEL"
),
"/"
)

configProvider
.load(ConsoleLoggerConfig.config.nested("logger"))
.exit
.map { e =>
assert(e)(Assertion.failsWithA[Config.Error])
}
}
)
}
57 changes: 57 additions & 0 deletions core/jvm/src/test/scala/zio/logging/FileLoggerConfigSpec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package zio.logging

import zio.test._
import zio.{ Config, ConfigProvider, LogLevel }

import java.net.URI
import java.nio.charset.StandardCharsets
import java.nio.file.Paths

object FileLoggerConfigSpec extends ZIOSpecDefault {

val spec: Spec[Environment, Any] = suite("FileLoggerConfig")(
test("load valid config") {

val logPattern =
"%highlight{%timestamp{yyyy-MM-dd'T'HH:mm:ssZ} %fixed{7}{%level} [%fiberId] %name:%line %message %cause}"

val configProvider: ConfigProvider = ConfigProvider.fromMap(
Map(
"logger/pattern" -> logPattern,
"logger/path" -> "file:///tmp/test.log",
"logger/filter/rootLevel" -> LogLevel.Info.label,
"logger/filter/mappings/zio.logging.example.LivePingService" -> LogLevel.Debug.label
),
"/"
)

configProvider.load(FileLoggerConfig.config.nested("logger")).map { loadedConfig =>
assertTrue(loadedConfig.charset == StandardCharsets.UTF_8) &&
assertTrue(loadedConfig.destination == Paths.get(URI.create("file:///tmp/test.log"))) &&
assertTrue(loadedConfig.autoFlushBatchSize == 1) &&
assertTrue(loadedConfig.bufferedIOSize.isEmpty)
}
},
test("fail on invalid config") {

val logPattern =
"%highlight{%timestamp{yyyy-MM-dd'T'HH:mm:ssZ} %fixed{7}{%level} [%fiberId] %name:%line %message %cause}"

val configProvider: ConfigProvider = ConfigProvider.fromMap(
Map(
"logger/pattern" -> logPattern,
"logger/charset" -> "INVALID_CHARSET",
"logger/filter/rootLevel" -> "INVALID_LOG_LEVEL"
),
"/"
)

configProvider
.load(FileLoggerConfig.config.nested("logger"))
.exit
.map { e =>
assert(e)(Assertion.failsWithA[Config.Error])
}
}
)
}
31 changes: 0 additions & 31 deletions core/jvm/src/test/scala/zio/logging/LogPatternSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -86,37 +86,6 @@ object LogPatternSpec extends ZIOSpecDefault {
)
)
)
},
test("parse labeled pattern from config") {
val configProvider: ConfigProvider = ConfigProvider.fromMap(
Map(
"pattern/timestamp" -> "%timestamp",
"pattern/level" -> "%level",
"pattern/fiberId" -> "%fiberId",
"pattern/kvs" -> "%kvs",
"pattern/message" -> "%message",
"pattern/cause" -> "%cause",
"pattern/name" -> "%name"
),
"/"
)

val patternConfig = Config.table("pattern", LogPattern.config).withDefault(Map.empty)

configProvider.load(patternConfig).map { labelPattern =>
assertTrue(
labelPattern ==
Map(
"timestamp" -> LogPattern.Timestamp.default,
"level" -> LogPattern.LogLevel,
"fiberId" -> LogPattern.FiberId,
"kvs" -> LogPattern.KeyValues,
"message" -> LogPattern.LogMessage,
"cause" -> LogPattern.Cause,
"name" -> LogPattern.LoggerName
)
)
}
}
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,7 @@ object ConsoleLoggerConfig {

private val filterConfig = LogFilter.LogLevelByNameConfig.config.nested("filter")

val jsonLoggerConfig: Config[ConsoleLoggerConfig] = {
val patternConfig = Config.table("pattern", LogPattern.config).withDefault(Map.empty)
(patternConfig ++ filterConfig).map { case (pattern, filterConfig) =>
ConsoleLoggerConfig(LogFormat.makeLabeled(pattern), LogFilter.logLevelByName(filterConfig))
}
}

val stringLoggerConfig: Config[ConsoleLoggerConfig] = {
val config: Config[ConsoleLoggerConfig] = {
val patternConfig = LogPattern.config.nested("pattern")
(patternConfig ++ filterConfig).map { case (pattern, filterConfig) =>
ConsoleLoggerConfig(pattern.toLogFormat, LogFilter.logLevelByName(filterConfig))
Expand Down
17 changes: 1 addition & 16 deletions core/shared/src/main/scala/zio/logging/FileLoggerConfig.scala
Original file line number Diff line number Diff line change
Expand Up @@ -54,22 +54,7 @@ object FileLoggerConfig {
Config.string.mapOrFail(charsetValue).nested("charset").withDefault(StandardCharsets.UTF_8)
private val pathConfig = Config.string.mapOrFail(pathValue).nested("path")

val jsonLoggerConfig: Config[FileLoggerConfig] = {
val patternConfig = Config.table("pattern", LogPattern.config).withDefault(Map.empty)
(pathConfig ++ patternConfig ++ filterConfig ++ charsetConfig ++ autoFlushBatchSizeConfig ++ bufferedIOSizeConfig).map {
case (path, pattern, filterConfig, charset, autoFlushBatchSize, bufferedIOSize) =>
FileLoggerConfig(
path,
LogFormat.makeLabeled(pattern),
LogFilter.logLevelByName(filterConfig),
charset,
autoFlushBatchSize,
bufferedIOSize
)
}
}

val stringLoggerConfig: Config[FileLoggerConfig] = {
val config: Config[FileLoggerConfig] = {
val patternConfig = LogPattern.config.nested("pattern")
(pathConfig ++ patternConfig ++ filterConfig ++ charsetConfig ++ autoFlushBatchSizeConfig ++ bufferedIOSizeConfig).map {
case (path, pattern, filterConfig, charset, autoFlushBatchSize, bufferedIOSize) =>
Expand Down
14 changes: 7 additions & 7 deletions core/shared/src/main/scala/zio/logging/LogPattern.scala
Original file line number Diff line number Diff line change
Expand Up @@ -121,19 +121,19 @@ object LogPattern {
final case object EscapedArgPrefix extends Arg {
override val name = "%"

override val toLogFormat: LogFormat = LogFormat.text("%")
override val toLogFormat: LogFormat = LogFormat.text(name)
}

final case object EscapedOpenBracket extends Arg {
override val name = "{"

override val toLogFormat: LogFormat = LogFormat.text("{")
override val toLogFormat: LogFormat = LogFormat.text(name)
}

final case object EscapedCloseBracket extends Arg {
override val name = "}"

override val toLogFormat: LogFormat = LogFormat.text("}")
override val toLogFormat: LogFormat = LogFormat.text(name)
}

final case object Spans extends Arg {
Expand Down Expand Up @@ -189,7 +189,7 @@ object LogPattern {
}

final case class Text(text: String) extends LogPattern {
override def toLogFormat: LogFormat = LogFormat.text(text)
override val toLogFormat: LogFormat = LogFormat.text(text)
}

object Text {
Expand All @@ -211,7 +211,7 @@ object LogPattern {
.string
.transformEither(
make,
(p: A) => Right(p.arg1.toString)
(v: A) => Right(v.arg1.toString)
)

val end = Syntax.char('}')
Expand All @@ -220,7 +220,7 @@ object LogPattern {
}

private def arg1Syntax[A <: Arg1[_]](name: String, make: String => A): Syntax[String, Char, Char, A] =
arg1EitherSyntax[A](name, s => Right(make(s)))
arg1EitherSyntax[A](name, v => Right(make(v)))

private def arg1Syntax[A1, A <: Arg1[A1]](
name: String,
Expand All @@ -244,7 +244,7 @@ object LogPattern {
val begin2 = Syntax.char('{')
val end = Syntax.char('}')

(begin1 ~> a1Syntax <~ end).zip(begin2 ~> a2Syntax <~ end).transform((v) => make(v._1, v._2), v => (v.arg1, v.arg2))
(begin1 ~> a1Syntax <~ end).zip(begin2 ~> a2Syntax <~ end).transform(v => make(v._1, v._2), v => (v.arg1, v.arg2))
}

private def argSyntax[A <: Arg](name: String, value: A): Syntax[String, Char, Char, A] =
Expand Down
16 changes: 8 additions & 8 deletions core/shared/src/main/scala/zio/logging/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ package object logging {
def consoleLogger(configPath: String = "logger"): ZLayer[Any, Config.Error, Unit] =
ZLayer.scoped {
for {
config <- ZIO.config(ConsoleLoggerConfig.stringLoggerConfig.nested(configPath))
config <- ZIO.config(ConsoleLoggerConfig.config.nested(configPath))
_ <- ZIO.withLoggerScoped(makeConsoleLogger(config))
} yield ()
}
Expand All @@ -76,7 +76,7 @@ package object logging {
def consoleErrLogger(configPath: String = "logger"): ZLayer[Any, Config.Error, Unit] =
ZLayer.scoped {
for {
config <- ZIO.config(ConsoleLoggerConfig.stringLoggerConfig.nested(configPath))
config <- ZIO.config(ConsoleLoggerConfig.config.nested(configPath))
_ <- ZIO.withLoggerScoped(makeConsoleErrLogger(config))
} yield ()
}
Expand All @@ -87,7 +87,7 @@ package object logging {
def consoleJsonLogger(configPath: String = "logger"): ZLayer[Any, Config.Error, Unit] =
ZLayer.scoped {
for {
config <- ZIO.config(ConsoleLoggerConfig.jsonLoggerConfig.nested(configPath))
config <- ZIO.config(ConsoleLoggerConfig.config.nested(configPath))
_ <- ZIO.withLoggerScoped(makeConsoleJsonLogger(config))
} yield ()
}
Expand All @@ -98,7 +98,7 @@ package object logging {
def consoleErrJsonLogger(configPath: String = "logger"): ZLayer[Any, Config.Error, Unit] =
ZLayer.scoped {
for {
config <- ZIO.config(ConsoleLoggerConfig.jsonLoggerConfig.nested(configPath))
config <- ZIO.config(ConsoleLoggerConfig.config.nested(configPath))
_ <- ZIO.withLoggerScoped(makeConsoleErrJsonLogger(config))
} yield ()
}
Expand Down Expand Up @@ -137,7 +137,7 @@ package object logging {
def fileLogger(configPath: String = "logger"): ZLayer[Any, Config.Error, Unit] =
ZLayer.scoped {
for {
config <- ZIO.config(FileLoggerConfig.stringLoggerConfig.nested(configPath))
config <- ZIO.config(FileLoggerConfig.config.nested(configPath))
_ <- ZIO.withLoggerScoped(makeFileLogger(config))
} yield ()
}
Expand All @@ -148,7 +148,7 @@ package object logging {
def fileJsonLogger(configPath: String = "logger"): ZLayer[Any, Config.Error, Unit] =
ZLayer.scoped {
for {
config <- ZIO.config(FileLoggerConfig.jsonLoggerConfig.nested(configPath))
config <- ZIO.config(FileLoggerConfig.config.nested(configPath))
_ <- ZIO.withLoggerScoped(makeFileJsonLogger(config))
} yield ()
}
Expand Down Expand Up @@ -200,7 +200,7 @@ package object logging {
def fileAsyncLogger(configPath: String = "logger"): ZLayer[Any, Config.Error, Unit] =
ZLayer.scoped {
for {
config <- ZIO.config(FileLoggerConfig.stringLoggerConfig.nested(configPath))
config <- ZIO.config(FileLoggerConfig.config.nested(configPath))
_ <- makeFileAsyncLogger(config)
} yield ()
}
Expand All @@ -211,7 +211,7 @@ package object logging {
def fileAsyncJsonLogger(configPath: String = "logger"): ZLayer[Any, Config.Error, Unit] =
ZLayer.scoped {
for {
config <- ZIO.config(FileLoggerConfig.jsonLoggerConfig.nested(configPath))
config <- ZIO.config(FileLoggerConfig.config.nested(configPath))
_ <- makeFileAsyncJsonLogger(config)
} yield ()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,13 @@ object ConsoleJsonApp extends ZIOAppDefault {
private val userLogAnnotation = LogAnnotation[User]("user", (_, u) => u, _.toJson)
private val uuid = LogAnnotation[UUID]("uuid", (_, i) => i, _.toString)

val logPattern =
"%label{timestamp}{%timestamp{yyyy-MM-dd'T'HH:mm:ssZ}} %label{level}{%level} %label{fiberId}{%fiberId} %label{message}{%message} %label{cause}{%cause} %label{name}{%name} %kvs"

val configProvider: ConfigProvider = ConfigProvider.fromMap(
Map(
"logger/pattern/timestamp" -> "%timestamp{yyyy-MM-dd'T'HH:mm:ssZ}",
"logger/pattern/level" -> "%level",
"logger/pattern/fiberId" -> "%fiberId",
"logger/pattern/kvs" -> "%kvs",
"logger/pattern/message" -> "%message",
"logger/pattern/cause" -> "%cause",
"logger/pattern/name" -> "%name",
"logger/filter/rootLevel" -> LogLevel.Info.label
"logger/pattern" -> logPattern,
"logger/filter/rootLevel" -> LogLevel.Info.label
),
"/"
)
Expand Down

0 comments on commit 4c88b58

Please sign in to comment.