Class LoggerUtil

java.lang.Object
org.omegazero.common.logging.LoggerUtil

public final class LoggerUtil extends Object
Maintains and manages the logging state of omz-java-lib and Logger instances.
Since:
2.1
See Also:
  • Field Details

    • sysOut

      public static final PrintStream sysOut
      The initial value of System.out.
    • sysErr

      public static final PrintStream sysErr
      The initial value of System.err.
    • ENABLE_INTERNAL_DEBUG

      public static final boolean ENABLE_INTERNAL_DEBUG
      System property org.omegazero.common.logging.internalDebug

      Enables logging of internal debugging messages to stderr.

      Default: false

      Since:
      2.11.0
  • Method Details

    • init

      public static void init()
      Initializes the logger library, setting the log level to LogLevel.INFO.

      Equivalent to init(null, null).

      Throws:
      SecurityException - If a security manager is present and does not allow changing logger settings
      Since:
      2.11.0
    • init

      public static void init(LogLevel level, String file)
      Initializes the logger library, setting the maximum log level to level and the log file name, where log messages will be saved to.

      To reduce disk writes and increase logging speed, by default, log messages will only be saved every 5 minutes, if the logger buffer is full or when close() is called. This may be disabled using setSyncFlush(boolean) by setting it to true. The log buffer may be flushed explicitly using flushLogBuffer(). The save interval may be changed using the org.omegazero.common.logging.saveInterval system property by setting it to a number representing the time in seconds before this class is used.

      Parameters:
      level - The maximum log level for log messages. Log messages higher than this will be omitted. May be null, in which case it will be set to LogLevel.INFO
      file - The file name to save log messages to. May be null to disable disk saving
      Throws:
      SecurityException - If a security manager is present and does not allow changing logger settings
    • close

      public static void close()
      Saves the log buffer and resets the LoggerUtil.
      Throws:
      SecurityException - If a security manager is present and does not allow changing logger settings
    • createLogger

      public static Logger createLogger()
      Creates a new logger bound to the calling class.
      Returns:
      The new logger instance
      Throws:
      SecurityException - If a security manager is present and does not allow creating a new logger instance
    • createLogger

      protected static Logger createLogger(int off)
    • createLogger

      protected static Logger createLogger(Class<?> cl)
    • muteLogger

      public static boolean muteLogger(String fullClassName)
      Mutes the StandardLogger with the given name, causing the affected logger to no longer print log messages. If a fine log listener is configured using addFineLogListener(BiConsumer), the logger will continue to generate log messages.
      Parameters:
      fullClassName - The name of the logger, as returned by StandardLogger.getFullClassName() or Class.getName()
      Returns:
      true if the logger was not already muted
      Throws:
      SecurityException - If a security manager is present and does not allow muting the logger with the given name
      Since:
      2.5
      See Also:
    • unmuteLogger

      public static boolean unmuteLogger(String fullClassName)
      Unmutes the StandardLogger with the given name, reversing any mute operation by a previous call to muteLogger(String).
      Parameters:
      fullClassName - The name of the logger, as returned by StandardLogger.getFullClassName() or Class.getName()
      Returns:
      true if the logger was not muted
      Throws:
      SecurityException - If a security manager is present and does not allow unmuting the logger with the given name
      Since:
      2.5
      See Also:
    • isLoggerMuted

      public static boolean isLoggerMuted(String fullClassName)
      Returns whether the logger with the given name is muted due to a previous call to muteLogger(String)
      Parameters:
      fullClassName - The name of the logger, as returned by StandardLogger.getFullClassName() or Class.getName()
      Returns:
      true if the logger is muted
      Since:
      2.5
    • redirectStandardOutputStreams

      public static void redirectStandardOutputStreams()
      Redirects the System.out and System.err to Logger streams, causing messages printed using System.out.[...] or System.err.[...] to be formatted to standard logger format and printed with log level LogLevel.INFO.
      Throws:
      SecurityException - If a security manager is present and does not allow reassignment of the standard or logger output streams
    • addLogListener

      public static void addLogListener(BiConsumer<LogLevel,String> listener)
      Adds a log listener. A log listener is called every time a log message is generated on an enabled log level. To receive all log messages regardless of the configured log level, use addFineLogListener(BiConsumer) instead.

      The callback receives two arguments:

      • LogLevel - The log level on which the log message was generated.
      • String - The already formatted string, as it is printed to the output stream and the log file. Does not contain control characters for log coloring.
      Parameters:
      listener - The callback
      Throws:
      SecurityException - If a security manager is present and does not allow adding log listeners
      See Also:
    • addFineLogListener

      public static void addFineLogListener(BiConsumer<LogLevel,String> listener)
      Same as addLogListener(BiConsumer), except that listeners added here will receive all log messages, regardless of the configured log level. Note that this may slow down the application because all log messages need to be generated, instead of only ones below the configured log level.
      Parameters:
      listener - The callback
      Throws:
      SecurityException - If a security manager is present and does not allow adding log listeners
      Since:
      2.3
      See Also:
    • flushLogBuffer

      public static void flushLogBuffer()
      Calls LoggerOutput.flush() on all configured LoggerOutputs.
    • newLogMessage

      protected static void newLogMessage(String line, String markup)
    • logToListeners

      protected static void logToListeners(LogLevel logLevel, String s)
    • needAllLogMessages

      protected static boolean needAllLogMessages()
    • addLoggerOutput

      public static void addLoggerOutput(LoggerOutput output)
      Adds the given LoggerOutput to the list of configured outputs.
      Parameters:
      output - The LoggerOutput
      Since:
      2.10
    • removeLoggerOutput

      public static boolean removeLoggerOutput(LoggerOutput output)
      Removes the given LoggerOutput from the list of configured outputs.
      Parameters:
      output - The LoggerOutput
      Returns:
      true if the instance was found and removed
      Since:
      2.10
    • getLoggerOutputByType

      public static <T extends LoggerOutput> T getLoggerOutputByType(Class<T> cl)
      Returns a configured LoggerOutput with the given type or a subtype thereof. If multiple LoggerOutputs match, it is undefined which instance is returned.
      Parameters:
      cl - The LoggerOutput type to search for
      Returns:
      The LoggerOutput instance, or null if no matching instance was found
      Since:
      2.10
      See Also:
    • getLoggerOutputsByType

      public static <T extends LoggerOutput> List<T> getLoggerOutputsByType(Class<T> cl)
      Returns all configured LoggerOutputs with the given type or a subtype thereof.
      Parameters:
      cl - The LoggerOutput type to search for
      Returns:
      The LoggerOutput instances, may be empty
      Since:
      2.10
      See Also:
    • resolveLogLevel

      public static LogLevel resolveLogLevel(String str)
      Searches the log level referenced by the given String, either by log level number or log level name.
      Parameters:
      str - The string to resolve
      Returns:
      The log level represented by the given string, or null if no appropriate log level was found
    • setUseStderr

      @Deprecated public static void setUseStderr(boolean u)
      Deprecated.
      If set to true, all log messages created using a StandardLogger will be printed to the default System.err instead of the default System.out. Set the output stream where all log messages using a StandardLogger will be printed to.
      Parameters:
      u - If loggers should use stderr for log messages
      Throws:
      SecurityException - If a security manager is present and does not allow reassignment of the logger output stream
    • setSyncFlush

      @Deprecated public static void setSyncFlush(boolean syncFlush)
      Deprecated.
      Since 2.10, use getLoggerOutputByType(FileLoggerOutput.class).setSyncFlush(syncFlush) instead
      Sets whether the log buffer should be flushed for every message.
      Parameters:
      syncFlush - true to synchronously flush the log buffer
      Throws:
      SecurityException - If a security manager is present and does not allow changing logger settings
    • getLogFile

      @Deprecated public static String getLogFile()
      Deprecated.
      Since 2.10, use getLoggerOutputByType(FileLoggerOutput.class).getLogFile() instead
      Returns the file name log messages are written to.
      Returns:
      The configured log file, or null if no log file is used
      Since:
      2.5
    • setLogLevel

      public static LogLevel setLogLevel(LogLevel logLevel)
      Sets the log level to the given value and returns the previous value.
      Parameters:
      logLevel - The new log level
      Returns:
      The previous log level
      Throws:
      SecurityException - If a security manager is present and does not allow changing logger settings
    • getLogLevel

      public static LogLevel getLogLevel()
      Returns the current log level.
      Returns:
      The current log level