Improve logging configuration

- Log levels of specific loggers can be defined in the config
  file (doesn't work with env variables)

- Log events of background tasks carry now additional data
This commit is contained in:
eikek
2022-04-30 18:26:19 +02:00
parent c4c5985a6e
commit 5bdf728eb3
12 changed files with 91 additions and 51 deletions

View File

@ -10,10 +10,26 @@ import cats.data.NonEmptyList
import io.circe.{Decoder, Encoder}
final case class LogConfig(minimumLevel: Level, format: LogConfig.Format) {}
final case class LogConfig(
minimumLevel: Level,
format: LogConfig.Format,
levels: LogConfig.ExtraLevels
) {
def clearLevels: LogConfig =
copy(levels = Map.empty)
def withLevel(logger: String, level: Level): LogConfig =
copy(levels = levels.updated(logger, level))
def docspellLevel(level: Level): LogConfig =
withLevel("docspell", level)
}
object LogConfig {
type ExtraLevels = Map[String, Level]
sealed trait Format { self: Product =>
def name: String =
productPrefix.toLowerCase
@ -38,8 +54,10 @@ object LogConfig {
}
implicit val jsonDecoder: Decoder[LogConfig] =
Decoder.forProduct2("minimumLevel", "format")(LogConfig.apply)
Decoder.forProduct3("minimumLevel", "format", "levels")(LogConfig.apply)
implicit val jsonEncoder: Encoder[LogConfig] =
Encoder.forProduct2("minimumLevel", "format")(r => (r.minimumLevel, r.format))
Encoder.forProduct3("minimumLevel", "format", "levels")(r =>
(r.minimumLevel, r.format, r.levels)
)
}

View File

@ -26,6 +26,9 @@ final case class LogEvent(
def data[A: Encoder](key: String, value: => A): LogEvent =
copy(data = data.updated(key, () => Encoder[A].apply(value)))
def addData(m: Map[String, Json]): LogEvent =
copy(data = data ++ m.view.mapValues(json => () => json).toMap)
def addMessage(msg: => String): LogEvent =
copy(additional = (() => Left(msg)) :: additional)