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
     1 /**
     2  * copy-image-resizer
     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 cz.frantovo.copyImageResizer.logging;
    19 
    20 import cz.frantovo.copyImageResizer.logging.ColorfulPrintWriter.TerminalColor;
    21 import cz.frantovo.copyImageResizer.logging.ColorfulPrintWriter.TerminalStyle;
    22 import java.io.StringWriter;
    23 import java.util.logging.Level;
    24 import java.util.logging.LogRecord;
    25 
    26 /**
    27  * For console/terminal log output. Log messages are printed in brief and colorful form.
    28  *
    29  * @author Ing. František Kučera (frantovo.cz)
    30  */
    31 public class ColorfulConsoleFormatter extends AbstractFormatter {
    32 
    33 	@Override
    34 	public String format(LogRecord r) {
    35 		StringWriter sw = new StringWriter();
    36 		try (ColorfulPrintWriter out = new ColorfulPrintWriter(sw)) {
    37 			printLevel(out, r.getLevel());
    38 			printMessage(out, r);
    39 			printThrowable(out, r);
    40 			out.println();
    41 		}
    42 		return sw.toString();
    43 	}
    44 
    45 	private void printLevel(ColorfulPrintWriter out, Level l) {
    46 		TerminalColor color = TerminalColor.Magenta;
    47 
    48 		if (l == Level.SEVERE) {
    49 			color = TerminalColor.Red;
    50 		} else if (l == Level.WARNING) {
    51 			color = TerminalColor.Yellow;
    52 		}
    53 
    54 		out.print(color, rpad(l.getLocalizedName() + ": ", 10));
    55 	}
    56 
    57 	private void printMessage(ColorfulPrintWriter out, LogRecord r) {
    58 		out.print(formatMessage(r));
    59 	}
    60 
    61 	private void printThrowable(ColorfulPrintWriter out, LogRecord r) {
    62 		Throwable t = r.getThrown();
    63 		if (t != null) {
    64 			out.print(": ");
    65 			out.print(TerminalColor.Red, t.getClass().getSimpleName());
    66 			String message = t.getLocalizedMessage();
    67 			if (message != null) {
    68 				out.print(": ");
    69 				out.print(message);
    70 			}
    71 			if (isPrintStacktrace()) {
    72 				out.println();
    73 				out.setForegroundColor(TerminalColor.Yellow);
    74 				out.setStyle(TerminalStyle.Dim);
    75 				t.printStackTrace(out);
    76 				out.resetAll();
    77 			}
    78 		}
    79 	}
    80 }