Package com.nuix.logging
Class LogHelper
- java.lang.Object
-
- com.nuix.logging.LogHelper
-
public class LogHelper extends java.lang.Object
Provides various convenience methods for log4j2 related tasks, since doing some operations programmatically in log4j2 are not very straightforward.
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description org.apache.logging.log4j.core.appender.ConsoleAppender
attachConsoleAppender(java.lang.String pattern, java.util.function.Function<org.apache.logging.log4j.core.LogEvent,java.lang.Boolean> filteringFunction)
Attaches aConsoleAppender
org.apache.logging.log4j.core.appender.ConsoleAppender
attachConsoleAppender(java.lang.String pattern, org.apache.logging.log4j.core.Filter filter)
Attaches a newConsoleAppender
void
attachLogAppender(@NonNull org.apache.logging.log4j.core.Appender appender)
Attaches the given appenderLogMessageCallbackAppender
attachLogMessageCallbackAppender(java.lang.String pattern, java.util.Collection<java.lang.String> regexPatterns, java.util.function.Consumer<java.lang.String> consumer)
Attaches to logging system and returns aLogMessageCallbackAppender
whose purpose is to forward rendered log event strings to aConsumer
which then in turn makes use of that message.LogMessageCallbackAppender
attachLogMessageCallbackAppender(java.lang.String pattern, java.util.function.Consumer<java.lang.String> consumer)
Attaches to logging system and returns aLogMessageCallbackAppender
whose purpose is to forward rendered log event strings to aConsumer
which then in turn makes use of that message.org.apache.logging.log4j.core.Filter
createAcceptAllFilter()
Creates a Filter which accepts all log events.org.apache.logging.log4j.core.Filter
createPassFailFilter(java.util.function.Function<org.apache.logging.log4j.core.LogEvent,java.lang.Boolean> filteringFunction)
Convenience method for converting a Function<LogEvent,Boolean> into aAbstractFilter
which will yield ACCEPT when the function returns True and DENY when the function returns False.static LogHelper
getInstance()
org.apache.logging.log4j.Logger
getLogger()
Convenience method for obtaining a logger object, named after the class calling this method.org.apache.logging.log4j.Logger
getLogger(java.lang.String name)
Convenience method for obtaining a logger object.org.apache.logging.log4j.Level
getRootLoggerLevel()
Gets the current level of the root loggervoid
initAlternateLogFile(@NonNull java.io.File logFile, java.lang.String pattern, java.util.function.Function<org.apache.logging.log4j.core.LogEvent,java.lang.Boolean> filteringFunction)
Creates a file appender which will log to the specified file.org.apache.logging.log4j.core.appender.FileAppender
initAlternateLogFile(@NonNull java.io.File logFile, java.lang.String pattern, org.apache.logging.log4j.core.Filter filter)
Creates a file appender which will log to the specified file.void
removeAppender(@NonNull org.apache.logging.log4j.core.Appender appender)
Removes the given appenderorg.apache.logging.log4j.Level
setRootLoggerLevel(org.apache.logging.log4j.Level level)
Sets the root logger's level, returning the level it was set to before changing it, in case you wish to later set it back to where it was.org.apache.logging.log4j.Level
setRootLoggerLevelAll()
org.apache.logging.log4j.Level
setRootLoggerLevelDebug()
org.apache.logging.log4j.Level
setRootLoggerLevelError()
org.apache.logging.log4j.Level
setRootLoggerLevelFatal()
org.apache.logging.log4j.Level
setRootLoggerLevelInfo()
org.apache.logging.log4j.Level
setRootLoggerLevelOff()
org.apache.logging.log4j.Level
setRootLoggerLevelTrace()
org.apache.logging.log4j.Level
setRootLoggerLevelWarn()
-
-
-
Method Detail
-
getInstance
public static LogHelper getInstance()
-
getRootLoggerLevel
public org.apache.logging.log4j.Level getRootLoggerLevel()
Gets the current level of the root logger- Returns:
- The root logger's current level
-
setRootLoggerLevel
public org.apache.logging.log4j.Level setRootLoggerLevel(org.apache.logging.log4j.Level level)
Sets the root logger's level, returning the level it was set to before changing it, in case you wish to later set it back to where it was.- Parameters:
level
- The new level to assign to the root logger- Returns:
- The root logger's level before changing it
-
setRootLoggerLevelOff
public org.apache.logging.log4j.Level setRootLoggerLevelOff()
-
setRootLoggerLevelFatal
public org.apache.logging.log4j.Level setRootLoggerLevelFatal()
-
setRootLoggerLevelError
public org.apache.logging.log4j.Level setRootLoggerLevelError()
-
setRootLoggerLevelWarn
public org.apache.logging.log4j.Level setRootLoggerLevelWarn()
-
setRootLoggerLevelInfo
public org.apache.logging.log4j.Level setRootLoggerLevelInfo()
-
setRootLoggerLevelDebug
public org.apache.logging.log4j.Level setRootLoggerLevelDebug()
-
setRootLoggerLevelTrace
public org.apache.logging.log4j.Level setRootLoggerLevelTrace()
-
setRootLoggerLevelAll
public org.apache.logging.log4j.Level setRootLoggerLevelAll()
-
initAlternateLogFile
public org.apache.logging.log4j.core.appender.FileAppender initAlternateLogFile(@NonNull @NonNull java.io.File logFile, java.lang.String pattern, org.apache.logging.log4j.core.Filter filter)
Creates a file appender which will log to the specified file. If a pattern is provided, it will be used as the layout (see docs). If no pattern is supplied, then a default of "%d{yyyy-MM-dd HH:mm:ss.SSS Z} [%t] %r %-5p %c - %m%n" will be used. If a filter is provided, it will be used to determine which log events are ultimately written to the created file appender (and therefore the log file this creates). If no filter is provided, a default one will be used which accepts all log events it is provided.- Parameters:
logFile
- The absolute file path to which this appender will write eventspattern
- The logging pattern, uses "%d{yyyy-MM-dd HH:mm:ss.SSS Z} [%t] %r %-5p %c - %m%n" if null provided.filter
- The filter used to determine which log events are written. Will accept all events if null is provided.- Returns:
- The FileAppender object, which could be used to later remove/close the appender.
-
attachConsoleAppender
public org.apache.logging.log4j.core.appender.ConsoleAppender attachConsoleAppender(java.lang.String pattern, org.apache.logging.log4j.core.Filter filter)
Attaches a newConsoleAppender
- Parameters:
pattern
- The pattern to use, a null value will default to%d{yyyy-MM-dd HH:mm:ss.SSS Z} [%t] %r %-5p %c - %m%n
filter
- The filter to use, a null value will default to filter returned bycreateAcceptAllFilter()
.- Returns:
- Returns the newly created and attached console appender
-
attachLogAppender
public void attachLogAppender(@NonNull @NonNull org.apache.logging.log4j.core.Appender appender)
Attaches the given appender- Parameters:
appender
- The appender to attach
-
removeAppender
public void removeAppender(@NonNull @NonNull org.apache.logging.log4j.core.Appender appender)
Removes the given appender- Parameters:
appender
- The appender to remove
-
attachConsoleAppender
public org.apache.logging.log4j.core.appender.ConsoleAppender attachConsoleAppender(java.lang.String pattern, java.util.function.Function<org.apache.logging.log4j.core.LogEvent,java.lang.Boolean> filteringFunction)
Attaches aConsoleAppender
- Parameters:
pattern
- The pattern to usefilteringFunction
- A function that will receive each log event, returning True to ACCEPT and False to DENY.- Returns:
- The newly created console appender
-
initAlternateLogFile
public void initAlternateLogFile(@NonNull @NonNull java.io.File logFile, java.lang.String pattern, java.util.function.Function<org.apache.logging.log4j.core.LogEvent,java.lang.Boolean> filteringFunction)
Creates a file appender which will log to the specified file. If a pattern is provided, it will be used as the layout (see docs). If no pattern is supplied, then a default of "%d{yyyy-MM-dd HH:mm:ss.SSS Z} [%t] %r %-5p %c - %m%n" will be used. If a filter is provided, it will be used to determine which log events are ultimately written to the created file appender (and therefore the log file this creates). If no filter is provided, a default one will be used which accepts all log events it is provided. This differs from the methodinitAlternateLogFile(File, String, Filter)
in that this function accepts a generic Function<LogEvent,Boolean> which will be wrapped by an anonymousAbstractFilter
instance. When the provided filtering function returns True, the wrapped filter will return an ACCEPT result. When it returns False, the wrapped will return a DENY result. This method exists mostly to make working with it from a scripting language (such as Ruby) potentially easier. Example in Ruby:
LogHelper.initAlternateLogFile(java.io.File.new("D:\\temp\\script.log"),nil) do |event| next (!event.nil? && event.getLoggerName.startsWith("proservTest")) end
- Parameters:
logFile
- The absolute file path to which this appender will write eventspattern
- The logging pattern, uses "%d{yyyy-MM-dd HH:mm:ss.SSS Z} [%t] %r %-5p %c - %m%n" if null provided.filteringFunction
- A function that will receive each log event, returning True to ACCEPT and False to DENY.
-
attachLogMessageCallbackAppender
public LogMessageCallbackAppender attachLogMessageCallbackAppender(java.lang.String pattern, java.util.function.Consumer<java.lang.String> consumer)
Attaches to logging system and returns aLogMessageCallbackAppender
whose purpose is to forward rendered log event strings to aConsumer
which then in turn makes use of that message. For example, if you wish to forward log messages to the Nuix script console you might do something like:
Note that by default, no filtering is applied to log events! If you wish to control which log events get forwarded the you will want to instead do something like this:pattern = "%d{yyyy-MM-dd HH:mm:ss.SSS Z} [%t] %r %-5p %c - %m%n" appender = LogHelper.getInstance.attachLogMessageCallbackAppender(pattern){|message| puts message}
pattern = "%d{yyyy-MM-dd HH:mm:ss.SSS Z} [%t] %r %-5p %c - %m%n" appender = LogHelper.getInstance.attachLogMessageCallbackAppender(pattern){|message| puts message} filter = LogHelper.getInstance.createPassFailFilter do |log_event| return log_event.getLoggerName == "MY_SPECIAL_LOGGER" end appender.addFilter(filter)
- Parameters:
pattern
- The pattern used to construct thePatternLayout
that will be used to renderLogEvent
instances into strings.consumer
- The consumer which will receive log messages.- Returns:
- The appender so you can remove it at a later time using
removeAppender(Appender)
.
-
attachLogMessageCallbackAppender
public LogMessageCallbackAppender attachLogMessageCallbackAppender(java.lang.String pattern, java.util.Collection<java.lang.String> regexPatterns, java.util.function.Consumer<java.lang.String> consumer)
Attaches to logging system and returns aLogMessageCallbackAppender
whose purpose is to forward rendered log event strings to aConsumer
which then in turn makes use of that message. This will also attach aFilter
which will accept any LogEvents in which the logger name matches one of the provided regex patterns. For example, if you wish to forward log messages to the Nuix script console you might do something like:pattern = "%d{yyyy-MM-dd HH:mm:ss.SSS Z} [%t] %r %-5p %c - %m%n" regex_patterns = ["com\.nuix\.proserv.*"] # Forward events from this library appender = LogHelper.getInstance.attachLogMessageCallbackAppender(pattern){|message| puts message}
- Parameters:
pattern
- The pattern used to construct thePatternLayout
that will be used to renderLogEvent
instances into strings.regexPatterns
- One or more regular expressions that will be used by the filter to filter log events by their logger name.consumer
- The consumer which will receive log messages.- Returns:
- The appender so you can remove it at a later time using
removeAppender(Appender)
.
-
getLogger
public org.apache.logging.log4j.Logger getLogger(java.lang.String name)
Convenience method for obtaining a logger object.- Parameters:
name
- The name to assign the logger- Returns:
- A logger object
-
getLogger
public org.apache.logging.log4j.Logger getLogger()
Convenience method for obtaining a logger object, named after the class calling this method. Internally the class name is determined and then used in call togetLogger(String)
.- Returns:
- A logger object
-
createPassFailFilter
public org.apache.logging.log4j.core.Filter createPassFailFilter(java.util.function.Function<org.apache.logging.log4j.core.LogEvent,java.lang.Boolean> filteringFunction)
Convenience method for converting a Function<LogEvent,Boolean> into aAbstractFilter
which will yield ACCEPT when the function returns True and DENY when the function returns False.- Parameters:
filteringFunction
- A function which will be used to determine what events are filtered.- Returns:
- A
Filter
instance
-
createAcceptAllFilter
public org.apache.logging.log4j.core.Filter createAcceptAllFilter()
Creates a Filter which accepts all log events.- Returns:
- A Filter which accepts all log events blindly
-
-