1.1 --- a/java/sql-dk/src/info/globalcode/sql/dk/logging/LoggerInitializer.java Wed Dec 25 01:04:03 2013 +0100
1.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/logging/LoggerInitializer.java Wed Dec 25 01:23:27 2013 +0100
1.3 @@ -19,6 +19,7 @@
1.4
1.5 import info.globalcode.sql.dk.Constants;
1.6 import java.util.logging.ConsoleHandler;
1.7 +import java.util.logging.Handler;
1.8 import java.util.logging.Level;
1.9 import java.util.logging.Logger;
1.10
1.11 @@ -30,6 +31,10 @@
1.12 */
1.13 public class LoggerInitializer {
1.14
1.15 + private static final Logger log = Logger.getLogger(LoggerInitializer.class.getName());
1.16 + public static final String LEVEL_PROPERTY = LoggerInitializer.class.getName() + ".level";
1.17 + private static final Level DEFAULT_LEVEL = Level.INFO;
1.18 +
1.19 public LoggerInitializer() {
1.20 Logger logger = Logger.getLogger(Constants.JAVA_PACKAGE);
1.21 ConsoleHandler handler = new ConsoleHandler();
1.22 @@ -38,12 +43,34 @@
1.23 logger.addHandler(handler);
1.24 handler.setFormatter(formatter);
1.25
1.26 - handler.setLevel(Level.INFO);
1.27 - logger.setLevel(Level.INFO);
1.28 + setLevel(logger, handler);
1.29
1.30
1.31 /**
1.32 * TODO: FileHandler – detailed logs in file in ~/sql-dk/log/…
1.33 */
1.34 }
1.35 +
1.36 + private void setLevel(Logger logger, Handler handler) {
1.37 + boolean levelParseError = false;
1.38 + Level level;
1.39 + String cliLevel = System.getProperty(LEVEL_PROPERTY);
1.40 + if (cliLevel == null) {
1.41 + level = DEFAULT_LEVEL;
1.42 + } else {
1.43 + try {
1.44 + level = Level.parse(cliLevel);
1.45 + } catch (IllegalArgumentException e) {
1.46 + level = DEFAULT_LEVEL;
1.47 + levelParseError = true;
1.48 + }
1.49 + }
1.50 +
1.51 + handler.setLevel(level);
1.52 + logger.setLevel(level);
1.53 +
1.54 + if (levelParseError) {
1.55 + log.log(Level.WARNING, "Invalid logging level „{0}“ specified in „{1}“ → using default level „{2}“", new Object[]{cliLevel, LEVEL_PROPERTY, DEFAULT_LEVEL});
1.56 + }
1.57 + }
1.58 }