Package com.nuix.logging
Class LogHelper
- java.lang.Object
-
- com.nuix.logging.LogHelper
-
public class LogHelper extends java.lang.ObjectProvides 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.ConsoleAppenderattachConsoleAppender(java.lang.String pattern, java.util.function.Function<org.apache.logging.log4j.core.LogEvent,java.lang.Boolean> filteringFunction)Attaches aConsoleAppenderorg.apache.logging.log4j.core.appender.ConsoleAppenderattachConsoleAppender(java.lang.String pattern, org.apache.logging.log4j.core.Filter filter)Attaches a newConsoleAppendervoidattachLogAppender(@NonNull org.apache.logging.log4j.core.Appender appender)Attaches the given appenderLogMessageCallbackAppenderattachLogMessageCallbackAppender(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 aLogMessageCallbackAppenderwhose purpose is to forward rendered log event strings to aConsumerwhich then in turn makes use of that message.LogMessageCallbackAppenderattachLogMessageCallbackAppender(java.lang.String pattern, java.util.function.Consumer<java.lang.String> consumer)Attaches to logging system and returns aLogMessageCallbackAppenderwhose purpose is to forward rendered log event strings to aConsumerwhich then in turn makes use of that message.org.apache.logging.log4j.core.FiltercreateAcceptAllFilter()Creates a Filter which accepts all log events.org.apache.logging.log4j.core.FiltercreatePassFailFilter(java.util.function.Function<org.apache.logging.log4j.core.LogEvent,java.lang.Boolean> filteringFunction)Convenience method for converting a Function<LogEvent,Boolean> into aAbstractFilterwhich will yield ACCEPT when the function returns True and DENY when the function returns False.static LogHelpergetInstance()org.apache.logging.log4j.LoggergetLogger()Convenience method for obtaining a logger object, named after the class calling this method.org.apache.logging.log4j.LoggergetLogger(java.lang.String name)Convenience method for obtaining a logger object.org.apache.logging.log4j.LevelgetRootLoggerLevel()Gets the current level of the root loggervoidinitAlternateLogFile(@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.FileAppenderinitAlternateLogFile(@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.voidremoveAppender(@NonNull org.apache.logging.log4j.core.Appender appender)Removes the given appenderorg.apache.logging.log4j.LevelsetRootLoggerLevel(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.LevelsetRootLoggerLevelAll()org.apache.logging.log4j.LevelsetRootLoggerLevelDebug()org.apache.logging.log4j.LevelsetRootLoggerLevelError()org.apache.logging.log4j.LevelsetRootLoggerLevelFatal()org.apache.logging.log4j.LevelsetRootLoggerLevelInfo()org.apache.logging.log4j.LevelsetRootLoggerLevelOff()org.apache.logging.log4j.LevelsetRootLoggerLevelTrace()org.apache.logging.log4j.LevelsetRootLoggerLevelWarn()
-
-
-
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%nfilter- 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 anonymousAbstractFilterinstance. 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 aLogMessageCallbackAppenderwhose purpose is to forward rendered log event strings to aConsumerwhich 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 thePatternLayoutthat will be used to renderLogEventinstances 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 aLogMessageCallbackAppenderwhose purpose is to forward rendered log event strings to aConsumerwhich then in turn makes use of that message. This will also attach aFilterwhich 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 thePatternLayoutthat will be used to renderLogEventinstances 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 aAbstractFilterwhich 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
Filterinstance
-
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
-
-