Class HybrisLogger
- All Implemented Interfaces:
org.apache.log4j.spi.AppenderAttachable
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
MDC or NDC class, 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
FieldsFields inherited from class org.apache.log4j.Category
additive, bundle, level, parent, repository -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedHybrisLogger(String name) Instantiates a new logger with given name. -
Method Summary
Modifier and TypeMethodDescriptionstatic 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.voidvoidstatic voidstatic voidvoidvoidprotected booleanexistsListenerEnabledFor(org.apache.log4j.Priority level) Does a listener exists in the static list of listener, which is enabled for given log level?static HybrisLoggingEventCalls all filter sequential in list order.voidvoidstatic List<HybrisLogListener>static List<HybrisLogFilter>voidvoidbooleanbooleanisEnabledFor(org.apache.log4j.Priority level) booleanprotected static booleanvoidvoidvoidstatic voidlogToListeners(HybrisLoggingEvent event) Calls all listeners with given log event which are enabled for level of log.protected static voidstatic voidRemoves 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 voidvoidvoidMethods inherited from class org.apache.log4j.Logger
getLogger, getLogger, getLogger, getRootLogger, isTraceEnabled, trace, traceMethods 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, getRoot, isAttached, isErrorEnabled, isFatalEnabled, isWarnEnabled, l7dlog, l7dlog, removeAllAppenders, removeAppender, removeAppender, setAdditivity, setPriority, setResourceBundle, shutdown
-
Field Details
-
slf4jLogger
protected final org.slf4j.Logger slf4jLogger
-
-
Constructor Details
-
HybrisLogger
Instantiates a new logger with given name. For getting the logger instance for your class use static methodgetLogger(..).- Parameters:
name- name of the logger
-
-
Method Details
-
disableListeners
public static void disableListeners() -
enableListeners
public static void enableListeners() -
addFilter
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
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
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
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
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
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
-
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
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
- Overrides:
debugin classorg.apache.log4j.Category
-
debug
- Overrides:
debugin classorg.apache.log4j.Category
-
error
- Overrides:
errorin classorg.apache.log4j.Category
-
error
- Overrides:
errorin classorg.apache.log4j.Category
-
fatal
- Overrides:
fatalin classorg.apache.log4j.Category
-
fatal
- Overrides:
fatalin classorg.apache.log4j.Category
-
info
- Overrides:
infoin classorg.apache.log4j.Category
-
warn
- Overrides:
warnin classorg.apache.log4j.Category
-
warn
- Overrides:
warnin classorg.apache.log4j.Category
-
info
- Overrides:
infoin classorg.apache.log4j.Category
-
log
- Overrides:
login classorg.apache.log4j.Category
-
log
- Overrides:
login classorg.apache.log4j.Category
-
log
- 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
-