Skip to content

Commit 46e2ee3

Browse files
authored
Merge pull request #125 from avaje/feature/dynamic-logback-debug
dynamic-logback: improve debug logging
2 parents 9d17317 + 83097cb commit 46e2ee3

File tree

7 files changed

+66
-5
lines changed

7 files changed

+66
-5
lines changed

avaje-aws-appconfig/src/main/java/io/avaje/config/appconfig/AppConfigFetcher.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package io.avaje.config.appconfig;
22

3+
import java.net.URI;
4+
35
interface AppConfigFetcher {
46

57
static AppConfigFetcher.Builder builder() {
@@ -8,6 +10,8 @@ static AppConfigFetcher.Builder builder() {
810

911
Result fetch() throws FetchException;
1012

13+
URI uri();
14+
1115
class FetchException extends Exception {
1216

1317
public FetchException(Exception e) {

avaje-aws-appconfig/src/main/java/io/avaje/config/appconfig/AppConfigPlugin.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,16 @@ static final class Loader {
6464

6565
var app = configuration.get("aws.appconfig.application");
6666
var env = configuration.get("aws.appconfig.environment");
67-
var con = configuration.get("aws.appconfig.configuration", env + "-" + app);
67+
var con = configuration.get("aws.appconfig.configuration", "default");
6868

6969
this.fetcher = AppConfigFetcher.builder()
7070
.application(app)
7171
.environment(env)
7272
.configuration(con)
7373
.build();
7474

75+
log.log(DEBUG, "AwsAppConfig uri {0}", fetcher.uri());
76+
7577
boolean pollEnabled = configuration.enabled("aws.appconfig.pollingEnabled", true);
7678
long pollSeconds = configuration.getLong("aws.appconfig.pollingSeconds", 45L);
7779
this.nextRefreshSeconds = configuration.getLong("aws.appconfig.refreshSeconds", pollSeconds - 1);

avaje-aws-appconfig/src/main/java/io/avaje/config/appconfig/DAppConfigFetcher.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ final class DAppConfigFetcher implements AppConfigFetcher {
1717
.build();
1818
}
1919

20+
@Override
21+
public URI uri() {
22+
return uri;
23+
}
24+
2025
@Override
2126
public AppConfigFetcher.Result fetch() throws FetchException {
2227
HttpRequest request = HttpRequest.newBuilder()

avaje-dynamic-logback/pom.xml

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,19 @@
3232
<groupId>ch.qos.logback</groupId>
3333
<artifactId>logback-classic</artifactId>
3434
<version>1.5.0</version>
35-
<scope>provided</scope>
35+
</dependency>
36+
37+
<dependency>
38+
<groupId>io.avaje</groupId>
39+
<artifactId>avaje-applog-slf4j</artifactId>
40+
<version>1.0</version>
41+
</dependency>
42+
43+
<dependency>
44+
<groupId>io.avaje</groupId>
45+
<artifactId>junit</artifactId>
46+
<version>1.4</version>
47+
<scope>test</scope>
3648
</dependency>
3749

3850
</dependencies>

avaje-dynamic-logback/src/main/java/io/avaje/config/dynamiclogback/LogbackPlugin.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import org.slf4j.LoggerFactory;
1111

1212
import static java.lang.System.Logger.Level.DEBUG;
13+
import static java.lang.System.Logger.Level.TRACE;
1314

1415
/**
1516
* Plugin to dynamically adjust the log levels via configuration changes.
@@ -25,14 +26,14 @@ public void apply(Configuration configuration) {
2526
for (String key : config.keys()) {
2627
String rawLevel = config.getNullable(key);
2728
setLogLevel(key, loggerContext, rawLevel);
29+
log.log(TRACE, "log level {0} for {1}", rawLevel, key);
2830
}
2931
configuration.onChange(this::onChangeAny);
3032
}
3133

3234
private static void setLogLevel(String key, LoggerContext loggerContext, String level) {
3335
Logger logger = loggerContext.getLogger(key);
3436
if (logger != null && level != null) {
35-
log.log(DEBUG, "logger change for {0} to {1}", key, level);
3637
logger.setLevel(Level.toLevel(level));
3738
}
3839
}
@@ -43,8 +44,10 @@ private void onChangeAny(ModificationEvent modificationEvent) {
4344
modificationEvent.modifiedKeys().stream()
4445
.filter(key -> key.startsWith("log.level."))
4546
.forEach(key -> {
46-
String rawLevel = config.getNullable(key.substring(10));
47-
setLogLevel(key, loggerContext, rawLevel);
47+
String logKey = key.substring(10);
48+
String rawLevel = config.getNullable(key);
49+
setLogLevel(logKey, loggerContext, rawLevel);
50+
log.log(DEBUG, "set log level {0} for {1}", rawLevel, logKey);
4851
});
4952
}
5053

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package io.avaje.config.dynamiclogback;
2+
3+
import io.avaje.config.Configuration;
4+
import org.junit.jupiter.api.Test;
5+
6+
import java.util.Map;
7+
8+
class LogbackPluginTest {
9+
10+
@Test
11+
void apply() {
12+
Configuration config = Configuration.builder()
13+
.put("log.level.other.Foo", "DEBUG")
14+
.build();
15+
16+
var plugin = new LogbackPlugin();
17+
plugin.apply(config);
18+
19+
config.putAll(Map.of("log.level.other.Foo", "INFO", "log.level.my.Bar", "TRACE"));
20+
}
21+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<configuration>
2+
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
3+
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
4+
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
5+
</encoder>
6+
</appender>
7+
8+
<root level="DEBUG">
9+
<appender-ref ref="STDOUT"/>
10+
</root>
11+
12+
<logger name="io.avaje" level="TRACE"/>
13+
14+
</configuration>

0 commit comments

Comments
 (0)