java/sql-dk/src/main/java/info/globalcode/sql/dk/logging/ColorfulConsoleFormatter.java
author František Kučera <franta-hg@frantovo.cz>
Mon, 04 Mar 2019 20:15:24 +0100
branchv_0
changeset 238 4a1864c3e867
parent 182 java/sql-dk/src/info/globalcode/sql/dk/logging/ColorfulConsoleFormatter.java@4ff7a273f3e8
child 250 aae5009bd0af
permissions -rw-r--r--
mavenized: sql-dk
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
55
f5ed7c4efacc colorful logging
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     1
/**
f5ed7c4efacc colorful logging
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     2
 * SQL-DK
f5ed7c4efacc colorful logging
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     3
 * Copyright © 2013 František Kučera (frantovo.cz)
f5ed7c4efacc colorful logging
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     4
 *
f5ed7c4efacc colorful logging
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     5
 * This program is free software: you can redistribute it and/or modify
f5ed7c4efacc colorful logging
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
f5ed7c4efacc colorful logging
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     7
 * the Free Software Foundation, either version 3 of the License, or
f5ed7c4efacc colorful logging
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     8
 * (at your option) any later version.
f5ed7c4efacc colorful logging
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     9
 *
f5ed7c4efacc colorful logging
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    10
 * This program is distributed in the hope that it will be useful,
f5ed7c4efacc colorful logging
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
f5ed7c4efacc colorful logging
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
f5ed7c4efacc colorful logging
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    13
 * GNU General Public License for more details.
f5ed7c4efacc colorful logging
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    14
 *
f5ed7c4efacc colorful logging
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License
f5ed7c4efacc colorful logging
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    16
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
f5ed7c4efacc colorful logging
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    17
 */
f5ed7c4efacc colorful logging
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    18
package info.globalcode.sql.dk.logging;
f5ed7c4efacc colorful logging
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    19
f5ed7c4efacc colorful logging
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    20
import info.globalcode.sql.dk.ColorfulPrintWriter;
f5ed7c4efacc colorful logging
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    21
import static info.globalcode.sql.dk.ColorfulPrintWriter.TerminalColor;
f5ed7c4efacc colorful logging
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    22
import static info.globalcode.sql.dk.ColorfulPrintWriter.TerminalStyle;
f5ed7c4efacc colorful logging
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    23
import static info.globalcode.sql.dk.Functions.rpad;
f5ed7c4efacc colorful logging
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    24
import java.io.StringWriter;
f5ed7c4efacc colorful logging
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    25
import java.util.logging.Formatter;
f5ed7c4efacc colorful logging
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    26
import java.util.logging.Level;
f5ed7c4efacc colorful logging
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    27
import java.util.logging.LogRecord;
f5ed7c4efacc colorful logging
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    28
f5ed7c4efacc colorful logging
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    29
/**
155
eb3676c6929b more JavaDoc
František Kučera <franta-hg@frantovo.cz>
parents: 59
diff changeset
    30
 * For console/terminal log output. Log messages are printed in brief and colorful form.
55
f5ed7c4efacc colorful logging
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    31
 *
f5ed7c4efacc colorful logging
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
 * @author Ing. František Kučera (frantovo.cz)
f5ed7c4efacc colorful logging
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    33
 */
