From 66eaab7c4d1b6b946f879639b5b0e55b408d02a5 Mon Sep 17 00:00:00 2001 From: Phoenix616 Date: Mon, 4 Jan 2021 22:42:17 +0100 Subject: [PATCH] Include log level (if not INFO) and error stack trace in log file --- .../ChestShop/Logging/FileFormatter.java | 22 ++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/Acrobot/ChestShop/Logging/FileFormatter.java b/src/main/java/com/Acrobot/ChestShop/Logging/FileFormatter.java index a9e24a6..a372cf4 100644 --- a/src/main/java/com/Acrobot/ChestShop/Logging/FileFormatter.java +++ b/src/main/java/com/Acrobot/ChestShop/Logging/FileFormatter.java @@ -1,9 +1,12 @@ package com.Acrobot.ChestShop.Logging; +import java.io.PrintWriter; +import java.io.StringWriter; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; import java.util.logging.Formatter; +import java.util.logging.Level; import java.util.logging.LogRecord; /** @@ -14,7 +17,24 @@ public class FileFormatter extends Formatter { @Override public String format(LogRecord record) { - return getDateAndTime() + ' ' + record.getMessage() + '\n'; + StringBuilder message = new StringBuilder(getDateAndTime()); + + if (record.getLevel() != Level.INFO) { + message.append(' ').append(record.getLevel().getLocalizedName()); + } + + message.append(' ').append(record.getMessage()); + + if (record.getThrown() != null) { + StringWriter sw = new StringWriter(); + PrintWriter pw = new PrintWriter(sw); + pw.println(); + record.getThrown().printStackTrace(pw); + pw.close(); + message.append(sw.toString()); + } + + return message.append('\n').toString(); } private String getDateAndTime() {