Package org.omegazero.common.logging
Class LoggerUtil
java.lang.Object
org.omegazero.common.logging.LoggerUtil
Maintains and manages the logging state of omz-java-lib and
Logger instances.- Since:
- 2.1
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classRepresents a permission checked by the security manager when an operation is performed that changes the logging state. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final booleanSystem propertyorg.omegazero.common.logging.internalDebugstatic final PrintStreamThe initial value ofSystem.err.static final PrintStreamThe initial value ofSystem.out. -
Method Summary
Modifier and TypeMethodDescriptionstatic voidaddFineLogListener(BiConsumer<LogLevel, String> listener) Same asaddLogListener(BiConsumer), except that listeners added here will receive all log messages, regardless of the configured log level.static voidaddLoggerOutput(LoggerOutput output) Adds the givenLoggerOutputto the list of configured outputs.static voidaddLogListener(BiConsumer<LogLevel, String> listener) Adds a log listener.static voidclose()Saves the log buffer and resets the LoggerUtil.static LoggerCreates a new logger bound to the calling class.protected static LoggercreateLogger(int off) protected static LoggercreateLogger(Class<?> cl) static voidCallsLoggerOutput.flush()on all configuredLoggerOutputs.static StringDeprecated.static <T extends LoggerOutput>
TgetLoggerOutputByType(Class<T> cl) Returns a configuredLoggerOutputwith the given type or a subtype thereof.static <T extends LoggerOutput>
List<T> getLoggerOutputsByType(Class<T> cl) Returns all configuredLoggerOutputs with the given type or a subtype thereof.static LogLevelReturns the current log level.static voidinit()Initializes the logger library, setting the log level toLogLevel.INFO.static voidInitializes the logger library, setting the maximum log level to level and the log file name, where log messages will be saved to.static booleanisLoggerMuted(String fullClassName) Returns whether the logger with the given name is muted due to a previous call tomuteLogger(String)protected static voidlogToListeners(LogLevel logLevel, String s) static booleanmuteLogger(String fullClassName) Mutes theStandardLoggerwith the given name, causing the affected logger to no longer print log messages.protected static booleanprotected static voidnewLogMessage(String line, String markup) static voidRedirects theSystem.outandSystem.errto Logger streams, causing messages printed usingSystem.out.[...]orSystem.err.[...]to be formatted to standard logger format and printed with log levelLogLevel.INFO.static booleanremoveLoggerOutput(LoggerOutput output) Removes the givenLoggerOutputfrom the list of configured outputs.static LogLevelresolveLogLevel(String str) Searches the log level referenced by the given String, either by log level number or log level name.static LogLevelsetLogLevel(LogLevel logLevel) Sets the log level to the given value and returns the previous value.static voidsetSyncFlush(boolean syncFlush) Deprecated.Since 2.10, useinsteadgetLoggerOutputByType(FileLoggerOutput.class).setSyncFlush(syncFlush)static voidsetUseStderr(boolean u) Deprecated.Since 2.10, useinsteadgetLoggerOutputByType(StdStreamsLoggerOutput.class).setUseStderr(u)static booleanunmuteLogger(String fullClassName) Unmutes theStandardLoggerwith the given name, reversing any mute operation by a previous call tomuteLogger(String).
-
Field Details
-
sysOut
The initial value ofSystem.out. -
sysErr
The initial value ofSystem.err. -
ENABLE_INTERNAL_DEBUG
public static final boolean ENABLE_INTERNAL_DEBUGSystem propertyorg.omegazero.common.logging.internalDebugEnables 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 toLogLevel.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
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 usingsetSyncFlush(boolean)by setting it totrue. The log buffer may be flushed explicitly usingflushLogBuffer(). The save interval may be changed using theorg.omegazero.common.logging.saveIntervalsystem 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 benull, in which case it will be set toLogLevel.INFOfile- The file name to save log messages to. May benullto 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
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
-
createLogger
-
muteLogger
Mutes theStandardLoggerwith the given name, causing the affected logger to no longer print log messages. If a fine log listener is configured usingaddFineLogListener(BiConsumer), the logger will continue to generate log messages.- Parameters:
fullClassName- The name of the logger, as returned byStandardLogger.getFullClassName()orClass.getName()- Returns:
trueif 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
Unmutes theStandardLoggerwith the given name, reversing any mute operation by a previous call tomuteLogger(String).- Parameters:
fullClassName- The name of the logger, as returned byStandardLogger.getFullClassName()orClass.getName()- Returns:
trueif 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
Returns whether the logger with the given name is muted due to a previous call tomuteLogger(String)- Parameters:
fullClassName- The name of the logger, as returned byStandardLogger.getFullClassName()orClass.getName()- Returns:
trueif the logger is muted- Since:
- 2.5
-
redirectStandardOutputStreams
public static void redirectStandardOutputStreams()Redirects theSystem.outandSystem.errto Logger streams, causing messages printed usingSystem.out.[...]orSystem.err.[...]to be formatted to standard logger format and printed with log levelLogLevel.INFO.- Throws:
SecurityException- If a security manager is present and does not allow reassignment of the standard or logger output streams
-
addLogListener
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, useaddFineLogListener(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
Same asaddLogListener(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()CallsLoggerOutput.flush()on all configuredLoggerOutputs. -
newLogMessage
-
logToListeners
-
needAllLogMessages
protected static boolean needAllLogMessages() -
addLoggerOutput
Adds the givenLoggerOutputto the list of configured outputs.- Parameters:
output- TheLoggerOutput- Since:
- 2.10
-
removeLoggerOutput
Removes the givenLoggerOutputfrom the list of configured outputs.- Parameters:
output- TheLoggerOutput- Returns:
trueif the instance was found and removed- Since:
- 2.10
-
getLoggerOutputByType
Returns a configuredLoggerOutputwith the given type or a subtype thereof. If multipleLoggerOutputs match, it is undefined which instance is returned.- Parameters:
cl- TheLoggerOutputtype to search for- Returns:
- The
LoggerOutputinstance, ornullif no matching instance was found - Since:
- 2.10
- See Also:
-
getLoggerOutputsByType
Returns all configuredLoggerOutputs with the given type or a subtype thereof.- Parameters:
cl- TheLoggerOutputtype to search for- Returns:
- The
LoggerOutputinstances, may be empty - Since:
- 2.10
- See Also:
-
resolveLogLevel
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.Since 2.10, useinsteadgetLoggerOutputByType(StdStreamsLoggerOutput.class).setUseStderr(u)If set totrue, all log messages created using aStandardLoggerwill be printed to the defaultSystem.errinstead of the defaultSystem.out. Set the output stream where all log messages using aStandardLoggerwill be printed to.- Parameters:
u- If loggers should usestderrfor log messages- Throws:
SecurityException- If a security manager is present and does not allow reassignment of the logger output stream
-
setSyncFlush
Deprecated.Since 2.10, useinsteadgetLoggerOutputByType(FileLoggerOutput.class).setSyncFlush(syncFlush)Sets whether the log buffer should be flushed for every message.- Parameters:
syncFlush-trueto synchronously flush the log buffer- Throws:
SecurityException- If a security manager is present and does not allow changing logger settings
-
getLogFile
Deprecated.Since 2.10, useinsteadgetLoggerOutputByType(FileLoggerOutput.class).getLogFile()Returns the file name log messages are written to.- Returns:
- The configured log file, or
nullif no log file is used - Since:
- 2.5
-
setLogLevel
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
Returns the current log level.- Returns:
- The current log level
-
insteadgetLoggerOutputByType(FileLoggerOutput.class).getLogFile()