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