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@250: * the Free Software Foundation, version 3 of the License. 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@155: * For console/terminal log output. Log messages are printed in brief and colorful form. 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@59: private boolean printStacktrace = false; franta-hg@59: 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@59: printThrowable(out, r); 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: 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@59: private void printThrowable(ColorfulPrintWriter out, LogRecord r) { franta-hg@59: Throwable t = r.getThrown(); 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@182: if (printStacktrace) { franta-hg@182: out.print(message); franta-hg@182: } else { franta-hg@182: out.print(message.replaceAll("\\n", " ")); franta-hg@182: } franta-hg@55: } franta-hg@59: if (printStacktrace) { franta-hg@59: out.println(); franta-hg@59: out.setForegroundColor(TerminalColor.Yellow); franta-hg@59: out.setStyle(TerminalStyle.Dim); franta-hg@59: t.printStackTrace(out); franta-hg@59: out.resetAll(); franta-hg@59: } franta-hg@55: } franta-hg@55: } franta-hg@59: franta-hg@59: public boolean isPrintStacktrace() { franta-hg@59: return printStacktrace; franta-hg@59: } franta-hg@59: franta-hg@59: public void setPrintStacktrace(boolean printStacktrace) { franta-hg@59: this.printStacktrace = printStacktrace; franta-hg@59: } franta-hg@55: }