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