f5ed7c4efacc colorful logging
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    34
public class ColorfulConsoleFormatter extends Formatter {
f5ed7c4efacc colorful logging
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    35
59
5f745ae795a8 logging: print stacktraces if level is less than INFO
František Kučera <franta-hg@frantovo.cz>
parents: 55
diff changeset
    36
	private boolean printStacktrace = false;
5f745ae795a8 logging: print stacktraces if level is less than INFO
František Kučera <franta-hg@frantovo.cz>
parents: 55
diff changeset
    37
55
f5ed7c4efacc colorful logging
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    38
	@Override
f5ed7c4efacc colorful logging
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    39
	public String format(LogRecord r) {
f5ed7c4efacc colorful logging
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    40
		StringWriter sw = new StringWriter();
f5ed7c4efacc colorful logging
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    41
		try (ColorfulPrintWriter out = new ColorfulPrintWriter(sw)) {
f5ed7c4efacc colorful logging
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    42
			printLevel(out, r.getLevel());
f5ed7c4efacc colorful logging
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    43
			printMessage(out, r);
59
5f745ae795a8 logging: print stacktraces if level is less than INFO
František Kučera <franta-hg@frantovo.cz>
parents: 55
diff changeset
    44
			printThrowable(out, r);
55
f5ed7c4efacc colorful logging
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    45
			out.println();
f5ed7c4efacc colorful logging
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    46
		}
f5ed7c4efacc colorful logging
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    47
		return sw.toString();
f5ed7c4efacc colorful logging
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    48
	}
f5ed7c4efacc colorful logging
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    49
f5ed7c4efacc colorful logging
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    50
	private void printLevel(ColorfulPrintWriter out, Level l) {
f5ed7c4efacc colorful logging
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    51
		TerminalColor color = TerminalColor.Magenta;
f5ed7c4efacc colorful logging
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    52
f5ed7c4efacc colorful logging
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    53
		if (l == Level.SEVERE) {
f5ed7c4efacc colorful logging
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    54
			color = TerminalColor.Red;
f5ed7c4efacc colorful logging
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    55
		} else if (l == Level.WARNING) {
f5ed7c4efacc colorful logging
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    56
			color = TerminalColor.Yellow;
f5ed7c4efacc colorful logging
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    57
		}
f5ed7c4efacc colorful logging
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    58
f5ed7c4efacc colorful logging
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    59
		out.print(color, rpad(l.getLocalizedName() + ": ", 10));
f5ed7c4efacc colorful logging
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    60
	}
f5ed7c4efacc colorful logging
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    61
f5ed7c4efacc colorful logging
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    62
	private void printMessage(ColorfulPrintWriter out, LogRecord r) {
f5ed7c4efacc colorful logging
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    63
		out.print(formatMessage(r));
f5ed7c4efacc colorful logging
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    64
	}
f5ed7c4efacc colorful logging
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    65
59
5f745ae795a8 logging: print stacktraces if level is less than INFO
František Kučera <franta-hg@frantovo.cz>
parents: 55
diff changeset
    66
	private void printThrowable(ColorfulPrintWriter out, LogRecord r) {
5f745ae795a8 logging: print stacktraces if level is less than INFO
František Kučera <franta-hg@frantovo.cz>
parents: 55
diff changeset
    67
		Throwable t = r.getThrown();
55
f5ed7c4efacc colorful logging
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    68
		if (t != null) {
f5ed7c4efacc colorful logging
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    69
			out.print(": ");
f5ed7c4efacc colorful logging
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    70
			out.print(TerminalColor.Red, t.getClass().getSimpleName());
f5ed7c4efacc colorful logging
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    71
			String message = t.getLocalizedMessage();
f5ed7c4efacc colorful logging
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    72
			if (message != null) {
f5ed7c4efacc colorful logging
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    73
				out.print(": ");
182
4ff7a273f3e8 logging: strip line ends from the error message if not printing stacktrace
František Kučera <franta-hg@frantovo.cz>
parents: 155
diff changeset
    74
				if (printStacktrace) {
4ff7a273f3e8 logging: strip line ends from the error message if not printing stacktrace
František Kučera <franta-hg@frantovo.cz>
parents: 155
diff changeset
    75
					out.print(message);
4ff7a273f3e8 logging: strip line ends from the error message if not printing stacktrace
František Kučera <franta-hg@frantovo.cz>
parents: 155
diff changeset
    76
				} else {
4ff7a273f3e8 logging: strip line ends from the error message if not printing stacktrace
František Kučera <franta-hg@frantovo.cz>
parents: 155
diff changeset
    77
					out.print(message.replaceAll("\\n", " "));
4ff7a273f3e8 logging: strip line ends from the error message if not printing stacktrace
František Kučera <franta-hg@frantovo.cz>
parents: 155
diff changeset
    78
				}
55
f5ed7c4efacc colorful logging
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    79
			}
59
5f745ae795a8 logging: print stacktraces if level is less than INFO
František Kučera <franta-hg@frantovo.cz>
parents: 55
diff changeset
    80
			if (printStacktrace) {
5f745ae795a8 logging: print stacktraces if level is less than INFO
František Kučera <franta-hg@frantovo.cz>
parents: 55
diff changeset
    81
				out.println();
5f745ae795a8 logging: print stacktraces if level is less than INFO
František Kučera <franta-hg@frantovo.cz>
parents: 55
diff changeset
    82
				out.setForegroundColor(TerminalColor.Yellow);
5f745ae795a8 logging: print stacktraces if level is less than INFO
František Kučera <franta-hg@frantovo.cz>
parents: 55
diff changeset
    83
				out.setStyle(TerminalStyle.Dim);
5f745ae795a8 logging: print stacktraces if level is less than INFO
František Kučera <franta-hg@frantovo.cz>
parents: 55
diff changeset
    84
				t.printStackTrace(out);
5f745ae795a8 logging: print stacktraces if level is less than INFO
František Kučera <franta-hg@frantovo.cz>
parents: 55
diff changeset
    85
				out.resetAll();
5f745ae795a8 logging: print stacktraces if level is less than INFO
František Kučera <franta-hg@frantovo.cz>
parents: 55
diff changeset
    86
			}
55
f5ed7c4efacc colorful logging
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    87
		}
f5ed7c4efacc colorful logging
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    88
	}
59
5f745ae795a8 logging: print stacktraces if level is less than INFO
František Kučera <franta-hg@frantovo.cz>
parents: 55
diff changeset
    89
5f745ae795a8 logging: print stacktraces if level is less than INFO
František Kučera <franta-hg@frantovo.cz>
parents: 55
diff changeset
    90
	public boolean isPrintStacktrace() {
5f745ae795a8 logging: print stacktraces if level is less than INFO
František Kučera <franta-hg@frantovo.cz>
parents: 55
diff changeset
    91
		return printStacktrace;
5f745ae795a8 logging: print stacktraces if level is less than INFO
František Kučera <franta-hg@frantovo.cz>
parents: 55
diff changeset
    92
	}
5f745ae795a8 logging: print stacktraces if level is less than INFO
František Kučera <franta-hg@frantovo.cz>
parents: 55
diff changeset
    93
5f745ae795a8 logging: print stacktraces if level is less than INFO
František Kučera <franta-hg@frantovo.cz>
parents: 55
diff changeset
    94
	public void setPrintStacktrace(boolean printStacktrace) {
5f745ae795a8 logging: print stacktraces if level is less than INFO
František Kučera <franta-hg@frantovo.cz>
parents: 55
diff changeset
    95
		this.printStacktrace = printStacktrace;
5f745ae795a8 logging: print stacktraces if level is less than INFO
František Kučera <franta-hg@frantovo.cz>
parents: 55
diff changeset
    96
	}
55
f5ed7c4efacc colorful logging
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    97
}