franta-hg@55
|
1 |
/**
|
franta-hg@55
|
2 |
* SQL-DK
|
franta-hg@55
|
3 |
* Copyright © 2013 František Kučera (frantovo.cz)
|
franta-hg@55
|
4 |
*
|
franta-hg@55
|
5 |
* This program is free software: you can redistribute it and/or modify
|
franta-hg@55
|
6 |
* it under the terms of the GNU General Public License as published by
|
franta-hg@55
|
7 |
* the Free Software Foundation, either version 3 of the License, or
|
franta-hg@55
|
8 |
* (at your option) any later version.
|
franta-hg@55
|
9 |
*
|
franta-hg@55
|
10 |
* This program is distributed in the hope that it will be useful,
|
franta-hg@55
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
franta-hg@55
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
franta-hg@55
|
13 |
* GNU General Public License for more details.
|
franta-hg@55
|
14 |
*
|
franta-hg@55
|
15 |
* You should have received a copy of the GNU General Public License
|
franta-hg@55
|
16 |
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
franta-hg@55
|
17 |
*/
|
franta-hg@55
|
18 |
package info.globalcode.sql.dk.logging;
|
franta-hg@55
|
19 |
|
franta-hg@55
|
20 |
import info.globalcode.sql.dk.ColorfulPrintWriter;
|
franta-hg@55
|
21 |
import static info.globalcode.sql.dk.ColorfulPrintWriter.TerminalColor;
|
franta-hg@55
|
22 |
import static info.globalcode.sql.dk.ColorfulPrintWriter.TerminalStyle;
|
franta-hg@55
|
23 |
import static info.globalcode.sql.dk.Functions.rpad;
|
franta-hg@55
|
24 |
import java.io.StringWriter;
|
franta-hg@55
|
25 |
import java.util.logging.Formatter;
|
franta-hg@55
|
26 |
import java.util.logging.Level;
|
franta-hg@55
|
27 |
import java.util.logging.LogRecord;
|
franta-hg@55
|
28 |
|
franta-hg@55
|
29 |
/**
|
franta-hg@155
|
30 |
* For console/terminal log output. Log messages are printed in brief and colorful form.
|
franta-hg@55
|
31 |
*
|
franta-hg@55
|
32 |
* @author Ing. František Kučera (frantovo.cz)
|
franta-hg@55
|
33 |
*/
|
franta-hg@55
|
34 |
public class ColorfulConsoleFormatter extends Formatter {
|
franta-hg@55
|
35 |
|
franta-hg@59
|
36 |
private boolean printStacktrace = false;
|
franta-hg@59
|
37 |
|
franta-hg@55
|
38 |
@Override
|
franta-hg@55
|
39 |
public String format(LogRecord r) {
|
franta-hg@55
|
40 |
StringWriter sw = new StringWriter();
|
franta-hg@55
|
41 |
try (ColorfulPrintWriter out = new ColorfulPrintWriter(sw)) {
|
franta-hg@55
|
42 |
printLevel(out, r.getLevel());
|
franta-hg@55
|
43 |
printMessage(out, r);
|
franta-hg@59
|
44 |
printThrowable(out, r);
|
franta-hg@55
|
45 |
out.println();
|
franta-hg@55
|
46 |
}
|
franta-hg@55
|
47 |
return sw.toString();
|
franta-hg@55
|
48 |
}
|
franta-hg@55
|
49 |
|
franta-hg@55
|
50 |
private void printLevel(ColorfulPrintWriter out, Level l) {
|
franta-hg@55
|
51 |
TerminalColor color = TerminalColor.Magenta;
|
franta-hg@55
|
52 |
|
franta-hg@55
|
53 |
if (l == Level.SEVERE) {
|
franta-hg@55
|
54 |
color = TerminalColor.Red;
|
franta-hg@55
|
55 |
} else if (l == Level.WARNING) {
|
franta-hg@55
|
56 |
color = TerminalColor.Yellow;
|
franta-hg@55
|
57 |
}
|
franta-hg@55
|
58 |
|
franta-hg@55
|
59 |
out.print(color, rpad(l.getLocalizedName() + ": ", 10));
|
franta-hg@55
|
60 |
}
|
franta-hg@55
|
61 |
|
franta-hg@55
|
62 |
private void printMessage(ColorfulPrintWriter out, LogRecord r) {
|
franta-hg@55
|
63 |
out.print(formatMessage(r));
|
franta-hg@55
|
64 |
}
|
franta-hg@55
|
65 |
|
franta-hg@59
|
66 |
private void printThrowable(ColorfulPrintWriter out, LogRecord r) {
|
franta-hg@59
|
67 |
Throwable t = r.getThrown();
|
franta-hg@55
|
68 |
if (t != null) {
|
franta-hg@55
|
69 |
out.print(": ");
|
franta-hg@55
|
70 |
out.print(TerminalColor.Red, t.getClass().getSimpleName());
|
franta-hg@55
|
71 |
String message = t.getLocalizedMessage();
|
franta-hg@55
|
72 |
if (message != null) {
|
franta-hg@55
|
73 |
out.print(": ");
|
franta-hg@182
|
74 |
if (printStacktrace) {
|
franta-hg@182
|
75 |
out.print(message);
|
franta-hg@182
|
76 |
} else {
|
franta-hg@182
|
77 |
out.print(message.replaceAll("\\n", " "));
|
franta-hg@182
|
78 |
}
|
franta-hg@55
|
79 |
}
|
franta-hg@59
|
80 |
if (printStacktrace) {
|
franta-hg@59
|
81 |
out.println();
|
franta-hg@59
|
82 |
out.setForegroundColor(TerminalColor.Yellow);
|
franta-hg@59
|
83 |
out.setStyle(TerminalStyle.Dim);
|
franta-hg@59
|
84 |
t.printStackTrace(out);
|
franta-hg@59
|
85 |
out.resetAll();
|
franta-hg@59
|
86 |
}
|
franta-hg@55
|
87 |
}
|
franta-hg@55
|
88 |
}
|
franta-hg@59
|
89 |
|
franta-hg@59
|
90 |
public boolean isPrintStacktrace() {
|
franta-hg@59
|
91 |
return printStacktrace;
|
franta-hg@59
|
92 |
}
|
franta-hg@59
|
93 |
|
franta-hg@59
|
94 |
public void setPrintStacktrace(boolean printStacktrace) {
|
franta-hg@59
|
95 |
this.printStacktrace = printStacktrace;
|
franta-hg@59
|
96 |
}
|
franta-hg@55
|
97 |
}
|