Skip to content

Commit

Permalink
Merge pull request #801 from eikek/logging-config
Browse files Browse the repository at this point in the history
Allow to override log levels in the config file
  • Loading branch information
mergify[bot] authored Jun 25, 2022
2 parents 219d0d2 + 7c1a154 commit 8f6c872
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 35 deletions.
23 changes: 20 additions & 3 deletions modules/logging/api/src/main/scala/sharry/logging/LogConfig.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,24 @@ 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 sharryLevel(level: Level): LogConfig =
withLevel("sharry", level)
}

object LogConfig {
type ExtraLevels = Map[String, Level]

sealed trait Format { self: Product =>
def name: String =
Expand All @@ -38,8 +53,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)
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,39 +14,35 @@ import scribe.jul.JULHandler
import scribe.writer.SystemOutWriter

object ScribeConfigure {
private[this] val sharryRootVerbose = "SHARRY_ROOT_LOGGER_LEVEL"

def configure[F[_]: Sync](cfg: LogConfig): F[Unit] =
Sync[F].delay {
replaceJUL()
val sharryLogger = scribe.Logger("sharry")
unsafeConfigure(scribe.Logger.root, cfg.copy(minimumLevel = getRootMinimumLevel))
unsafeConfigure(sharryLogger, cfg)
unsafeConfigure(scribe.Logger("org.flywaydb"), cfg)
unsafeConfigure(scribe.Logger("binny"), cfg)
// unsafeConfigure(scribe.Logger("org.http4s"), cfg)
unsafeConfigure(cfg)
}

def getRootMinimumLevel: Level =
Option(System.getenv(sharryRootVerbose))
.map(Level.fromString)
.flatMap {
case Right(level) => Some(level)
case Left(err) =>
scribe.warn(
s"Environment variable '$sharryRootVerbose' has invalid value: $err"
)
None
}
.getOrElse(Level.Error)
def unsafeConfigure(cfg: LogConfig): Unit = {
unsafeConfigure(scribe.Logger.root, cfg.format, cfg.minimumLevel)
cfg.levels.foreach { case (name, level) =>
unsafeConfigure(scribe.Logger(name), cfg.format, level)
}
}
def unsafeConfigure(logger: String, cfg: LogConfig): Unit = {
val log = scribe.Logger(logger)
val level = cfg.levels.getOrElse(logger, cfg.minimumLevel)
unsafeConfigure(log, cfg.format, level)
}

def unsafeConfigure(logger: scribe.Logger, cfg: LogConfig): Unit = {
def unsafeConfigure(
logger: scribe.Logger,
format: LogConfig.Format,
level: Level
): Unit = {
val mods: List[scribe.Logger => scribe.Logger] = List(
_.clearHandlers(),
_.withMinimumLevel(ScribeWrapper.convertLevel(cfg.minimumLevel)),
_.withMinimumLevel(ScribeWrapper.convertLevel(level)),
l =>
if (logger.id == scribe.Logger.RootId) {
cfg.format match {
format match {
case Format.Fancy =>
l.withHandler(formatter = Formatter.enhanced, writer = SystemOutWriter)
case Format.Plain =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,7 @@ trait TestLoggingConfig extends Suite {

override def beforeAll(): Unit = {
super.beforeAll()
val docspellLogger = scribe.Logger("docspell")
ScribeConfigure.unsafeConfigure(docspellLogger, docspellLogConfig)
val rootCfg = docspellLogConfig.copy(minimumLevel = rootMinimumLevel)
ScribeConfigure.unsafeConfigure(scribe.Logger.root, rootCfg)
()
ScribeConfigure.unsafeConfigure(docspellLogConfig)
}

}
11 changes: 8 additions & 3 deletions modules/microsite/docs/doc/configure.md
Original file line number Diff line number Diff line change
Expand Up @@ -550,9 +550,9 @@ cleans up the following resources:

## Logging

By default, sharry logs to stdout. This works well, when managed by
systemd or other inits. Logging can be configured with two settings in
the config file:
Sharry logs to stdout. This works well, when managed by systemd or
similar tools. Logging can be configured with these settings in the
config file:

- `logging.minimum-level` specifies the log level to control the
verbosity. Levels are ordered from: *Trace*, *Debug*, *Info*, *Warn*
Expand All @@ -561,3 +561,8 @@ the config file:
two formats for humans: *Plain* and *Fancy*. And two more suited for
machine consumption: *Json* and *Logfmt*. The *Json* format contains
all details, while the others may omit some for readability
- `levels` optional _logger name_ to _level_ mappings to override the
log level for specific loggers. If not mentioned here, everything is
logged with `minimum-level`.

The default sets some selected loggers to info and the others to warn.
10 changes: 9 additions & 1 deletion modules/restserver/src/main/resources/reference.conf
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,15 @@ sharry.restserver {

# The minimum level to log. From lowest to highest:
# Trace, Debug, Info, Warn, Error
minimum-level = "Info"
minimum-level = "Warn"

# Override the log level of specific loggers
levels = {
"sharry" = "Info"
"org.flywaydb" = "Info"
"binny" = "Info"
"org.http4s" = "Info"
}
}

# The time from receiving a request until the first line of the
Expand Down

0 comments on commit 8f6c872

Please sign in to comment.