From 789865554f1e1d1730793d2fad1ae04cf523c8e6 Mon Sep 17 00:00:00 2001 From: Alexandre Archambault Date: Thu, 29 Jun 2023 17:19:17 +0200 Subject: [PATCH] Allow users to tweak default log level via ALMOND_LOG_LEVEL (#1170) --- .../src/main/scala/almond/Options.scala | 2 +- .../src/main/scala/almond/ScalaKernel.scala | 18 +++++++++++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/modules/scala/scala-kernel/src/main/scala/almond/Options.scala b/modules/scala/scala-kernel/src/main/scala/almond/Options.scala index 9ac31a07e..7c5872742 100644 --- a/modules/scala/scala-kernel/src/main/scala/almond/Options.scala +++ b/modules/scala/scala-kernel/src/main/scala/almond/Options.scala @@ -41,7 +41,7 @@ final case class Options( @HelpMessage("Enable Maven profile (start with ! to disable)") profile: List[String] = Nil, @HelpMessage("Log level (one of none, error, warn, info, debug)") - log: String = "warn", + log: Option[String] = None, @HelpMessage("Send log to a file rather than stderr") @ValueDescription("/path/to/log-file") logTo: Option[String] = None, diff --git a/modules/scala/scala-kernel/src/main/scala/almond/ScalaKernel.scala b/modules/scala/scala-kernel/src/main/scala/almond/ScalaKernel.scala index 8c8678ea9..6c7b43b9f 100644 --- a/modules/scala/scala-kernel/src/main/scala/almond/ScalaKernel.scala +++ b/modules/scala/scala-kernel/src/main/scala/almond/ScalaKernel.scala @@ -27,7 +27,23 @@ object ScalaKernel extends CaseApp[Options] { // Enable ANSI output in Windows terminal coursier.jniutils.WindowsAnsiTerminal.enableAnsiOutput() - val logCtx = Level.fromString(options.log) match { + def logLevelFromEnv = + Option(System.getenv("ALMOND_LOG_LEVEL")).flatMap { str => + Level.fromString(str) match { + case Left(err) => + Console.err.println( + s"Error parsing log level from environment variable ALMOND_LOG_LEVEL: $err, ignoring it" + ) + None + case other => + Some(other) + } + } + val maybeLogLevel = options.log + .map(Level.fromString) + .orElse(logLevelFromEnv) + .getOrElse(Right(Level.Warning)) + val logCtx = maybeLogLevel match { case Left(err) => Console.err.println(err) sys.exit(1)