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.ColorfulPrintWriter; |
|
21 import static info.globalcode.sql.dk.ColorfulPrintWriter.TerminalColor; |
|
22 import static info.globalcode.sql.dk.ColorfulPrintWriter.TerminalStyle; |
|
23 import static info.globalcode.sql.dk.Functions.rpad; |
|
24 import java.io.StringWriter; |
|
25 import java.util.logging.Formatter; |
|
26 import java.util.logging.Level; |
|
27 import java.util.logging.LogRecord; |
|
28 |
|
29 /** |
|
30 * For console/terminal log output. Log messages are printed in brief and colorful form. |
|
31 * |
|
32 * @author Ing. František Kučera (frantovo.cz) |
|
33 */ |
|
34 public class ColorfulConsoleFormatter extends Formatter { |
|
35 |
|
36 private boolean printStacktrace = false; |
|
37 |
|
38 @Override |
|
39 public String format(LogRecord r) { |
|
40 StringWriter sw = new StringWriter(); |
|
41 try (ColorfulPrintWriter out = new ColorfulPrintWriter(sw)) { |
|
42 printLevel(out, r.getLevel()); |
|
43 printMessage(out, r); |
|
44 printThrowable(out, r); |
|
45 out.println(); |
|
46 } |
|
47 return sw.toString(); |
|
48 } |
|
49 |
|
50 private void printLevel(ColorfulPrintWriter out, Level l) { |
|
51 TerminalColor color = TerminalColor.Magenta; |
|
52 |
|
53 if (l == Level.SEVERE) { |
|
54 color = TerminalColor.Red; |
|
55 } else if (l == Level.WARNING) { |
|
56 color = TerminalColor.Yellow; |
|
57 } |
|
58 |
|
59 out.print(color, rpad(l.getLocalizedName() + ": ", 10)); |
|
60 } |
|
61 |
|
62 private void printMessage(ColorfulPrintWriter out, LogRecord r) { |
|
63 out.print(formatMessage(r)); |
|
64 } |
|
65 |
|
66 private void printThrowable(ColorfulPrintWriter out, LogRecord r) { |
|
67 Throwable t = r.getThrown(); |
|
68 if (t != null) { |
|
69 out.print(": "); |
|
70 out.print(TerminalColor.Red, t.getClass().getSimpleName()); |
|
71 String message = t.getLocalizedMessage(); |
|
72 if (message != null) { |
|
73 out.print(": "); |
|
74 if (printStacktrace) { |
|
75 out.print(message); |
|
76 } else { |
|
77 out.print(message.replaceAll("\\n", " ")); |
|
78 } |
|
79 } |
|
80 if (printStacktrace) { |
|
81 out.println(); |
|
82 out.setForegroundColor(TerminalColor.Yellow); |
|
83 out.setStyle(TerminalStyle.Dim); |
|
84 t.printStackTrace(out); |
|
85 out.resetAll(); |
|
86 } |
|
87 } |
|
88 } |
|
89 |
|
90 public boolean isPrintStacktrace() { |
|
91 return printStacktrace; |
|
92 } |
|
93 |
|
94 public void setPrintStacktrace(boolean printStacktrace) { |
|
95 this.printStacktrace = printStacktrace; |
|
96 } |
|
97 } |
|