Skip to content

Commit

Permalink
zio update, config updates (#705)
Browse files Browse the repository at this point in the history
  • Loading branch information
justcoon authored Apr 8, 2023
1 parent 44479c2 commit 6795902
Show file tree
Hide file tree
Showing 10 changed files with 35 additions and 42 deletions.
3 changes: 2 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ inThisBuild(
url("https://github.com/pshemass")
),
Developer("justcoon", "Peter Kotula", "[email protected]", url("https://github.com/justcoon"))
)
),
zioVersion := "2.0.11"
)
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ object FileLoggerConfigSpec extends ZIOSpecDefault {
"logger/path" -> "file:///tmp/test.log",
"logger/autoFlushBatchSize" -> "2",
"logger/bufferedIOSize" -> "4096",
"logger/rollingPolicy/type" -> "TimeBasedRollingPolicy",
"logger/filter/rootLevel" -> LogLevel.Info.label,
"logger/filter/mappings/zio.logging.example.LivePingService" -> LogLevel.Debug.label
),
Expand All @@ -31,7 +32,8 @@ object FileLoggerConfigSpec extends ZIOSpecDefault {
assertTrue(loadedConfig.charset == StandardCharsets.UTF_8) &&
assertTrue(loadedConfig.destination == Paths.get(URI.create("file:///tmp/test.log"))) &&
assertTrue(loadedConfig.autoFlushBatchSize == 2) &&
assertTrue(loadedConfig.bufferedIOSize == Some(4096))
assertTrue(loadedConfig.bufferedIOSize == Some(4096)) &&
assertTrue(loadedConfig.rollingPolicy == Some(FileLoggerConfig.FileRollingPolicy.TimeBasedRollingPolicy))
}
},
test("load default config") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ object WriterProviderSpec extends ZIOSpecDefault {
destination = FileSystems.getDefault.getPath("/tmp/file_app"),
charset = StandardCharsets.UTF_8,
bufferedIOSize = Some(1),
makeNewTime = testMakeNewTime
time = testMakeNewTime
)

val parallelExecution = ZIO
Expand Down
21 changes: 10 additions & 11 deletions core/shared/src/main/scala/zio/logging/FileLoggerConfig.scala
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,19 @@ final case class FileLoggerConfig(
)

