Skip to content

Commit

Permalink
Fixes #3, enable to read env variables for getProperty
Browse files Browse the repository at this point in the history
  • Loading branch information
rmannibucau committed Apr 9, 2023
1 parent afe57ff commit 3f7237e
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 5 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<version>3.11.0</version>
<configuration>
<source>11</source>
<target>11</target>
Expand All @@ -86,7 +86,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
<version>3.0.0</version>
<configuration>
<trimStackTrace>false</trimStackTrace>
</configuration>
Expand Down
2 changes: 2 additions & 0 deletions src/main/minisite/content/jul-integration.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -150,3 +150,5 @@ And finally a configuration using file output instead of standard one:
.handlers = file
file.formatter = inline
----

TIP: you can set all properties as system properties and also environment variables (in uppercase and dots replaced by underscores).
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Pattern;
import java.util.stream.Stream;

import static java.util.Collections.enumeration;
Expand All @@ -59,6 +60,8 @@
//
// note: it is not a 100% complete impl yet, it mainly targets docker containers for now and some features as config listeners are ignored
public class YupiikLoggers {
private final Pattern posix = Pattern.compile("[^A-Za-z0-9]");

public static class State { // makes it easy to reset at once
private final ConcurrentMap<Runnable, Runnable> listeners = new ConcurrentHashMap<>();
private final ConcurrentMap<String, YupiikLogger> loggers = new ConcurrentHashMap<>();
Expand Down Expand Up @@ -106,7 +109,9 @@ public YupiikLogger getLoggerOrNull(final String name) {
}

public String getProperty(final String name) {
return System.getProperty(name, state.configuration.get(name));
return ofNullable(System.getProperty(name))
.or(() -> ofNullable(System.getenv(posix.matcher(name).replaceAll("_").toUpperCase(ROOT))))
.orElseGet(() -> state.configuration.get(name));
}

public Enumeration<String> getLoggerNames() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ protected static String replace(final String str) { // [lang3] would be good but
}

final String propName = str.substring(start + 2, end);
String replacement = !propName.isEmpty() ? System.getProperty(propName) : null;
String replacement = !propName.isEmpty() ? readValue(propName) : null;
if (replacement == null) {
replacement = System.getenv(propName);
}
Expand All @@ -488,6 +488,13 @@ protected static String replace(final String str) { // [lang3] would be good but
return result;
}

private static String readValue(final String propName) {
if (propName.startsWith("env.")) {
return System.getenv(propName.substring("env.".length()));
}
return System.getProperty(propName);
}

private final class CountingStream extends OutputStream {
private final OutputStream out;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,6 @@ public String format(final LogRecord record) {
}

private static LocalDate asDate(final Instant instant) {
return instant.atZone(ZoneId.systemDefault()).toLocalDate();
return instant.atZone(ZoneId.of("UTC")).toLocalDate();
}
}

0 comments on commit 3f7237e

Please sign in to comment.