From 6044b5c2fd83632eb1441545cb1d427a74e98b0a Mon Sep 17 00:00:00 2001 From: MS Date: Wed, 24 Jun 2020 11:52:08 +0200 Subject: nexus offers logfile name among options --- nexus/src/main/kotlin/tech/libeufin/nexus/Main.kt | 9 ++++++-- nexus/src/main/resources/late-logback.xml | 28 +++++++++++++++++++++++ nexus/src/main/resources/logback.xml | 27 ---------------------- util/src/main/kotlin/Config.kt | 20 ++++++++++++++++ 4 files changed, 55 insertions(+), 29 deletions(-) create mode 100644 nexus/src/main/resources/late-logback.xml delete mode 100644 nexus/src/main/resources/logback.xml create mode 100644 util/src/main/kotlin/Config.kt diff --git a/nexus/src/main/kotlin/tech/libeufin/nexus/Main.kt b/nexus/src/main/kotlin/tech/libeufin/nexus/Main.kt index f2b9419b..9896243c 100644 --- a/nexus/src/main/kotlin/tech/libeufin/nexus/Main.kt +++ b/nexus/src/main/kotlin/tech/libeufin/nexus/Main.kt @@ -28,14 +28,15 @@ import com.github.ajalt.clikt.parameters.arguments.argument import com.github.ajalt.clikt.parameters.options.default import com.github.ajalt.clikt.parameters.options.option import com.github.ajalt.clikt.parameters.options.prompt -import com.github.ajalt.clikt.parameters.types.int import org.jetbrains.exposed.sql.transactions.transaction import org.slf4j.Logger import org.slf4j.LoggerFactory import tech.libeufin.nexus.server.serverMain import tech.libeufin.util.CryptoUtil.hashpw +import setLogFile -val logger: Logger = LoggerFactory.getLogger("tech.libeufin.nexus") + +lateinit var logger: Logger class NexusCommand : CliktCommand() { override fun run() = Unit @@ -47,13 +48,17 @@ class Serve : CliktCommand("Run nexus HTTP server") { helpFormatter = CliktHelpFormatter(showDefaultValues = true) } } + private val logFile by option() private val dbName by option().default("libeufin-nexus.sqlite3") private val host by option().default("127.0.0.1") override fun run() { + setLogFile(logFile, "nexusLogFile","late-logback.xml") + logger = LoggerFactory.getLogger("tech.libeufin.nexus") 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/nexus/src/main/resources/late-logback.xml b/nexus/src/main/resources/late-logback.xml new file mode 100644 index 00000000..cd545cbe --- /dev/null +++ b/nexus/src/main/resources/late-logback.xml @@ -0,0 +1,28 @@ + + + + + %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n + + + + + + ${NEXUS_LOG_FILE} + true + + %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n + + + + + + + + + + + + + + \ No newline at end of file diff --git a/nexus/src/main/resources/logback.xml b/nexus/src/main/resources/logback.xml deleted file mode 100644 index 2d6c01ff..00000000 --- a/nexus/src/main/resources/logback.xml +++ /dev/null @@ -1,27 +0,0 @@ - - - - %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n - - - - - - /tmp/nexus.log - true - - %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n - - - - - - - - - - - - - - \ No newline at end of file diff --git a/util/src/main/kotlin/Config.kt b/util/src/main/kotlin/Config.kt new file mode 100644 index 00000000..62f3b276 --- /dev/null +++ b/util/src/main/kotlin/Config.kt @@ -0,0 +1,20 @@ +import ch.qos.logback.classic.util.ContextInitializer +import ch.qos.logback.core.util.Loader + +/** + * Set system properties to wanted values, and load logback configuration after. + * While it can set any system property, it is used only to set the log file name. + * + * @param logFile filename of logfile. If null, then no logfile will be produced. + * @param logFileNameAsProperty property that indicates the logfile name in logback configuration. + * @param configFileName name of logback's config file. Typically something different + * from "logback.xml" (otherwise logback will load it by itself upon startup.) + */ +fun setLogFile(logFile: String?, logFileNameAsProperty: String, configFileName: String) { + if (logFile != null) System.setProperty(logFileNameAsProperty, logFile) + val configFilePath = Loader.getResource(configFileName, ClassLoader.getSystemClassLoader()) + if (configFilePath == null) { + println("Warning: could not find log config file") + } + System.setProperty(ContextInitializer.CONFIG_FILE_PROPERTY, configFilePath.toString()) +} \ No newline at end of file -- cgit v1.2.3 From 3c50e816038d0efa289329f286cb81ac31fbbeee Mon Sep 17 00:00:00 2001 From: MS Date: Wed, 24 Jun 2020 12:09:21 +0200 Subject: sandbox offers logfile name among options --- nexus/src/main/kotlin/tech/libeufin/nexus/Main.kt | 2 +- .../src/main/kotlin/tech/libeufin/sandbox/Main.kt | 6 +++- sandbox/src/main/resources/late-logback.xml | 36 ++++++++++++++++++++++ sandbox/src/main/resources/logback.xml | 35 --------------------- util/src/main/kotlin/Config.kt | 2 ++ 5 files changed, 44 insertions(+), 37 deletions(-) create mode 100644 sandbox/src/main/resources/late-logback.xml delete mode 100644 sandbox/src/main/resources/logback.xml diff --git a/nexus/src/main/kotlin/tech/libeufin/nexus/Main.kt b/nexus/src/main/kotlin/tech/libeufin/nexus/Main.kt index 9896243c..f3510d5e 100644 --- a/nexus/src/main/kotlin/tech/libeufin/nexus/Main.kt +++ b/nexus/src/main/kotlin/tech/libeufin/nexus/Main.kt @@ -33,7 +33,7 @@ import org.slf4j.Logger import org.slf4j.LoggerFactory import tech.libeufin.nexus.server.serverMain import tech.libeufin.util.CryptoUtil.hashpw -import setLogFile +import tech.libeufin.util.* lateinit var logger: Logger diff --git a/sandbox/src/main/kotlin/tech/libeufin/sandbox/Main.kt b/sandbox/src/main/kotlin/tech/libeufin/sandbox/Main.kt index 8f77187b..d88f010e 100644 --- a/sandbox/src/main/kotlin/tech/libeufin/sandbox/Main.kt +++ b/sandbox/src/main/kotlin/tech/libeufin/sandbox/Main.kt @@ -64,13 +64,14 @@ import com.github.ajalt.clikt.parameters.arguments.argument import com.github.ajalt.clikt.parameters.options.default import com.github.ajalt.clikt.parameters.options.option import com.github.ajalt.clikt.parameters.options.prompt +import tech.libeufin.util.* class CustomerNotFound(id: String?) : Exception("Customer ${id} not found") class BadInputData(inputData: String?) : Exception("Customer provided invalid input data: ${inputData}") class UnacceptableFractional(badNumber: BigDecimal) : Exception( "Unacceptable fractional part ${badNumber}" ) -val LOGGER: Logger = LoggerFactory.getLogger("tech.libeufin.sandbox") +lateinit var LOGGER: Logger data class SandboxError( val statusCode: HttpStatusCode, @@ -83,7 +84,10 @@ class SandboxCommand : CliktCommand() { class Serve : CliktCommand("Run sandbox HTTP server") { private val dbName by option().default("libeufin-sandbox.sqlite3") + private val logFile by option() override fun run() { + setLogFile(logFile, "sandboxLogFile", "late-logback.xml") + LOGGER = LoggerFactory.getLogger("tech.libeufin.sandbox") serverMain(dbName) } } diff --git a/sandbox/src/main/resources/late-logback.xml b/sandbox/src/main/resources/late-logback.xml new file mode 100644 index 00000000..ddff9197 --- /dev/null +++ b/sandbox/src/main/resources/late-logback.xml @@ -0,0 +1,36 @@ + + + + %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n + + + + + + ${SANDBOX_LOG_FILE} + false + + %-5relative %-5level %logger{35} - %msg%n + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/sandbox/src/main/resources/logback.xml b/sandbox/src/main/resources/logback.xml deleted file mode 100644 index 7df7bcc7..00000000 --- a/sandbox/src/main/resources/logback.xml +++ /dev/null @@ -1,35 +0,0 @@ - - - - %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n - - - - - /tmp/sandbox.log - false - - %-5relative %-5level %logger{35} - %msg%n - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/util/src/main/kotlin/Config.kt b/util/src/main/kotlin/Config.kt index 62f3b276..86310865 100644 --- a/util/src/main/kotlin/Config.kt +++ b/util/src/main/kotlin/Config.kt @@ -1,3 +1,5 @@ +package tech.libeufin.util + import ch.qos.logback.classic.util.ContextInitializer import ch.qos.logback.core.util.Loader -- cgit v1.2.3