java/sql-dk/src/info/globalcode/sql/dk/logging/LoggerInitializer.java
branchv_0
changeset 238 4a1864c3e867
parent 237 7e08730da258
child 239 39e6c2ad3571
equal deleted inserted replaced
237:7e08730da258 238:4a1864c3e867
     1 /**
       
     2  * SQL-DK
       
     3  * Copyright © 2013 František Kučera (frantovo.cz)
       
     4  *
       
     5  * This program is free software: you can redistribute it and/or modify
       
     6  * it under the terms of the GNU General Public License as published by
       
     7  * the Free Software Foundation, either version 3 of the License, or
       
     8  * (at your option) any later version.
       
     9  *
       
    10  * This program is distributed in the hope that it will be useful,
       
    11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
       
    13  * GNU General Public License for more details.
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License
       
    16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
       
    17  */
       
    18 package info.globalcode.sql.dk.logging;
       
    19 
       
    20 import info.globalcode.sql.dk.Constants;
       
    21 import java.util.logging.ConsoleHandler;
       
    22 import java.util.logging.Handler;
       
    23 import java.util.logging.Level;
       
    24 import java.util.logging.Logger;
       
    25 
       
    26 /**
       
    27  * Configures logging subsystem.
       
    28  * Usage: java -Djava.util.logging.config.class=info.globalcode.sql.dk.logging.LoggerInitializer …
       
    29  *
       
    30  * @author Ing. František Kučera (frantovo.cz)
       
    31  */
       
    32 public class LoggerInitializer {
       
    33 
       
    34 	private static final Logger log = Logger.getLogger(LoggerInitializer.class.getName());
       
    35 	public static final String LEVEL_PROPERTY = LoggerInitializer.class.getName() + ".level";
       
    36 	private static final Level DEFAULT_LEVEL = Level.INFO;
       
    37 
       
    38 	public LoggerInitializer() {
       
    39 		Logger logger = Logger.getLogger(Constants.JAVA_PACKAGE);
       
    40 		ConsoleHandler handler = new ConsoleHandler();
       
    41 		ColorfulConsoleFormatter formatter = new ColorfulConsoleFormatter();
       
    42 
       
    43 		logger.addHandler(handler);
       
    44 		handler.setFormatter(formatter);
       
    45 
       
    46 		setLevel(logger, handler, formatter);
       
    47 
       
    48 
       
    49 		/**
       
    50 		 * TODO: optional FileHandler – detailed logs in file in ~/sql-dk/log/…
       
    51 		 */
       
    52 	}
       
    53 
       
    54 	private void setLevel(Logger logger, Handler handler, ColorfulConsoleFormatter formatter) {
       
    55 		boolean levelParseError = false;
       
    56 		Level level;
       
    57 		String cliLevel = System.getProperty(LEVEL_PROPERTY);
       
    58 		if (cliLevel == null) {
       
    59 			level = DEFAULT_LEVEL;
       
    60 		} else {
       
    61 			try {
       
    62 				level = Level.parse(cliLevel);
       
    63 			} catch (IllegalArgumentException e) {
       
    64 				level = DEFAULT_LEVEL;
       
    65 				levelParseError = true;
       
    66 			}
       
    67 		}
       
    68 
       
    69 		handler.setLevel(level);
       
    70 		logger.setLevel(level);
       
    71 
       
    72 		if (levelParseError) {
       
    73 			log.log(Level.WARNING, "Invalid logging level „{0}“ specified in „{1}“ → using default level „{2}“", new Object[]{cliLevel, LEVEL_PROPERTY, DEFAULT_LEVEL});
       
    74 		}
       
    75 
       
    76 		formatter.setPrintStacktrace(level.intValue() < Level.INFO.intValue());
       
    77 	}
       
    78 }