franta-hg@8: /** franta-hg@8: * copy-image-resizer franta-hg@8: * Copyright © 2013 František Kučera (frantovo.cz) franta-hg@8: * franta-hg@8: * This program is free software: you can redistribute it and/or modify franta-hg@8: * it under the terms of the GNU General Public License as published by franta-hg@8: * the Free Software Foundation, either version 3 of the License, or franta-hg@8: * (at your option) any later version. franta-hg@8: * franta-hg@8: * This program is distributed in the hope that it will be useful, franta-hg@8: * but WITHOUT ANY WARRANTY; without even the implied warranty of franta-hg@8: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the franta-hg@8: * GNU General Public License for more details. franta-hg@8: * franta-hg@8: * You should have received a copy of the GNU General Public License franta-hg@8: * along with this program. If not, see . franta-hg@8: */ franta-hg@8: package cz.frantovo.copyImageResizer.logging; franta-hg@8: franta-hg@8: import cz.frantovo.copyImageResizer.logging.ColorfulPrintWriter.TerminalColor; franta-hg@8: import cz.frantovo.copyImageResizer.logging.ColorfulPrintWriter.TerminalStyle; franta-hg@8: import java.io.StringWriter; franta-hg@8: import java.util.logging.Level; franta-hg@8: import java.util.logging.LogRecord; franta-hg@8: franta-hg@8: /** franta-hg@8: * For console/terminal log output. Log messages are printed in brief and colorful form. franta-hg@8: * franta-hg@8: * @author Ing. František Kučera (frantovo.cz) franta-hg@8: */ franta-hg@16: public class ColorfulConsoleFormatter extends AbstractFormatter { franta-hg@8: franta-hg@8: @Override franta-hg@8: public String format(LogRecord r) { franta-hg@8: StringWriter sw = new StringWriter(); franta-hg@8: try (ColorfulPrintWriter out = new ColorfulPrintWriter(sw)) { franta-hg@8: printLevel(out, r.getLevel()); franta-hg@8: printMessage(out, r); franta-hg@8: printThrowable(out, r); franta-hg@8: out.println(); franta-hg@8: } franta-hg@8: return sw.toString(); franta-hg@8: } franta-hg@8: franta-hg@8: private void printLevel(ColorfulPrintWriter out, Level l) { franta-hg@8: TerminalColor color = TerminalColor.Magenta; franta-hg@8: franta-hg@8: if (l == Level.SEVERE) { franta-hg@8: color = TerminalColor.Red; franta-hg@8: } else if (l == Level.WARNING) { franta-hg@8: color = TerminalColor.Yellow; franta-hg@8: } franta-hg@8: franta-hg@8: out.print(color, rpad(l.getLocalizedName() + ": ", 10)); franta-hg@8: } franta-hg@8: franta-hg@8: private void printMessage(ColorfulPrintWriter out, LogRecord r) { franta-hg@8: out.print(formatMessage(r)); franta-hg@8: } franta-hg@8: franta-hg@8: private void printThrowable(ColorfulPrintWriter out, LogRecord r) { franta-hg@8: Throwable t = r.getThrown(); franta-hg@8: if (t != null) { franta-hg@8: out.print(": "); franta-hg@8: out.print(TerminalColor.Red, t.getClass().getSimpleName()); franta-hg@8: String message = t.getLocalizedMessage(); franta-hg@8: if (message != null) { franta-hg@8: out.print(": "); franta-hg@8: out.print(message); franta-hg@8: } franta-hg@16: if (isPrintStacktrace()) { franta-hg@8: out.println(); franta-hg@8: out.setForegroundColor(TerminalColor.Yellow); franta-hg@8: out.setStyle(TerminalStyle.Dim); franta-hg@8: t.printStackTrace(out); franta-hg@8: out.resetAll(); franta-hg@8: } franta-hg@8: } franta-hg@8: } franta-hg@8: }