Skip to content
This repository has been archived by the owner on Mar 22, 2024. It is now read-only.

Support logback context properties #16

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ The general configuration format is as follows:
```scala
logback {
scan-period = 30 seconds

properties {
property1 = "value"
}

appenders {
appender-name {
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/org/gnieh/logback/config/ConfigConfigurator.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, ConfigValue> entry : propertyConfigs.toConfig().entrySet()) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The use of propertyConfigs.toConfig().entrySet() here will flatten any objects so one doesn't have to quote a property named 'x.y.z'.

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));
Expand Down
3 changes: 2 additions & 1 deletion src/main/resources/reference.conf
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
logback-root = logback

logback {
properties {}

loggers {}

appenders = {}
appenders {}

}
7 changes: 5 additions & 2 deletions src/test/resources/rollingFileAppender.conf
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
logback-root = test.logback

test.logback = ${logback} {
properties {
fname = test
}
appenders {
rolling = {
class = "ch.qos.logback.core.rolling.RollingFileAppender"
Expand All @@ -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"

Expand Down