From 3f8f35d043d1887ad37405388a78ad7a14daf771 Mon Sep 17 00:00:00 2001 From: Hubert Holierhoek Date: Fri, 15 Oct 2021 17:23:50 -0400 Subject: [PATCH] Support logback context properties --- README.md | 4 ++++ .../org/gnieh/logback/config/ConfigConfigurator.java | 11 +++++++++++ src/main/resources/reference.conf | 3 ++- src/test/resources/rollingFileAppender.conf | 7 +++++-- 4 files changed, 22 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index bcbbb87..da40b5e 100644 --- a/README.md +++ b/README.md @@ -73,6 +73,10 @@ The general configuration format is as follows: ```scala logback { scan-period = 30 seconds + + properties { + property1 = "value" + } appenders { appender-name { diff --git a/src/main/java/org/gnieh/logback/config/ConfigConfigurator.java b/src/main/java/org/gnieh/logback/config/ConfigConfigurator.java index 805ffba..a1381dc 100644 --- a/src/main/java/org/gnieh/logback/config/ConfigConfigurator.java +++ b/src/main/java/org/gnieh/logback/config/ConfigConfigurator.java @@ -86,6 +86,17 @@ public void configure(LoggerContext loggerContext) { // load the configuration per config loading rules final Config logbackConfig = config.getConfig(logbackConfigRoot); + if (logbackConfig.hasPath("properties")) { + if (logbackConfig.getValue("properties") instanceof ConfigObject) { + final ConfigObject propertyConfigs = (ConfigObject) logbackConfig.getValue("properties"); + for (Entry entry : propertyConfigs.toConfig().entrySet()) { + loggerContext.putProperty(entry.getKey(), entry.getValue().unwrapped().toString()); + } + } else { + addWarn("Invalid properties configuration. Ignoring it."); + } + } + final Config appenderConfigs = logbackConfig.getConfig("appenders"); final ConfigAppendersCache appendersCache = new ConfigAppendersCache(); appendersCache.setLoader(name -> configureAppender(loggerContext, name, appenderConfigs.getConfig("\"" + name + "\""), beanCache, appendersCache)); diff --git a/src/main/resources/reference.conf b/src/main/resources/reference.conf index 39760e2..7c2a4b0 100644 --- a/src/main/resources/reference.conf +++ b/src/main/resources/reference.conf @@ -1,9 +1,10 @@ logback-root = logback logback { + properties {} loggers {} - appenders = {} + appenders {} } diff --git a/src/test/resources/rollingFileAppender.conf b/src/test/resources/rollingFileAppender.conf index f6e99bf..d2460e2 100644 --- a/src/test/resources/rollingFileAppender.conf +++ b/src/test/resources/rollingFileAppender.conf @@ -1,6 +1,9 @@ logback-root = test.logback test.logback = ${logback} { + properties { + fname = test + } appenders { rolling = { class = "ch.qos.logback.core.rolling.RollingFileAppender" @@ -10,12 +13,12 @@ test.logback = ${logback} { pattern = "%date %level %logger %thread %msg%n" } - file = "logs/test.log" + file = "logs/${fname}.log" rolling-policy = { class = "ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy" - file-name-pattern = "logs/test%d{yyyy-MM-dd}.%i.log" + file-name-pattern = "logs/${fname}%d{yyyy-MM-dd}.%i.log" max-file-size = "5MB"