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