java/copy-image-resizer/src/cz/frantovo/copyImageResizer/logging/ColorfulConsoleFormatter.java
3 * Copyright © 2013 František Kučera (frantovo.cz)
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.
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.
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/>.
18 package cz.frantovo.copyImageResizer.logging;
20 import cz.frantovo.copyImageResizer.logging.ColorfulPrintWriter.TerminalColor;
21 import cz.frantovo.copyImageResizer.logging.ColorfulPrintWriter.TerminalStyle;
22 import java.io.StringWriter;
23 import java.util.logging.Level;
24 import java.util.logging.LogRecord;
27 * For console/terminal log output. Log messages are printed in brief and colorful form.
29 * @author Ing. František Kučera (frantovo.cz)
31 public class ColorfulConsoleFormatter extends AbstractFormatter {
34 public String format(LogRecord r) {
35 StringWriter sw = new StringWriter();
36 try (ColorfulPrintWriter out = new ColorfulPrintWriter(sw)) {
37 printLevel(out, r.getLevel());
39 printThrowable(out, r);
45 private void printLevel(ColorfulPrintWriter out, Level l) {
46 TerminalColor color = TerminalColor.Magenta;
48 if (l == Level.SEVERE) {
49 color = TerminalColor.Red;
50 } else if (l == Level.WARNING) {
51 color = TerminalColor.Yellow;
54 out.print(color, rpad(l.getLocalizedName() + ": ", 10));
57 private void printMessage(ColorfulPrintWriter out, LogRecord r) {
58 out.print(formatMessage(r));
61 private void printThrowable(ColorfulPrintWriter out, LogRecord r) {
62 Throwable t = r.getThrown();
65 out.print(TerminalColor.Red, t.getClass().getSimpleName());
66 String message = t.getLocalizedMessage();
67 if (message != null) {
71 if (isPrintStacktrace()) {
73 out.setForegroundColor(TerminalColor.Yellow);
74 out.setStyle(TerminalStyle.Dim);
75 t.printStackTrace(out);