From ea16d7a6dc955452937831638cc0d035e270d06e Mon Sep 17 00:00:00 2001 From: Obique PSWG Date: Tue, 5 Jan 2016 17:46:37 -0600 Subject: [PATCH] Added log levels to the Log, as well a verbose level --- src/resources/server_info/Log.java | 70 +++++++++++++++++++++++++----- src/services/CoreManager.java | 3 ++ 2 files changed, 61 insertions(+), 12 deletions(-) diff --git a/src/resources/server_info/Log.java b/src/resources/server_info/Log.java index 81195ff63..a0b219267 100644 --- a/src/resources/server_info/Log.java +++ b/src/resources/server_info/Log.java @@ -41,14 +41,16 @@ import resources.control.Service; public class Log { private static final DateFormat LOG_FORMAT = new SimpleDateFormat("dd-mm-yy HH:mm:ss.SSS"); - private static final Log LOG = new Log("log.txt"); + private static final Log LOG = new Log("log.txt", LogLevel.VERBOSE); private final File file; private BufferedWriter writer; + private LogLevel level; private boolean open; - private Log(String filename) { + private Log(String filename, LogLevel level) { this.file = new File(filename); + this.level = level; open = false; } @@ -72,6 +74,14 @@ public class Log { } } + private synchronized void setLevel(LogLevel level) { + this.level = level; + } + + private synchronized LogLevel getLevel() { + return level; + } + protected static final void start() { try { LOG.open(); @@ -88,15 +98,30 @@ public class Log { } } + /** + * Sets the minimum level for logs to be reported. This is according to the + * following order: VERBOSE, DEBUG, INFO, WARNING, ERROR, then ASSERT. + * @param level the minimum log level, inclusively. Default is VERBOSE + */ + public static final void setLogLevel(LogLevel level) { + synchronized (LOG) { + LOG.setLevel(level); + } + } + /** * Logs the string to the server log file, formatted to display the log * severity, time, tag and message. - * @param level the log level of this message between INFO and ASSERT + * @param level the log level of this message between VERBOSE and ASSERT * @param tag the tag to use for the log * @param str the format string for the log * @param args the string format arguments, if specified */ public static final void log(LogLevel level, String tag, String str, Object ... args) { + synchronized (LOG) { + if (LOG.getLevel().compareTo(level) > 0) + return; + } String date; synchronized (LOG_FORMAT) { date = LOG_FORMAT.format(System.currentTimeMillis()); @@ -114,26 +139,24 @@ public class Log { /** * Logs the string to the server log file, formatted to display the log - * severity as INFO, as well as the time, tag and message. - * @param level the log level of this message between INFO and ASSERT + * severity as VERBOSE, as well as the time, tag and message. * @param tag the tag to use for the log * @param message the format string for the log * @param args the string format arguments, if specified */ - public static final void i(String tag, String message, Object ... args) { - log(LogLevel.INFO, tag, message, args); + public static final void v(String tag, String message, Object ... args) { + log(LogLevel.VERBOSE, tag, message, args); } /** * Logs the string to the server log file, formatted to display the log - * severity as INFO, as well as the time, service name and message. - * @param level the log level of this message between INFO and ASSERT + * severity as VERBOSE, as well as the time, service name and message. * @param service the service outputting this log info * @param message the format string for the log * @param args the string format arguments, if specified */ - public static final void i(Service service, String message, Object ... args) { - log(LogLevel.INFO, service.getClass().getSimpleName(), message, args); + public static final void v(Service service, String message, Object ... args) { + log(LogLevel.VERBOSE, service.getClass().getSimpleName(), message, args); } /** @@ -158,6 +181,28 @@ public class Log { log(LogLevel.DEBUG, service.getClass().getSimpleName(), message, args); } + /** + * Logs the string to the server log file, formatted to display the log + * severity as INFO, as well as the time, tag and message. + * @param tag the tag to use for the log + * @param message the format string for the log + * @param args the string format arguments, if specified + */ + public static final void i(String tag, String message, Object ... args) { + log(LogLevel.INFO, tag, message, args); + } + + /** + * Logs the string to the server log file, formatted to display the log + * severity as INFO, as well as the time, service name and message. + * @param service the service outputting this log info + * @param message the format string for the log + * @param args the string format arguments, if specified + */ + public static final void i(Service service, String message, Object ... args) { + log(LogLevel.INFO, service.getClass().getSimpleName(), message, args); + } + /** * Logs the string to the server log file, formatted to display the log * severity as WARN, as well as the time, tag and message. @@ -225,8 +270,9 @@ public class Log { } public static enum LogLevel { - INFO ('I'), + VERBOSE ('V'), DEBUG ('D'), + INFO ('I'), WARN ('W'), ERROR ('E'), ASSERT ('A'); diff --git a/src/services/CoreManager.java b/src/services/CoreManager.java index ce45a08db..96641fe00 100644 --- a/src/services/CoreManager.java +++ b/src/services/CoreManager.java @@ -45,6 +45,8 @@ import resources.control.Intent; import resources.control.Manager; import resources.control.ServerStatus; import resources.server_info.Config; +import resources.server_info.Log; +import resources.server_info.Log.LogLevel; import services.admin.OnlineInterfaceService; import services.galaxy.GalacticManager; import utilities.ThreadUtilities; @@ -63,6 +65,7 @@ public class CoreManager extends Manager { private boolean shutdownRequested; public CoreManager() { + Log.setLogLevel(LogLevel.valueOf(getConfig(ConfigFile.PRIMARY).getString("LOG-LEVEL", LogLevel.DEBUG.name()))); shutdownService = Executors.newSingleThreadScheduledExecutor(ThreadUtilities.newThreadFactory("core-shutdown-service")); shutdownRequested = false; galaxy = getGalaxy();