commit 6faffd2ad236a7f3b30971fe7cba382b0d075744
parent d80a5e343ab341bf980849b4347c3474bbb48b84
Author: MS <ms@taler.net>
Date: Wed, 24 Jun 2020 18:55:32 +0200
moving loglevel setter to util
Diffstat:
2 files changed, 27 insertions(+), 5 deletions(-)
diff --git a/nexus/src/main/kotlin/tech/libeufin/nexus/Main.kt b/nexus/src/main/kotlin/tech/libeufin/nexus/Main.kt
@@ -33,10 +33,11 @@ import org.slf4j.Logger
import org.slf4j.LoggerFactory
import tech.libeufin.nexus.server.serverMain
import tech.libeufin.util.CryptoUtil.hashpw
-import tech.libeufin.util.*
+import ch.qos.logback.classic.Level
+import ch.qos.logback.classic.LoggerContext
+import tech.libeufin.util.setLogLevel
-
-lateinit var logger: Logger
+val logger: Logger = LoggerFactory.getLogger("tech.libeufin.nexus")
class NexusCommand : CliktCommand() {
override fun run() = Unit
@@ -50,13 +51,13 @@ class Serve : CliktCommand("Run nexus HTTP server") {
}
private val dbName by option().default("libeufin-nexus.sqlite3")
private val host by option().default("127.0.0.1")
+ private val logLevel by option()
override fun run() {
- logger = LoggerFactory.getLogger("tech.libeufin.nexus")
+ setLogLevel(logLevel)
serverMain(dbName, host)
}
}
-
class Superuser : CliktCommand("Add superuser or change pw") {
private val dbName by option().default("libeufin-nexus.sqlite3")
private val username by argument()
diff --git a/util/src/main/kotlin/Config.kt b/util/src/main/kotlin/Config.kt
@@ -1,7 +1,10 @@
package tech.libeufin.util
+import ch.qos.logback.classic.Level
+import ch.qos.logback.classic.LoggerContext
import ch.qos.logback.classic.util.ContextInitializer
import ch.qos.logback.core.util.Loader
+import org.slf4j.LoggerFactory
/**
* Set system properties to wanted values, and load logback configuration after.
@@ -19,4 +22,22 @@ fun setLogFile(logFile: String?, logFileNameAsProperty: String, configFileName:
println("Warning: could not find log config file")
}
System.setProperty(ContextInitializer.CONFIG_FILE_PROPERTY, configFilePath.toString())
+}
+
+/**
+ * Set level of any logger belonging to LibEuFin (= has "libeufin" in name)
+ * _and_ found under the calling classpath (= obeying to the same logback.xml)
+ */
+fun setLogLevel(logLevel: String?) {
+ when (val immutable = logLevel) {
+ is String -> {
+ val ctx = LoggerFactory.getILoggerFactory() as LoggerContext
+ val loggers: List<ch.qos.logback.classic.Logger> = ctx.loggerList
+ loggers.forEach {
+ if (it.name.contains("libeufin")) {
+ it.level = Level.toLevel(immutable)
+ }
+ }
+ }
+ }
}
\ No newline at end of file