libeufin

Integration and sandbox testing for FinTech APIs and data formats
Log | Files | Refs | Submodules | README | LICENSE

commit 6044b5c2fd83632eb1441545cb1d427a74e98b0a
parent 104081254163dea144ed24d0b344acf5bafd5285
Author: MS <ms@taler.net>
Date:   Wed, 24 Jun 2020 11:52:08 +0200

nexus offers logfile name among options

Diffstat:
Mnexus/src/main/kotlin/tech/libeufin/nexus/Main.kt | 9+++++++--
Anexus/src/main/resources/late-logback.xml | 29+++++++++++++++++++++++++++++
Dnexus/src/main/resources/logback.xml | 28----------------------------
Autil/src/main/kotlin/Config.kt | 21+++++++++++++++++++++
4 files changed, 57 insertions(+), 30 deletions(-)

diff --git 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 @@ -0,0 +1,28 @@ +<!-- configuration scan="true" --> +<configuration scan="true"> + <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> + <encoder> + <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern> + </encoder> + </appender> + + <property name="NEXUS_LOG_FILE" value="${nexusLogFile}" /> + <appender name="NEXUS-FILE" class="ch.qos.logback.core.FileAppender"> + <file>${NEXUS_LOG_FILE}</file> + <append>true</append> + <encoder> + <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern> + </encoder> + </appender> + + <logger name="tech.libeufin" level="TRACE"/> + <logger name="io.netty" level="WARN"/> + <logger name="ktor" level="WARN"/> + <logger name="Exposed" level="WARN"/> + + <root level="WARN"> + <appender-ref ref="STDOUT"/> + <appender-ref ref="NEXUS-FILE"/> + </root> + +</configuration> +\ No newline at end of file diff --git a/nexus/src/main/resources/logback.xml b/nexus/src/main/resources/logback.xml @@ -1,27 +0,0 @@ -<configuration> - <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> - <encoder> - <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern> - </encoder> - </appender> - - - <appender name="NEXUS-FILE" class="ch.qos.logback.core.FileAppender"> - <file>/tmp/nexus.log</file> - <append>true</append> - <encoder> - <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern> - </encoder> - </appender> - - <logger name="tech.libeufin" level="TRACE"/> - <logger name="io.netty" level="WARN"/> - <logger name="ktor" level="WARN"/> - <logger name="Exposed" level="WARN"/> - - <root level="WARN"> - <appender-ref ref="STDOUT"/> - <appender-ref ref="NEXUS-FILE"/> - </root> - -</configuration> -\ No newline at end of file diff --git a/util/src/main/kotlin/Config.kt 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