Merge pull request #415 from Qveshn/feature/customlog

Customizable command log format (Date+Time are now available)
This commit is contained in:
Matthew Miller 2018-05-27 16:50:26 +10:00 committed by GitHub
commit 9f86e71d47
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 67 additions and 27 deletions

View File

@ -40,6 +40,17 @@ use-inventory:
logging: logging:
log-commands: false log-commands: false
file: worldedit.log file: worldedit.log
# The format of custom log message. This is java general format string (java.util.Formatter). Arguments are:
# 1$ : date - a Date object representing event time of the log record.
# 2$ : source - a string representing the caller, if available; otherwise, the logger's name.
# 3$ : logger - the logger's name.
# 4$ : level - the log level.
# 5$ : message - the formatted log message returned from the Formatter.formatMessage(LogRecord) method. It uses java.text formatting and does not use the java.util.Formatter format argument.
# 6$ : thrown - a string representing the throwable associated with the log record and its backtrace beginning with a newline character, if any; otherwise, an empty string.
# For details see:
# https://docs.oracle.com/javase/8/docs/api/java/util/Formatter.html
# https://docs.oracle.com/javase/8/docs/api/java/util/logging/SimpleFormatter.html#format-java.util.logging.LogRecord-
format: "[%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS %4$s]: %5$s%6$s%n"
super-pickaxe: super-pickaxe:
drop-items: true drop-items: true

View File

@ -21,6 +21,7 @@ package com.sk89q.worldedit;
import com.sk89q.worldedit.blocks.BlockID; import com.sk89q.worldedit.blocks.BlockID;
import com.sk89q.worldedit.blocks.ItemID; import com.sk89q.worldedit.blocks.ItemID;
import com.sk89q.worldedit.util.logging.LogFormat;
import com.sk89q.worldedit.world.snapshot.SnapshotRepository; import com.sk89q.worldedit.world.snapshot.SnapshotRepository;
import java.io.File; import java.io.File;
@ -92,6 +93,7 @@ public abstract class LocalConfiguration {
public int maxBrushRadius = 6; public int maxBrushRadius = 6;
public boolean logCommands = false; public boolean logCommands = false;
public String logFile = ""; public String logFile = "";
public String logFormat = LogFormat.DEFAULT_FORMAT;
public boolean registerHelp = true; // what is the point of this, it's not even used public boolean registerHelp = true; // what is the point of this, it's not even used
public int wandItem = ItemID.WOOD_AXE; public int wandItem = ItemID.WOOD_AXE;
public boolean superPickaxeDrop = true; public boolean superPickaxeDrop = true;

View File

@ -96,7 +96,6 @@ public final class CommandManager {
// Setup the logger // Setup the logger
commandLog.addHandler(dynamicHandler); commandLog.addHandler(dynamicHandler);
dynamicHandler.setFormatter(new LogFormat());
// Set up the commands manager // Set up the commands manager
ParametricBuilder builder = new ParametricBuilder(); ParametricBuilder builder = new ParametricBuilder();
@ -185,6 +184,8 @@ public final class CommandManager {
} catch (IOException e) { } catch (IOException e) {
log.log(Level.WARNING, "Could not use command log file " + path + ": " + e.getMessage()); log.log(Level.WARNING, "Could not use command log file " + path + ": " + e.getMessage());
} }
dynamicHandler.setFormatter(new LogFormat(config.logFormat));
} }
platform.registerCommands(dispatcher); platform.registerCommands(dispatcher);

View File

