java/copy-image-resizer/src/cz/frantovo/copyImageResizer/logging/SimpleFormatter.java
changeset 16 4634176c6602
parent 8 4819598eb623
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/java/copy-image-resizer/src/cz/frantovo/copyImageResizer/logging/SimpleFormatter.java	Mon Nov 17 21:58:52 2014 +0100
     1.3 @@ -0,0 +1,103 @@
     1.4 +/**
     1.5 + * copy-image-resizer
     1.6 + * Copyright © 2013 František Kučera (frantovo.cz)
     1.7 + *
     1.8 + * This program is free software: you can redistribute it and/or modify
     1.9 + * it under the terms of the GNU General Public License as published by
    1.10 + * the Free Software Foundation, either version 3 of the License, or
    1.11 + * (at your option) any later version.
    1.12 + *
    1.13 + * This program is distributed in the hope that it will be useful,
    1.14 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    1.16 + * GNU General Public License for more details.
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License
    1.19 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
    1.20 + */
    1.21 +package cz.frantovo.copyImageResizer.logging;
    1.22 +
    1.23 +import java.io.PrintWriter;
    1.24 +import java.io.StringWriter;
    1.25 +import java.text.SimpleDateFormat;
    1.26 +import java.util.Date;
    1.27 +import java.util.logging.Level;
    1.28 +import java.util.logging.LogRecord;
    1.29 +
    1.30 +/**
    1.31 + * For console/terminal log output. Log messages are printed in brief and colorful form.
    1.32 + *
    1.33 + * @author Ing. František Kučera (frantovo.cz)
    1.34 + */
    1.35 +public class SimpleFormatter extends AbstractFormatter {
    1.36 +
    1.37 +	private static final ThreadLocal<SimpleDateFormat> dateFormatter = new ThreadLocal<SimpleDateFormat>() {
    1.38 +
    1.39 +		@Override
    1.40 +		protected SimpleDateFormat initialValue() {
    1.41 +			return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    1.42 +		}
    1.43 +
    1.44 +	};
    1.45 +
    1.46 +	@Override
    1.47 +	public String format(LogRecord r) {
    1.48 +		StringBuilder sb = new StringBuilder();
    1.49 +
    1.50 +		printTimestamp(sb, new Date(r.getMillis()));
    1.51 +		printSeparator(sb);
    1.52 +		printLevel(sb, r.getLevel());
    1.53 +		printSeparator(sb);
    1.54 +		printMessage(sb, r);
    1.55 +		printSeparator(sb);
    1.56 +		printThrowable(sb, r);
    1.57 +		sb.append("\n");
    1.58 +
    1.59 +		return sb.toString();
    1.60 +	}
    1.61 +
    1.62 +	private void printSeparator(StringBuilder out) {
    1.63 +		out.append(" | ");
    1.64 +	}
    1.65 +
    1.66 +	private void printTimestamp(StringBuilder out, Date date) {
    1.67 +		out.append(dateFormatter.get().format(date));
    1.68 +	}
    1.69 +
    1.70 +	private void printLevel(StringBuilder out, Level l) {
    1.71 +
    1.72 +		if (l == Level.SEVERE) {
    1.73 +			out.append(rpad("ERROR", 10));
    1.74 +		} else if (l == Level.INFO) {
    1.75 +			out.append(rpad("OK", 10));
    1.76 +		} else {
    1.77 +			out.append(rpad(l.getName(), 10));
    1.78 +		}
    1.79 +	}
    1.80 +
    1.81 +	private void printMessage(StringBuilder out, LogRecord r) {
    1.82 +		String message = formatMessage(r);
    1.83 +		// convert to single-line message
    1.84 +		message = message.replaceAll("\\\\", "\\\\"); // escape backslashes
    1.85 +		message = message.replaceAll("\\n", "\\n"); // escape line ends
    1.86 +		out.append(message);
    1.87 +	}
    1.88 +
    1.89 +	private void printThrowable(StringBuilder out, LogRecord r) {
    1.90 +		Throwable t = r.getThrown();
    1.91 +		if (t != null) {
    1.92 +			out.append(t.getClass().getSimpleName());
    1.93 +			String message = t.getLocalizedMessage();
    1.94 +			if (message != null) {
    1.95 +				out.append(": ");
    1.96 +				out.append(message);
    1.97 +			}
    1.98 +			if (isPrintStacktrace()) {
    1.99 +				out.append("\n");
   1.100 +				StringWriter sw = new StringWriter();
   1.101 +				t.printStackTrace(new PrintWriter(sw));
   1.102 +				out.append(sw.toString());
   1.103 +			}
   1.104 +		}
   1.105 +	}
   1.106 +}