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