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 class
Represents a permission checked by the security manager when an operation is performed that changes the logging state. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final boolean
System propertyorg.omegazero.common.logging.internalDebug
static final PrintStream
The initial value ofSystem.err
.static final PrintStream
The initial value ofSystem.out
. -
Method Summary
Modifier and TypeMethodDescriptionstatic void
addFineLogListener
(BiConsumer<LogLevel, String> listener) Same asaddLogListener(BiConsumer)
, except that listeners added here will receive all log messages, regardless of the configured log level.static void
addLoggerOutput
(LoggerOutput output) Adds the givenLoggerOutput
to the list of configured outputs.static void
addLogListener
(BiConsumer<LogLevel, String> listener) Adds a log listener.static void
close()
Saves the log buffer and resets the LoggerUtil.static Logger
Creates a new logger bound to the calling class.protected static Logger
createLogger
(int off) protected static Logger
createLogger
(Class<?> cl) static void
CallsLoggerOutput.flush()
on all configuredLoggerOutput
s.static String
Deprecated.static <T extends LoggerOutput>
TgetLoggerOutputByType
(Class<T> cl) Returns a configuredLoggerOutput
with the given type or a subtype thereof.static <T extends LoggerOutput>
List<T>getLoggerOutputsByType
(Class<T> cl) Returns all configuredLoggerOutput
s with the given type or a subtype thereof.static LogLevel
Returns the current log level.static void
init()
Initializes the logger library, setting the log level toLogLevel.INFO
.static void
Initializes the logger library, setting the maximum log level to level and the log file name, where log messages will be saved to.static boolean
isLoggerMuted
(String fullClassName) Returns whether the logger with the given name is muted due to a previous call tomuteLogger(String)
protected static void
logToListeners
(LogLevel logLevel, String s) static boolean
muteLogger
(String fullClassName) Mutes theStandardLogger
with the given name, causing the affected logger to no longer print log messages.protected static boolean
protected static void
newLogMessage
(String line, String markup) static void
Redirects theSystem.out
andSystem.err
to Logger streams, causing messages printed usingSystem.out.[...]
orSystem.err.[...]
to be formatted to standard logger format and printed with log levelLogLevel.INFO
.static boolean
removeLoggerOutput
(LoggerOutput output) Removes the givenLoggerOutput
from the list of configured outputs.static LogLevel
resolveLogLevel
(String str) Searches the log level referenced by the given String, either by log level number or log level name.static LogLevel
setLogLevel
(LogLevel logLevel) Sets the log level to the given value and returns the previous value.static void
setSyncFlush
(boolean syncFlush) Deprecated.Since 2.10, use
insteadgetLoggerOutputByType
(FileLoggerOutput
.class).setSyncFlush
(syncFlush)static void
setUseStderr
(boolean u) Deprecated.Since 2.10, use
insteadgetLoggerOutputByType
(StdStreamsLoggerOutput
.class).setUseStderr
(u)static boolean
unmuteLogger
(String fullClassName) Unmutes theStandardLogger
with 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.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 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.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 benull
, in which case it will be set toLogLevel.INFO
file
- The file name to save log messages to. May benull
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
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 theStandardLogger
with 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:
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
Unmutes theStandardLogger
with 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:
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
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:
true
if the logger is muted- Since:
- 2.5
-
redirectStandardOutputStreams
public static void redirectStandardOutputStreams()Redirects theSystem.out
andSystem.err
to 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 configuredLoggerOutput
s. -
newLogMessage
-
logToListeners
-
needAllLogMessages
protected static boolean needAllLogMessages() -
addLoggerOutput
Adds the givenLoggerOutput
to the list of configured outputs.- Parameters:
output
- TheLoggerOutput
- Since:
- 2.10
-
removeLoggerOutput
Removes the givenLoggerOutput
from the list of configured outputs.- Parameters:
output
- TheLoggerOutput
- Returns:
true
if the instance was found and removed- Since:
- 2.10
-
getLoggerOutputByType
Returns a configuredLoggerOutput
with the given type or a subtype thereof. If multipleLoggerOutput
s match, it is undefined which instance is returned.- Parameters:
cl
- TheLoggerOutput
type to search for- Returns:
- The
LoggerOutput
instance, ornull
if no matching instance was found - Since:
- 2.10
- See Also:
-
getLoggerOutputsByType
Returns all configuredLoggerOutput
s with the given type or a subtype thereof.- Parameters:
cl
- TheLoggerOutput
type to search for- Returns:
- The
LoggerOutput
instances, 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, use
insteadgetLoggerOutputByType
(StdStreamsLoggerOutput
.class).setUseStderr
(u)If set totrue
, all log messages created using aStandardLogger
will be printed to the defaultSystem.err
instead of the defaultSystem.out
. Set the output stream where all log messages using aStandardLogger
will be printed to.- Parameters:
u
- If loggers should usestderr
for 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, use
insteadgetLoggerOutputByType
(FileLoggerOutput
.class).setSyncFlush
(syncFlush)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.Since 2.10, use
insteadgetLoggerOutputByType
(FileLoggerOutput
.class).getLogFile
()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
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
()