java/sql-dk/src/info/globalcode/sql/dk/logging/ColorfulConsoleFormatter.java
branchv_0
changeset 55 f5ed7c4efacc
child 59 5f745ae795a8
equal deleted inserted replaced
54:53020d0bd2e4 55:f5ed7c4efacc
       
     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
       
     7  * the Free Software Foundation, either version 3 of the License, or
       
     8  * (at your option) any later version.
       
     9  *
       
    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.
       
    14  *
       
    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/>.
       
    17  */
       
    18 package info.globalcode.sql.dk.logging;
       
    19 
       
    20 import info.globalcode.sql.dk.ColorfulPrintWriter;
       
    21 import static info.globalcode.sql.dk.ColorfulPrintWriter.TerminalColor;
       
    22 import static info.globalcode.sql.dk.ColorfulPrintWriter.TerminalStyle;
       
    23 import static info.globalcode.sql.dk.Functions.rpad;
       
    24 import java.io.StringWriter;
       
    25 import java.util.logging.Formatter;
       
    26 import java.util.logging.Level;
       
    27 import java.util.logging.LogRecord;
       
    28 
       
    29 /**
       
    30  *
       
    31  * @author Ing. František Kučera (frantovo.cz)
       
    32  */
       
    33 public class ColorfulConsoleFormatter extends Formatter {
       
    34 
       
    35 	@Override
       
    36 	public String format(LogRecord r) {
       
    37 		StringWriter sw = new StringWriter();
       
    38 		try (ColorfulPrintWriter out = new ColorfulPrintWriter(sw)) {
       
    39 			printLevel(out, r.getLevel());
       
    40 			printMessage(out, r);
       
    41 			printThrowable(out, r.getThrown());
       
    42 			out.println();
       
    43 		}
       
    44 		return sw.toString();
       
    45 	}
       
    46 
       
    47 	private void printLevel(ColorfulPrintWriter out, Level l) {
       
    48 		TerminalColor color = TerminalColor.Magenta;
       
    49 		TerminalStyle style;
       
    50 
       
    51 		if (l == Level.SEVERE) {
       
    52 			color = TerminalColor.Red;
       
    53 		} else if (l == Level.WARNING) {
       
    54 			color = TerminalColor.Yellow;
       
    55 		}
       
    56 
       
    57 		out.print(color, rpad(l.getLocalizedName() + ": ", 10));
       
    58 	}
       
    59 
       
    60 	private void printMessage(ColorfulPrintWriter out, LogRecord r) {
       
    61 		out.print(formatMessage(r));
       
    62 	}
       
    63 
       
    64 	private void printThrowable(ColorfulPrintWriter out, Throwable t) {
       
    65 		if (t != null) {
       
    66 			out.print(": ");
       
    67 			out.print(TerminalColor.Red, t.getClass().getSimpleName());
       
    68 			String message = t.getLocalizedMessage();
       
    69 			if (message != null) {
       
    70 				out.print(": ");
       
    71 				out.print(message);
       
    72 			}
       
    73 		}
       
    74 	}
       
    75 }