java/sql-dk/src/info/globalcode/sql/dk/logging/ColorfulConsoleFormatter.java
author František Kučera <franta-hg@frantovo.cz>
Sat, 28 Dec 2013 16:45:04 +0100
branchv_0
changeset 89 98d18e9a357b
parent 59 5f745ae795a8
child 155 eb3676c6929b
permissions -rw-r--r--
InfoLister (configuration listings) will use TabularPrefetchingFormatter as default
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@59
    35
	private boolean printStacktrace = false;
franta-hg@59
    36
franta-hg@55
    37
	@Override
franta-hg@55
    38
	public String format(LogRecord r) {
franta-hg@55
    39
		StringWriter sw = new StringWriter();
franta-hg@55
    40
		try (ColorfulPrintWriter out = new ColorfulPrintWriter(sw)) {
franta-hg@55
    41
			printLevel(out, r.getLevel());
franta-hg@55
    42
			printMessage(out, r);
franta-hg@59
    43
			printThrowable(out, r);
franta-hg@55
    44
			out.println();
franta-hg@55
    45
		}
franta-hg@55
    46
		return sw.toString();
franta-hg@55
    47
	}
franta-hg@55
    48
franta-hg@55
    49
	private void printLevel(ColorfulPrintWriter out, Level l) {
franta-hg@55
    50
		TerminalColor color = TerminalColor.Magenta;
franta-hg@55
    51
franta-hg@55
    52
		if (l == Level.SEVERE) {
franta-hg@55
    53
			color = TerminalColor.Red;
franta-hg@55
    54
		} else if (l == Level.WARNING) {
franta-hg@55
    55
			color = TerminalColor.Yellow;
franta-hg@55
    56
		}
franta-hg@55
    57
franta-hg@55
    58
		out.print(color, rpad(l.getLocalizedName() + ": ", 10));
franta-hg@55
    59
	}
franta-hg@55
    60
franta-hg@55
    61
	private void printMessage(ColorfulPrintWriter out, LogRecord r) {
franta-hg@55
    62
		out.print(formatMessage(r));
franta-hg@55
    63
	}
franta-hg@55
    64
franta-hg@59
    65
	private void printThrowable(ColorfulPrintWriter out, LogRecord r) {
franta-hg@59
    66
		Throwable t = r.getThrown();
franta-hg@55
    67
		if (t != null) {
franta-hg@55
    68
			out.print(": ");
franta-hg@55
    69
			out.print(TerminalColor.Red, t.getClass().getSimpleName());
franta-hg@55
    70
			String message = t.getLocalizedMessage();
franta-hg@55
    71
			if (message != null) {
franta-hg@55
    72
				out.print(": ");
franta-hg@55
    73
				out.print(message);
franta-hg@55
    74
			}
franta-hg@59
    75
			if (printStacktrace) {
franta-hg@59
    76
				out.println();
franta-hg@59
    77
				out.setForegroundColor(TerminalColor.Yellow);
franta-hg@59
    78
				out.setStyle(TerminalStyle.Dim);
franta-hg@59
    79
				t.printStackTrace(out);
franta-hg@59
    80
				out.resetAll();
franta-hg@59
    81
			}
franta-hg@55
    82
		}
franta-hg@55
    83
	}
franta-hg@59
    84
franta-hg@59
    85
	public boolean isPrintStacktrace() {
franta-hg@59
    86
		return printStacktrace;
franta-hg@59
    87
	}
franta-hg@59
    88
franta-hg@59
    89
	public void setPrintStacktrace(boolean printStacktrace) {
franta-hg@59
    90
		this.printStacktrace = printStacktrace;
franta-hg@59
    91
	}
franta-hg@55
    92
}