java/sql-dk/src/info/globalcode/sql/dk/logging/ColorfulConsoleFormatter.java
author František Kučera <franta-hg@frantovo.cz>
Wed, 25 Dec 2013 00:43:06 +0100
branchv_0
changeset 55 f5ed7c4efacc
child 59 5f745ae795a8
permissions -rw-r--r--
colorful logging
franta-hg@55
     1
/**
franta-hg@55
     2
 * SQL-DK
franta-hg@55
     3
 * Copyright © 2013 František Kučera (frantovo.cz)
franta-hg@55
     4
 *
franta-hg@55
     5
 * This program is free software: you can redistribute it and/or modify
franta-hg@55
     6
 * it under the terms of the GNU General Public License as published by
franta-hg@55
     7
 * the Free Software Foundation, either version 3 of the License, or
franta-hg@55
     8
 * (at your option) any later version.
franta-hg@55
     9
 *
franta-hg@55
    10
 * This program is distributed in the hope that it will be useful,
franta-hg@55
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
franta-hg@55
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
franta-hg@55
    13
 * GNU General Public License for more details.
franta-hg@55
    14
 *
franta-hg@55
    15
 * You should have received a copy of the GNU General Public License
franta-hg@55
    16
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
franta-hg@55
    17
 */
franta-hg@55
    18
package info.globalcode.sql.dk.logging;
franta-hg@55
    19
franta-hg@55
    20
import info.globalcode.sql.dk.ColorfulPrintWriter;
franta-hg@55
    21
import static info.globalcode.sql.dk.ColorfulPrintWriter.TerminalColor;
franta-hg@55
    22
import static info.globalcode.sql.dk.ColorfulPrintWriter.TerminalStyle;
franta-hg@55
    23
import static info.globalcode.sql.dk.Functions.rpad;
franta-hg@55
    24
import java.io.StringWriter;
franta-hg@55
    25
import java.util.logging.Formatter;
franta-hg@55
    26
import java.util.logging.Level;
franta-hg@55
    27
import java.util.logging.LogRecord;
franta-hg@55
    28
franta-hg@55
    29
/**
franta-hg@55
    30
 *
franta-hg@55
    31
 * @author Ing. František Kučera (frantovo.cz)
franta-hg@55
    32
 */
franta-hg@55
    33
public class ColorfulConsoleFormatter extends Formatter {
franta-hg@55
    34
franta-hg@55
    35
	@Override
franta-hg@55
    36
	public String format(LogRecord r) {
franta-hg@55
    37
		StringWriter sw = new StringWriter();
franta-hg@55
    38
		try (ColorfulPrintWriter out = new ColorfulPrintWriter(sw)) {
franta-hg@55
    39
			printLevel(out, r.getLevel());
franta-hg@55
    40
			printMessage(out, r);
franta-hg@55
    41
			printThrowable(out, r.getThrown());
franta-hg@55
    42
			out.println();
franta-hg@55
    43
		}
franta-hg@55
    44
		return sw.toString();
franta-hg@55
    45
	}
franta-hg@55
    46
franta-hg@55
    47
	private void printLevel(ColorfulPrintWriter out, Level l) {
franta-hg@55
    48
		TerminalColor color = TerminalColor.Magenta;
franta-hg@55
    49
		TerminalStyle style;
franta-hg@55
    50
franta-hg@55
    51
		if (l == Level.SEVERE) {
franta-hg@55
    52
			color = TerminalColor.Red;
franta-hg@55
    53
		} else if (l == Level.WARNING) {
franta-hg@55
    54
			color = TerminalColor.Yellow;
franta-hg@55
    55
		}
franta-hg@55
    56
franta-hg@55
    57
		out.print(color, rpad(l.getLocalizedName() + ": ", 10));
franta-hg@55
    58
	}
franta-hg@55
    59
franta-hg@55
    60
	private void printMessage(ColorfulPrintWriter out, LogRecord r) {
franta-hg@55
    61
		out.print(formatMessage(r));
franta-hg@55
    62
	}
franta-hg@55
    63
franta-hg@55
    64
	private void printThrowable(ColorfulPrintWriter out, Throwable t) {
franta-hg@55
    65
		if (t != null) {
franta-hg@55
    66
			out.print(": ");
franta-hg@55
    67
			out.print(TerminalColor.Red, t.getClass().getSimpleName());
franta-hg@55
    68
			String message = t.getLocalizedMessage();
franta-hg@55
    69
			if (message != null) {
franta-hg@55
    70
				out.print(": ");
franta-hg@55
    71
				out.print(message);
franta-hg@55
    72
			}
franta-hg@55
    73
		}
franta-hg@55
    74
	}
franta-hg@55
    75
}