Skip to content

Commit

Permalink
* Improve parsing of levels... also accept DEBUG etc
Browse files Browse the repository at this point in the history
  • Loading branch information
blaubaer committed Nov 7, 2018
1 parent 2a734c8 commit a7c951b
Showing 1 changed file with 21 additions and 7 deletions.
28 changes: 21 additions & 7 deletions core/src/main/java/org/echocat/tjl/Handler.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@
import java.io.OutputStream;
import java.security.PrivilegedAction;
import java.util.Optional;
import java.util.logging.*;
import java.util.logging.Formatter;
import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.StreamHandler;

import static java.lang.ClassLoader.getSystemClassLoader;
import static java.lang.System.getProperty;
import static java.lang.System.getenv;
import static java.security.AccessController.doPrivileged;
import static java.util.Optional.ofNullable;
import static java.util.logging.Level.ALL;
import static java.util.logging.Level.parse;
import static java.util.logging.Level.*;
import static java.util.logging.LogManager.getLogManager;

public class Handler extends StreamHandler {
Expand Down Expand Up @@ -67,18 +69,30 @@ protected Formatter determineFormatter() {

protected Level determineLevel() {
return findProperty(getClass(), "level")
.map(value -> parse(value.trim()))
.map(this::parseLevel)
.orElse(ALL);
}

protected void patchGlobalLevelIfRequired() {
findSystemProperty("log.level", "LOG_LEVEL")
.map(String::trim)
.map(String::toUpperCase)
.map(Level::parse)
.map(this::parseLevel)
.ifPresent(level -> getLogManager().getLogger("").setLevel(level));
}

protected Level parseLevel(String plain) {
final String input = plain.trim().toUpperCase();
if (input.equals("DEBUG")) {
return FINE;
}
if (input.equals("TRACE")) {
return FINEST;
}
if (input.equals("ERROR") || input.equals("FATAL")) {
return SEVERE;
}
return Level.parse(input);
}

protected Object newInstanceOf(String className) {
try {
//noinspection deprecation
Expand Down

0 comments on commit a7c951b

Please sign in to comment.