java/sql-dk/src/main/java/info/globalcode/sql/dk/logging/ColorfulConsoleFormatter.java
author František Kučera <franta-hg@frantovo.cz>
Thu, 24 Oct 2019 21:43:08 +0200
branchv_0
changeset 250 aae5009bd0af
parent 238 4a1864c3e867
permissions -rw-r--r--
fix license version: GNU GPLv3

/**
 * SQL-DK
 * Copyright © 2013 František Kučera (frantovo.cz)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package info.globalcode.sql.dk.logging;

import info.globalcode.sql.dk.ColorfulPrintWriter;
import static info.globalcode.sql.dk.ColorfulPrintWriter.TerminalColor;
import static info.globalcode.sql.dk.ColorfulPrintWriter.TerminalStyle;
import static info.globalcode.sql.dk.Functions.rpad;
import java.io.StringWriter;
import java.util.logging.Formatter;
import java.util.logging.Level;
import java.util.logging.LogRecord;

/**
 * For console/terminal log output. Log messages are printed in brief and colorful form.
 *
 * @author Ing. František Kučera (frantovo.cz)
 */
public class ColorfulConsoleFormatter extends Formatter {

	private boolean printStacktrace = false;

	@Override
	public String format(LogRecord r) {
		StringWriter sw = new StringWriter();
		try (ColorfulPrintWriter out = new ColorfulPrintWriter(sw)) {
			printLevel(out, r.getLevel());
			printMessage(out, r);
			printThrowable(out, r);
			out.println();
		}
		return sw.toString();
	}

	private void printLevel(ColorfulPrintWriter out, Level l) {
		TerminalColor color = TerminalColor.Magenta;

		if (l == Level.SEVERE) {
			color = TerminalColor.Red;
		} else if (l == Level.WARNING) {
			color = TerminalColor.Yellow;
		}

		out.print(color, rpad(l.getLocalizedName() + ": ", 10));
	}

	private void printMessage(ColorfulPrintWriter out, LogRecord r) {
		out.print(formatMessage(r));
	}

	private void printThrowable(ColorfulPrintWriter out, LogRecord r) {
		Throwable t = r.getThrown();
		if (t != null) {
			out.print(": ");
			out.print(TerminalColor.Red, t.getClass().getSimpleName());
			String message = t.getLocalizedMessage();
			if (message != null) {
				out.print(": ");
				if (printStacktrace) {
					out.print(message);
				} else {
					out.print(message.replaceAll("\\n", " "));
				}
			}
			if (printStacktrace) {
				out.println();
				out.setForegroundColor(TerminalColor.Yellow);
				out.setStyle(TerminalStyle.Dim);
				t.printStackTrace(out);
				out.resetAll();
			}
		}
	}

	public boolean isPrintStacktrace() {
		return printStacktrace;
	}

	public void setPrintStacktrace(boolean printStacktrace) {
		this.printStacktrace = printStacktrace;
	}
}