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 59 5f745ae795a8
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
--- a/java/sql-dk/src/info/globalcode/sql/dk/ColorfulPrintWriter.java	Wed Dec 25 01:23:27 2013 +0100
+++ b/java/sql-dk/src/info/globalcode/sql/dk/ColorfulPrintWriter.java	Wed Dec 25 02:04:57 2013 +0100
@@ -79,6 +79,10 @@
 	}
 	private boolean colorful = true;
 
+	public void setStyle(TerminalStyle style) {
+		setStyle(EnumSet.of(style));
+	}
+
 	public void setStyle(EnumSet<TerminalStyle> styles) {
 		printCodes(getStyleCodes(styles));
 	}
--- a/java/sql-dk/src/info/globalcode/sql/dk/logging/ColorfulConsoleFormatter.java	Wed Dec 25 01:23:27 2013 +0100
+++ b/java/sql-dk/src/info/globalcode/sql/dk/logging/ColorfulConsoleFormatter.java	Wed Dec 25 02:04:57 2013 +0100
@@ -32,13 +32,15 @@
  */
 public class ColorfulConsoleFormatter extends Formatter {
 
+	private boolean printStacktrace = false;
+
 	@Override
 	public String format(LogRecord r) {
 		StringWriter sw = new StringWriter();
 		try (ColorfulPrintWriter out = new ColorfulPrintWriter(sw)) {
 			printLevel(out, r.getLevel());
 			printMessage(out, r);
-			printThrowable(out, r.getThrown());
+			printThrowable(out, r);
 			out.println();
 		}
 		return sw.toString();
@@ -46,7 +48,6 @@
 
 	private void printLevel(ColorfulPrintWriter out, Level l) {
 		TerminalColor color = TerminalColor.Magenta;
-		TerminalStyle style;
 
 		if (l == Level.SEVERE) {
 			color = TerminalColor.Red;
@@ -61,7 +62,8 @@
 		out.print(formatMessage(r));
 	}
 
-	private void printThrowable(ColorfulPrintWriter out, Throwable t) {
+	private void printThrowable(ColorfulPrintWriter out, LogRecord r) {
+		Throwable t = r.getThrown();
 		if (t != null) {
 			out.print(": ");
 			out.print(TerminalColor.Red, t.getClass().getSimpleName());
@@ -70,6 +72,21 @@
 				out.print(": ");
 				out.print(message);
 			}
+			if (printStacktrace) {
+				out.println();
+				out.setForegroundColor(TerminalColor.Yellow);
+				out.setStyle(TerminalStyle.Dim);
+				t.printStackTrace(out);
+				out.resetAll();
+			}
 		}
 	}
+
+	public boolean isPrintStacktrace() {
+		return printStacktrace;
+	}
+
+	public void setPrintStacktrace(boolean printStacktrace) {
+		this.printStacktrace = printStacktrace;
+	}
 }
--- a/java/sql-dk/src/info/globalcode/sql/dk/logging/LoggerInitializer.java	Wed Dec 25 01:23:27 2013 +0100
+++ b/java/sql-dk/src/info/globalcode/sql/dk/logging/LoggerInitializer.java	Wed Dec 25 02:04:57 2013 +0100
@@ -43,7 +43,7 @@
 		logger.addHandler(handler);
 		handler.setFormatter(formatter);
 
-		setLevel(logger, handler);
+		setLevel(logger, handler, formatter);
 
 
 		/**
@@ -51,7 +51,7 @@
 		 */
 	}
 
-	private void setLevel(Logger logger, Handler handler) {
+	private void setLevel(Logger logger, Handler handler, ColorfulConsoleFormatter formatter) {
 		boolean levelParseError = false;
 		Level level;
 		String cliLevel = System.getProperty(LEVEL_PROPERTY);
@@ -72,5 +72,7 @@
 		if (levelParseError) {
 			log.log(Level.WARNING, "Invalid logging level „{0}“ specified in „{1}“ → using default level „{2}“", new Object[]{cliLevel, LEVEL_PROPERTY, DEFAULT_LEVEL});
 		}
+
+		formatter.setPrintStacktrace(level.intValue() < Level.INFO.intValue());
 	}
 }