30 * |
30 * |
31 * @author Ing. František Kučera (frantovo.cz) |
31 * @author Ing. František Kučera (frantovo.cz) |
32 */ |
32 */ |
33 public class ColorfulConsoleFormatter extends Formatter { |
33 public class ColorfulConsoleFormatter extends Formatter { |
34 |
34 |
|
35 private boolean printStacktrace = false; |
|
36 |
35 @Override |
37 @Override |
36 public String format(LogRecord r) { |
38 public String format(LogRecord r) { |
37 StringWriter sw = new StringWriter(); |
39 StringWriter sw = new StringWriter(); |
38 try (ColorfulPrintWriter out = new ColorfulPrintWriter(sw)) { |
40 try (ColorfulPrintWriter out = new ColorfulPrintWriter(sw)) { |
39 printLevel(out, r.getLevel()); |
41 printLevel(out, r.getLevel()); |
40 printMessage(out, r); |
42 printMessage(out, r); |
41 printThrowable(out, r.getThrown()); |
43 printThrowable(out, r); |
42 out.println(); |
44 out.println(); |
43 } |
45 } |
44 return sw.toString(); |
46 return sw.toString(); |
45 } |
47 } |
46 |
48 |
47 private void printLevel(ColorfulPrintWriter out, Level l) { |
49 private void printLevel(ColorfulPrintWriter out, Level l) { |
48 TerminalColor color = TerminalColor.Magenta; |
50 TerminalColor color = TerminalColor.Magenta; |
49 TerminalStyle style; |
|
50 |
51 |
51 if (l == Level.SEVERE) { |
52 if (l == Level.SEVERE) { |
52 color = TerminalColor.Red; |
53 color = TerminalColor.Red; |
53 } else if (l == Level.WARNING) { |
54 } else if (l == Level.WARNING) { |
54 color = TerminalColor.Yellow; |
55 color = TerminalColor.Yellow; |
59 |
60 |
60 private void printMessage(ColorfulPrintWriter out, LogRecord r) { |
61 private void printMessage(ColorfulPrintWriter out, LogRecord r) { |
61 out.print(formatMessage(r)); |
62 out.print(formatMessage(r)); |
62 } |
63 } |
63 |
64 |
64 private void printThrowable(ColorfulPrintWriter out, Throwable t) { |
65 private void printThrowable(ColorfulPrintWriter out, LogRecord r) { |
|
66 Throwable t = r.getThrown(); |
65 if (t != null) { |
67 if (t != null) { |
66 out.print(": "); |
68 out.print(": "); |
67 out.print(TerminalColor.Red, t.getClass().getSimpleName()); |
69 out.print(TerminalColor.Red, t.getClass().getSimpleName()); |
68 String message = t.getLocalizedMessage(); |
70 String message = t.getLocalizedMessage(); |
69 if (message != null) { |
71 if (message != null) { |
70 out.print(": "); |
72 out.print(": "); |
71 out.print(message); |
73 out.print(message); |
72 } |
74 } |
|
75 if (printStacktrace) { |
|
76 out.println(); |
|
77 out.setForegroundColor(TerminalColor.Yellow); |
|
78 out.setStyle(TerminalStyle.Dim); |
|
79 t.printStackTrace(out); |
|
80 out.resetAll(); |
|
81 } |
73 } |
82 } |
74 } |
83 } |
|
84 |
|
85 public boolean isPrintStacktrace() { |
|
86 return printStacktrace; |
|
87 } |
|
88 |
|
89 public void setPrintStacktrace(boolean printStacktrace) { |
|
90 this.printStacktrace = printStacktrace; |
|
91 } |
75 } |
92 } |