object FileLoggerConfig {

sealed trait FileRollingPolicy

object FileRollingPolicy {
case object TimeBasedRollingPolicy extends FileRollingPolicy

private[logging] val fileRollingPolicyMapping: Map[String, FileRollingPolicy] = Map(
"TimeBasedRollingPolicy" -> FileRollingPolicy.TimeBasedRollingPolicy
)
final case object TimeBasedRollingPolicy extends FileRollingPolicy

private[logging] def fileRollingPolicyValue(value: String): Either[Config.Error.InvalidData, FileRollingPolicy] =
fileRollingPolicyMapping.get(value) match {
case Some(v) => Right(v)
case None => Left(Config.Error.InvalidData(Chunk.empty, s"Expected a FileRollingPolicy, but found ${value}"))
}
val config: Config[FileRollingPolicy] = {
val tpe = Config.string("type")
val timeBased = Config.succeed(TimeBasedRollingPolicy)

tpe.switch("TimeBasedRollingPolicy" -> timeBased)
}
}

private def charsetValue(value: String): Either[Config.Error.InvalidData, Charset] =
Expand All @@ -70,8 +70,7 @@ object FileLoggerConfig {
Config.string.mapOrFail(charsetValue).nested("charset").withDefault(StandardCharsets.UTF_8)
val pathConfig = Config.string.mapOrFail(pathValue).nested("path")
val formatConfig = LogFormat.config.nested("format").withDefault(LogFormat.default)
val rollingPolicyConfig =
Config.string.mapOrFail(FileRollingPolicy.fileRollingPolicyValue).nested("rollingPolicy").optional
val rollingPolicyConfig = FileRollingPolicy.config.nested("rollingPolicy").optional

(pathConfig ++ formatConfig ++ filterConfig ++ charsetConfig ++ autoFlushBatchSizeConfig ++ bufferedIOSizeConfig ++ rollingPolicyConfig).map {
case (path, format, filterConfig, charset, autoFlushBatchSize, bufferedIOSize, rollingPolicy) =>
Expand Down
21 changes: 2 additions & 19 deletions core/shared/src/main/scala/zio/logging/LogFilter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -161,26 +161,9 @@ object LogFilter {

object LogLevelByNameConfig {

private[logging] val logLevelMapping: Map[String, LogLevel] = Map(
LogLevel.All.label -> LogLevel.All,
LogLevel.Trace.label -> LogLevel.Trace,
LogLevel.Debug.label -> LogLevel.Debug,
LogLevel.Info.label -> LogLevel.Info,
LogLevel.Warning.label -> LogLevel.Warning,
LogLevel.Error.label -> LogLevel.Error,
LogLevel.Fatal.label -> LogLevel.Fatal,
LogLevel.None.label -> LogLevel.None
)

private[logging] def logLevelValue(value: String): Either[Config.Error.InvalidData, LogLevel] =
logLevelMapping.get(value.toUpperCase) match {
case Some(v) => Right(v)
case None => Left(Config.Error.InvalidData(Chunk.empty, s"Expected a LogLevel, but found ${value}"))
}

val config: Config[LogLevelByNameConfig] = {
val rootLevelConfig = Config.string.mapOrFail(logLevelValue).nested("rootLevel").withDefault(LogLevel.Info)
val mappingsConfig = Config.table("mappings", Config.string.mapOrFail(logLevelValue)).withDefault(Map.empty)
val rootLevelConfig = Config.logLevel.nested("rootLevel").withDefault(LogLevel.Info)
val mappingsConfig = Config.table("mappings", Config.logLevel).withDefault(Map.empty)

(rootLevelConfig ++ mappingsConfig).map { case (rootLevel, mappings) =>
LogLevelByNameConfig(rootLevel, mappings)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,17 @@ private[logging] object WriterProvider {
destination: Path,
charset: Charset,
bufferedIOSize: Option[Int],
makeNewTime: () => LocalDateTime = TimeBasedRollingWriterProvider.makeNewTime
time: () => LocalDateTime = TimeBasedRollingWriterProvider.makeNewTime
) extends WriterProvider {
import java.util.concurrent.locks.ReentrantLock
import TimeBasedRollingWriterProvider._

private var timeInUse = makeNewTime()
private var timeInUse = time()
private var currentWriter: Writer = makeWriter(makePath(destination, timeInUse), charset, bufferedIOSize)
private val lock: ReentrantLock = new ReentrantLock()

override def writer: Writer = {
val newTime = makeNewTime()
val newTime = time()
if (newTime != timeInUse) {
lock.lock()
try
Expand Down
1 change: 1 addition & 0 deletions docs/console-logger.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ logger {
# log format, default value: LogFormat.default
format = "%label{timestamp}{%fixed{32}{%timestamp}} %label{level}{%level} %label{thread}{%fiberId} %label{message}{%message} %label{cause}{%cause}"
# log filter
filter {
# see filter configuration
rootLevel = INFO
Expand Down
13 changes: 9 additions & 4 deletions docs/file-logger.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,16 @@ logger {
# if defined, buffered writer is used, with given buffer size
# bufferedIOSize = 8192
# if defined, file log rolling policy is used
rollingPolicy {
type = TimeBasedRollingPolicy # time based file rolling policy based on date - currently only this one is supported
}
# log filter
filter {
# see filter configuration
rootLevel = INFO
}
# log rolling, default value: None
rollingPolicy = TimeBasedRollingPolicy
}
```

Expand Down Expand Up @@ -87,7 +90,9 @@ object FileApp extends ZIOAppDefault {
|logger {
| format = "%timestamp{yyyy-MM-dd'T'HH:mm:ssZ} %fixed{7}{%level} [%fiberId] %name:%line %message %cause"
| path = "file:///tmp/file_app.log"
| rollingPolicy = TimeBasedRollingPolicy
| rollingPolicy {
| type = TimeBasedRollingPolicy
| }
|}
|""".stripMargin

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ object FileApp extends ZIOAppDefault {
|logger {
| format = "%timestamp{yyyy-MM-dd'T'HH:mm:ssZ} %fixed{7}{%level} [%fiberId] %name:%line %message %cause"
| path = "file:///tmp/file_app.log"
| rollingPolicy = TimeBasedRollingPolicy
| rollingPolicy {
| type = TimeBasedRollingPolicy
| }
|}
|""".stripMargin

Expand Down
2 changes: 1 addition & 1 deletion project/MimaSettings.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import sbt.Keys.{ name, organization }
import sbt._

object MimaSettings {
lazy val bincompatVersionToCompare = "2.1.10"
lazy val bincompatVersionToCompare = "2.1.11"

def mimaSettings(failOnProblem: Boolean) =
Seq(
Expand Down

0 comments on commit 6795902

Please sign in to comment.