Class HybrisLogger
- java.lang.Object
-
- org.apache.log4j.Category
-
- org.apache.log4j.Logger
-
- de.hybris.platform.util.logging.HybrisLogger
-
- All Implemented Interfaces:
org.apache.log4j.spi.AppenderAttachable
public class HybrisLogger extends org.apache.log4j.LoggerThis is a [y]hybris specific logger which extends the log4j-Logger with an additional listener and filter concept. You can easily add your own listener to this class which will be called each time a log is propagated with this logger class as well as a filter which can deny a log and modify the logged message.The reason for for using an own logger with listener concept, is the lack of context dependent filtering of log messages before calling the log method. Our use case is the specialized logging within a job instance, besides the normal logging. Here, there are two outputs (to database and file) additional to the normal output each with different and at runtime configurable log level. Because the support of thread-hierarchical logging is not supported you have to store the job instance local to the thread to determine while logging which appenders and log level is needed (When a job is set to the current thread we have to log additionally to the two outputs with special log levels). The easiest way is to set the global log level to debug, realize the additional outputs with
Appenders and for deciding whether log messages have to use the normal and/or other appender a Filter for each appender can be used. Problem is, you call the log method (isEnableFor is always true because of global debug), then the filter of each appender filters the not used logs dependent on level and then the log is putted out to the appender. So, each time the log method is used, even when a log is filtered completely. This is a performance bottleneck. So, the [y]hybris logger overwrites the isEnabledFor(..) methods to check for logging needs before calling the log methods.
See PLA-148 for further details.The reason for for using an own logger with filter concept, is the lack of extending the real log message in some specific situations after a log method is called. The only chance to do this is the usage of the
MDCorNDCclass, but the extending of the log has to be configured at the appender. If the message only has to be extended in some situations (here if the ImpEx extension logs something), it is not acceptable to modify the pattern layout. (The extending of the message in case for ImpEx has to be done always and there is no need to remove it from the pattern layout. So it is a better to not mention it at the layout configuration, it only confuses the user).
See PLA-5328 for further details.
-
-
Field Summary
Fields Modifier and Type Field Description protected org.slf4j.Loggerslf4jLogger
-
Constructor Summary
Constructors Modifier Constructor Description protectedHybrisLogger(java.lang.String name)Instantiates a new logger with given name.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static voidaddFilter(HybrisLogFilter newFilter)Adds a log filter to the static list of filters.static voidaddFilterFirst(HybrisLogFilter newFilter)Adds a log filter to the static list of filters.static voidaddListener(HybrisLogListener newListener)Adds a log listener to the static list of listeners.voiddebug(java.lang.Object message)voiddebug(java.lang.Object message, java.lang.Throwable throwable)static voiddisableListeners()static voidenableListeners()voiderror(java.lang.Object message)voiderror(java.lang.Object message, java.lang.Throwable throwable)protected booleanexistsListenerEnabledFor(org.apache.log4j.Priority level)Does a listener exists in the static list of listener, which is enabled for given log level?static HybrisLoggingEventextendByFilters(HybrisLoggingEvent event)Calls all filter sequential in list order.voidfatal(java.lang.Object message)voidfatal(java.lang.Object message, java.lang.Throwable throwable)static java.util.List<HybrisLogListener>getAllAListeners()static java.util.List<HybrisLogFilter>getFilters()voidinfo(java.lang.Object message)voidinfo(java.lang.Object message, java.lang.Throwable throwable)booleanisDebugEnabled()booleanisEnabledFor(org.apache.log4j.Priority level)booleanisInfoEnabled()protected static booleanisThreadCurrentlyLoggingToListeners()voidlog(java.lang.String callerFQCN, org.apache.log4j.Priority level, java.lang.Object message, java.lang.Throwable t)voidlog(org.apache.log4j.Priority priority, java.lang.Object message)voidlog(org.apache.log4j.Priority priority, java.lang.Object message, java.lang.Throwable t)static voidlogToListeners(HybrisLoggingEvent event)Calls all listeners with given log event which are enabled for level of log.protected static voidmarkThreadCurrentlyLoggingToListeners()static voidremoveAllListeners()Removes all listeners from the liststatic booleanremoveFilter(HybrisLogFilter toRemove)Removes a filter from the list of filters and therefore will not be called anymore.static booleanremoveListener(HybrisLogListener toRemove)Removes the first occurrence in the static listener list of the specified listener If the list does not contain the element, it is unchanged.voidsetLevel(org.apache.log4j.Level level)protected static voidunsetThreadCurrentlyLoggingToListeners()voidwarn(java.lang.Object message)voidwarn(java.lang.Object message, java.lang.Throwable throwable)-
Methods inherited from class org.apache.log4j.Logger
getLogger, getLogger, getLogger, getRootLogger, isTraceEnabled, trace, trace
-
Methods inherited from class org.apache.log4j.Category
addAppender, assertLog, callAppenders, exists, forcedLog, getAdditivity, getAllAppenders, getAppender, getChainedPriority, getCurrentCategories, getDefaultHierarchy, getEffectiveLevel, getHierarchy, getInstance, getInstance, getLevel, getLoggerRepository, getName, getParent, getPriority, getResourceBundle, getResourceBundleString, getRoot, isAttached, l7dlog, l7dlog, removeAllAppenders, removeAppender, removeAppender, setAdditivity, setPriority, setResourceBundle, shutdown
-
-
-
-
Method Detail
-
disableListeners
public static void disableListeners()
-
enableListeners
public static void enableListeners()
-
addFilter
public static void addFilter(HybrisLogFilter newFilter)
Adds a log filter to the static list of filters. This filters will be called sequential before a log is logged. Adds the filter at last position to the list.- Parameters:
newFilter- filter which will be called before a log is logged
-
addFilterFirst
public static void addFilterFirst(HybrisLogFilter newFilter)
Adds a log filter to the static list of filters. This filters will be called sequential before a log is logged. Adds the filter at first position to the list.- Parameters:
newFilter- filter which will be called before a log is logged
-
removeFilter
public static boolean removeFilter(HybrisLogFilter toRemove)
Removes a filter from the list of filters and therefore will not be called anymore.- Parameters:
toRemove- filter to remove- Returns:
- true if the filter was existent at the filter list, false otherwise
-
extendByFilters
public static HybrisLoggingEvent extendByFilters(HybrisLoggingEvent event)
Calls all filter sequential in list order. Each filter can modify the log event ( especially can set the deny flag).- Parameters:
event- log event which will be passed to each filter and can be modified- Returns:
- the event passed to all filters
-
addListener
public static void addListener(HybrisLogListener newListener)
Adds a log listener to the static list of listeners. This listeners will be called when a log is available.- Parameters:
newListener- listener which will be called from now when a log is given.
-
removeListener
public static boolean removeListener(HybrisLogListener toRemove)
Removes the first occurrence in the static listener list of the specified listener If the list does not contain the element, it is unchanged. More formally, removes the element with the lowest index i such that (o==null ? get(i)==null : o.equals(get(i))) (if such an element exists).- Parameters:
toRemove- listener to be removed from listener list, if present.- Returns:
- true if this list contained the specified element.
-
removeAllListeners
public static void removeAllListeners()
Removes all listeners from the list
-
getAllAListeners
public static java.util.List<HybrisLogListener> getAllAListeners()
-
existsListenerEnabledFor
protected boolean existsListenerEnabledFor(org.apache.log4j.Priority level)
Does a listener exists in the static list of listener, which is enabled for given log level?- Parameters:
level- the level for which the listeners will be checked- Returns:
- true if there is at least one listener which is enabled for given level, false otherwise
-
logToListeners
public static void logToListeners(HybrisLoggingEvent event)
Calls all listeners with given log event which are enabled for level of log.- Parameters:
event- the log event the listeners will be called
-
isThreadCurrentlyLoggingToListeners
protected static boolean isThreadCurrentlyLoggingToListeners()
-
markThreadCurrentlyLoggingToListeners
protected static void markThreadCurrentlyLoggingToListeners()
-
unsetThreadCurrentlyLoggingToListeners
protected static void unsetThreadCurrentlyLoggingToListeners()
-
debug
public void debug(java.lang.Object message)
- Overrides:
debugin classorg.apache.log4j.Category
-
debug
public void debug(java.lang.Object message, java.lang.Throwable throwable)- Overrides:
debugin classorg.apache.log4j.Category
-
error
public void error(java.lang.Object message)
- Overrides:
errorin classorg.apache.log4j.Category
-
error
public void error(java.lang.Object message, java.lang.Throwable throwable)- Overrides:
errorin classorg.apache.log4j.Category
-
fatal
public void fatal(java.lang.Object message)
- Overrides:
fatalin classorg.apache.log4j.Category
-
fatal
public void fatal(java.lang.Object message, java.lang.Throwable throwable)- Overrides:
fatalin classorg.apache.log4j.Category
-
info
public void info(java.lang.Object message)
- Overrides:
infoin classorg.apache.log4j.Category
-
warn
public void warn(java.lang.Object message, java.lang.Throwable throwable)- Overrides:
warnin classorg.apache.log4j.Category
-
warn
public void warn(java.lang.Object message)
- Overrides:
warnin classorg.apache.log4j.Category
-
info
public void info(java.lang.Object message, java.lang.Throwable throwable)- Overrides:
infoin classorg.apache.log4j.Category
-
log
public void log(java.lang.String callerFQCN, org.apache.log4j.Priority level, java.lang.Object message, java.lang.Throwable t)- Overrides:
login classorg.apache.log4j.Category
-
log
public void log(org.apache.log4j.Priority priority, java.lang.Object message)- Overrides:
login classorg.apache.log4j.Category
-
log
public void log(org.apache.log4j.Priority priority, java.lang.Object message, java.lang.Throwable t)- Overrides:
login classorg.apache.log4j.Category
-
isDebugEnabled
public boolean isDebugEnabled()
- Overrides:
isDebugEnabledin classorg.apache.log4j.Category
-
isEnabledFor
public boolean isEnabledFor(org.apache.log4j.Priority level)
- Overrides:
isEnabledForin classorg.apache.log4j.Category
-
setLevel
public void setLevel(org.apache.log4j.Level level)
- Overrides:
setLevelin classorg.apache.log4j.Category
-
isInfoEnabled
public boolean isInfoEnabled()
- Overrides:
isInfoEnabledin classorg.apache.log4j.Category
-
getFilters
public static java.util.List<HybrisLogFilter> getFilters()
-
-