@ -95,6 +95,7 @@ public class PropertiesConfiguration extends LocalConfiguration {
maxBrushRadius = getInt("max-brush-radius", maxBrushRadius); maxBrushRadius = getInt("max-brush-radius", maxBrushRadius);
logCommands = getBool("log-commands", logCommands); logCommands = getBool("log-commands", logCommands);
logFile = getString("log-file", logFile); logFile = getString("log-file", logFile);
logFormat = getString("log-format", logFormat);
registerHelp = getBool("register-help", registerHelp); registerHelp = getBool("register-help", registerHelp);
wandItem = getInt("wand-item", wandItem); wandItem = getInt("wand-item", wandItem);
superPickaxeDrop = getBool("super-pickaxe-drop-items", superPickaxeDrop); superPickaxeDrop = getBool("super-pickaxe-drop-items", superPickaxeDrop);

View File

@ -82,6 +82,7 @@ public class YAMLConfiguration extends LocalConfiguration {
registerHelp = config.getBoolean("register-help", true); registerHelp = config.getBoolean("register-help", true);
logCommands = config.getBoolean("logging.log-commands", logCommands); logCommands = config.getBoolean("logging.log-commands", logCommands);
logFile = config.getString("logging.file", logFile); logFile = config.getString("logging.file", logFile);
logFormat = config.getString("logging.format", logFormat);
superPickaxeDrop = config.getBoolean("super-pickaxe.drop-items", superPickaxeDrop = config.getBoolean("super-pickaxe.drop-items",
superPickaxeDrop); superPickaxeDrop);

View File

@ -19,6 +19,7 @@
package com.sk89q.worldedit.util.logging; package com.sk89q.worldedit.util.logging;
import java.util.Date;
import java.util.logging.Formatter; import java.util.logging.Formatter;
import java.util.logging.LogRecord; import java.util.logging.LogRecord;
import java.util.logging.Level; import java.util.logging.Level;
@ -29,35 +30,56 @@ import java.io.StringWriter;
* A standard logging format for WorldEdit. * A standard logging format for WorldEdit.
*/ */
public class LogFormat extends Formatter { public class LogFormat extends Formatter {
public static final String DEFAULT_FORMAT = "[%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS %4$s]: %5$s%6$s%n";
private final Date dat = new Date();
private final String format;
public LogFormat() {
this(null);
}
public LogFormat(String format) {
System.out.println(format);
if (format == null || format.isEmpty()) {
format = DEFAULT_FORMAT;
}
try {
//noinspection ResultOfMethodCallIgnored
String.format(format, new Date(), "", "", "", "", "");
} catch (IllegalArgumentException var3) {
format = DEFAULT_FORMAT;
}
this.format = format;
}
@Override @Override
public String format(LogRecord record) { public String format(LogRecord record) {
StringBuilder text = new StringBuilder(); dat.setTime(record.getMillis());
Level level = record.getLevel(); String source;
if (record.getSourceClassName() != null) {
if (level == Level.FINEST) { source = record.getSourceClassName();
text.append("[FINEST] "); if (record.getSourceMethodName() != null) {
} else if (level == Level.FINER) { source += " " + record.getSourceMethodName();
text.append("[FINER] "); }
} else if (level == Level.FINE) { } else {
text.append("[FINE] "); source = record.getLoggerName();
} else if (level == Level.INFO) {
text.append("[INFO] ");
} else if (level == Level.WARNING) {
text.append("[WARNING] ");
} else if (level == Level.SEVERE) {
text.append("[SEVERE] ");
} }
String message = formatMessage(record);
text.append(record.getMessage()); String throwable = "";
text.append("\r\n"); if (record.getThrown() != null) {
StringWriter sw = new StringWriter();
Throwable t = record.getThrown(); PrintWriter pw = new PrintWriter(sw);
if (t != null) { pw.println();
StringWriter writer = new StringWriter(); record.getThrown().printStackTrace(pw);
t.printStackTrace(new PrintWriter(writer)); pw.close();
text.append(writer); throwable = sw.toString();
} }
return String.format(format,
return text.toString(); dat,
source,
record.getLoggerName(),
record.getLevel().getName(),
message,
throwable);
} }
} }

View File

@ -18,6 +18,7 @@ scripting-timeout=3000
snapshots-dir= snapshots-dir=
use-inventory-creative-override=false use-inventory-creative-override=false
log-file=worldedit.log log-file=worldedit.log
log-format=[%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS %4$s]: %5$s%6$s%n
max-changed-blocks=-1 max-changed-blocks=-1
nav-wand-distance=50 nav-wand-distance=50
butcher-default-radius=-1 butcher-default-radius=-1

View File

@ -86,6 +86,7 @@ public class ConfigurateConfiguration extends LocalConfiguration {
registerHelp = node.getNode("register-help").getBoolean(true); registerHelp = node.getNode("register-help").getBoolean(true);
logCommands = node.getNode("logging", "log-commands").getBoolean(logCommands); logCommands = node.getNode("logging", "log-commands").getBoolean(logCommands);
logFile = node.getNode("logging", "file").getString(logFile); logFile = node.getNode("logging", "file").getString(logFile);
logFormat = node.getNode("logging", "format").getString(logFormat);
superPickaxeDrop = node.getNode("super-pickaxe", "drop-items").getBoolean(superPickaxeDrop); superPickaxeDrop = node.getNode("super-pickaxe", "drop-items").getBoolean(superPickaxeDrop);
superPickaxeManyDrop = node.getNode("super-pickaxe", "many-drop-items").getBoolean(superPickaxeManyDrop); superPickaxeManyDrop = node.getNode("super-pickaxe", "many-drop-items").getBoolean(superPickaxeManyDrop);