logging: print stacktraces if level is less than INFO v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Wed, 25 Dec 2013 02:04:57 +0100
branchv_0
changeset 595f745ae795a8
parent 58 fd2ac24c6a22
child 60 d4e88172a363
logging: print stacktraces if level is less than INFO
java/sql-dk/src/info/globalcode/sql/dk/ColorfulPrintWriter.java
java/sql-dk/src/info/globalcode/sql/dk/logging/ColorfulConsoleFormatter.java
java/sql-dk/src/info/globalcode/sql/dk/logging/LoggerInitializer.java
     1.1 --- a/java/sql-dk/src/info/globalcode/sql/dk/ColorfulPrintWriter.java	Wed Dec 25 01:23:27 2013 +0100
     1.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/ColorfulPrintWriter.java	Wed Dec 25 02:04:57 2013 +0100
     1.3 @@ -79,6 +79,10 @@
     1.4  	}
     1.5  	private boolean colorful = true;
     1.6  
     1.7 +	public void setStyle(TerminalStyle style) {
     1.8 +		setStyle(EnumSet.of(style));
     1.9 +	}
    1.10 +
    1.11  	public void setStyle(EnumSet<TerminalStyle> styles) {
    1.12  		printCodes(getStyleCodes(styles));
    1.13  	}
     2.1 --- a/java/sql-dk/src/info/globalcode/sql/dk/logging/ColorfulConsoleFormatter.java	Wed Dec 25 01:23:27 2013 +0100
     2.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/logging/ColorfulConsoleFormatter.java	Wed Dec 25 02:04:57 2013 +0100
     2.3 @@ -32,13 +32,15 @@
     2.4   */
     2.5  public class ColorfulConsoleFormatter extends Formatter {
     2.6  
     2.7 +	private boolean printStacktrace = false;
     2.8 +
     2.9  	@Override
    2.10  	public String format(LogRecord r) {
    2.11  		StringWriter sw = new StringWriter();
    2.12  		try (ColorfulPrintWriter out = new ColorfulPrintWriter(sw)) {
    2.13  			printLevel(out, r.getLevel());
    2.14  			printMessage(out, r);
    2.15 -			printThrowable(out, r.getThrown());
    2.16 +			printThrowable(out, r);
    2.17  			out.println();
    2.18  		}
    2.19  		return sw.toString();
    2.20 @@ -46,7 +48,6 @@
    2.21  
    2.22  	private void printLevel(ColorfulPrintWriter out, Level l) {
    2.23  		TerminalColor color = TerminalColor.Magenta;
    2.24 -		TerminalStyle style;
    2.25  
    2.26  		if (l == Level.SEVERE) {
    2.27  			color = TerminalColor.Red;
    2.28 @@ -61,7 +62,8 @@
    2.29  		out.print(formatMessage(r));
    2.30  	}
    2.31  
    2.32 -	private void printThrowable(ColorfulPrintWriter out, Throwable t) {
    2.33 +	private void printThrowable(ColorfulPrintWriter out, LogRecord r) {
    2.34 +		Throwable t = r.getThrown();
    2.35  		if (t != null) {
    2.36  			out.print(": ");
    2.37  			out.print(TerminalColor.Red, t.getClass().getSimpleName());
    2.38 @@ -70,6 +72,21 @@
    2.39  				out.print(": ");
    2.40  				out.print(message);
    2.41  			}
    2.42 +			if (printStacktrace) {
    2.43 +				out.println();
    2.44 +				out.setForegroundColor(TerminalColor.Yellow);
    2.45 +				out.setStyle(TerminalStyle.Dim);
    2.46 +				t.printStackTrace(out);
    2.47 +				out.resetAll();
    2.48 +			}
    2.49  		}
    2.50  	}
    2.51 +
    2.52 +	public boolean isPrintStacktrace() {
    2.53 +		return printStacktrace;
    2.54 +	}
    2.55 +
    2.56 +	public void setPrintStacktrace(boolean printStacktrace) {
    2.57 +		this.printStacktrace = printStacktrace;
    2.58 +	}
    2.59  }
     3.1 --- a/java/sql-dk/src/info/globalcode/sql/dk/logging/LoggerInitializer.java	Wed Dec 25 01:23:27 2013 +0100
     3.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/logging/LoggerInitializer.java	Wed Dec 25 02:04:57 2013 +0100
     3.3 @@ -43,7 +43,7 @@
     3.4  		logger.addHandler(handler);
     3.5  		handler.setFormatter(formatter);
     3.6  
     3.7 -		setLevel(logger, handler);
     3.8 +		setLevel(logger, handler, formatter);
     3.9  
    3.10  
    3.11  		/**
    3.12 @@ -51,7 +51,7 @@
    3.13  		 */
    3.14  	}
    3.15  
    3.16 -	private void setLevel(Logger logger, Handler handler) {
    3.17 +	private void setLevel(Logger logger, Handler handler, ColorfulConsoleFormatter formatter) {
    3.18  		boolean levelParseError = false;
    3.19  		Level level;
    3.20  		String cliLevel = System.getProperty(LEVEL_PROPERTY);
    3.21 @@ -72,5 +72,7 @@
    3.22  		if (levelParseError) {
    3.23  			log.log(Level.WARNING, "Invalid logging level „{0}“ specified in „{1}“ → using default level „{2}“", new Object[]{cliLevel, LEVEL_PROPERTY, DEFAULT_LEVEL});
    3.24  		}
    3.25 +
    3.26 +		formatter.setPrintStacktrace(level.intValue() < Level.INFO.intValue());
    3.27  	}
    3.28  }