java/copy-image-resizer/src/cz/frantovo/copyImageResizer/logging/ColorfulConsoleFormatter.java
author František Kučera <franta-hg@frantovo.cz>
Mon, 17 Nov 2014 18:12:27 +0100
changeset 8 4819598eb623
child 16 4634176c6602
permissions -rw-r--r--
custom console colorful logger
     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.Formatter;
    24 import java.util.logging.Level;
    25 import java.util.logging.LogRecord;
    26 
    27 /**
    28  * For console/terminal log output. Log messages are printed in brief and colorful form.
    29  *
    30  * @author Ing. František Kučera (frantovo.cz)
    31  */
    32 public class ColorfulConsoleFormatter extends Formatter {
    33 
    34 	private boolean printStacktrace = false;
    35 
    36 	@Override
    37 	public String format(LogRecord r) {
    38 		StringWriter sw = new StringWriter();
    39 		try (ColorfulPrintWriter out = new ColorfulPrintWriter(sw)) {
    40 			printLevel(out, r.getLevel());
    41 			printMessage(out, r);
    42 			printThrowable(out, r);
    43 			out.println();
    44 		}
    45 		return sw.toString();
    46 	}
    47 
    48 	private void printLevel(ColorfulPrintWriter out, Level l) {
    49 		TerminalColor color = TerminalColor.Magenta;
    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, LogRecord r) {
    65 		Throwable t = r.getThrown();
    66 		if (t != null) {
    67 			out.print(": ");
    68 			out.print(TerminalColor.Red, t.getClass().getSimpleName());
    69 			String message = t.getLocalizedMessage();
    70 			if (message != null) {
    71 				out.print(": ");
    72 				out.print(message);
    73 			}
    74 			if (printStacktrace) {
    75 				out.println();
    76 				out.setForegroundColor(TerminalColor.Yellow);
    77 				out.setStyle(TerminalStyle.Dim);
    78 				t.printStackTrace(out);
    79 				out.resetAll();
    80 			}
    81 		}
    82 	}
    83 
    84 	public boolean isPrintStacktrace() {
    85 		return printStacktrace;
    86 	}
    87 
    88 	public void setPrintStacktrace(boolean printStacktrace) {
    89 		this.printStacktrace = printStacktrace;
    90 	}
    91 
    92 	private static String rpad(String s, int n) {
    93 		if (n > 0) {
    94 			return String.format("%1$-" + n + "s", s);
    95 		} else {
    96 			return s;
    97 		}
    98 	}
    99 
   100 }