Class HybrisLogger

  • All Implemented Interfaces:
    org.apache.log4j.spi.AppenderAttachable

    public class HybrisLogger
    extends org.apache.log4j.Logger
    This 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 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

      Fields 
      Modifier and Type Field Description
      protected org.slf4j.Logger slf4jLogger  
      • Fields inherited from class org.apache.log4j.Category

        additive, level, parent, repository, resourceBundle
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      protected HybrisLogger​(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 void addFilter​(HybrisLogFilter newFilter)
      Adds a log filter to the static list of filters.
      static void addFilterFirst​(HybrisLogFilter newFilter)
      Adds a log filter to the static list of filters.
      static void addListener​(HybrisLogListener newListener)
      Adds a log listener to the static list of listeners.
      void debug​(java.lang.Object message)  
      void debug​(java.lang.Object message, java.lang.Throwable throwable)  
      static void disableListeners()  
      static void enableListeners()  
      void error​(java.lang.Object message)  
      void error​(java.lang.Object message, java.lang.Throwable throwable)  
      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?
      static HybrisLoggingEvent extendByFilters​(HybrisLoggingEvent event)
      Calls all filter sequential in list order.
      void fatal​(java.lang.Object message)  
      void fatal​(java.lang.Object message, java.lang.Throwable throwable)  
      static java.util.List<HybrisLogListener> getAllAListeners()  
      static java.util.List<HybrisLogFilter> getFilters()  
      void info​(java.lang.Object message)  
      void info​(java.lang.Object message, java.lang.Throwable throwable)  
      boolean isDebugEnabled()  
      boolean isEnabledFor​(org.apache.log4j.Priority level)  
      boolean isInfoEnabled()  
      protected static boolean isThreadCurrentlyLoggingToListeners()  
      void log​(java.lang.String callerFQCN, org.apache.log4j.Priority level, java.lang.Object message, java.lang.Throwable t)  
      void log​(org.apache.log4j.Priority priority, java.lang.Object message)  
      void log​(org.apache.log4j.Priority priority, java.lang.Object message, java.lang.Throwable t)  
      static void logToListeners​(HybrisLoggingEvent event)
      Calls all listeners with given log event which are enabled for level of log.
      protected static void markThreadCurrentlyLoggingToListeners()  
      static void removeAllListeners()
      Removes all listeners from the list
      static boolean removeFilter​(HybrisLogFilter toRemove)
      Removes a filter from the list of filters and therefore will not be called anymore.
      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.
      void setLevel​(org.apache.log4j.Level level)  
      protected static void unsetThreadCurrentlyLoggingToListeners()  
      void warn​(java.lang.Object message)  
      void warn​(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
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • slf4jLogger

        protected final org.slf4j.Logger slf4jLogger
    • Constructor Detail

      • HybrisLogger

        protected HybrisLogger​(java.lang.String name)
        Instantiates a new logger with given name. For getting the logger instance for your class use static method getLogger(..).
        Parameters:
        name - name of the logger
    • 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:
        debug in class org.apache.log4j.Category
      • debug

        public void debug​(java.lang.Object message,
                          java.lang.Throwable throwable)
        Overrides:
        debug in class org.apache.log4j.Category
      • error

        public void error​(java.lang.Object message)
        Overrides:
        error in class org.apache.log4j.Category
      • error

        public void error​(java.lang.Object message,
                          java.lang.Throwable throwable)
        Overrides:
        error in class org.apache.log4j.Category
      • fatal

        public void fatal​(java.lang.Object message)
        Overrides:
        fatal in class org.apache.log4j.Category
      • fatal

        public void fatal​(java.lang.Object message,
                          java.lang.Throwable throwable)
        Overrides:
        fatal in class org.apache.log4j.Category
      • info

        public void info​(java.lang.Object message)
        Overrides:
        info in class org.apache.log4j.Category
      • warn

        public void warn​(java.lang.Object message,
                         java.lang.Throwable throwable)
        Overrides:
        warn in class org.apache.log4j.Category
      • warn

        public void warn​(java.lang.Object message)
        Overrides:
        warn in class org.apache.log4j.Category
      • info

        public void info​(java.lang.Object message,
                         java.lang.Throwable throwable)
        Overrides:
        info in class org.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:
        log in class org.apache.log4j.Category
      • log

        public void log​(org.apache.log4j.Priority priority,
                        java.lang.Object message)
        Overrides:
        log in class org.apache.log4j.Category
      • log

        public void log​(org.apache.log4j.Priority priority,
                        java.lang.Object message,
                        java.lang.Throwable t)
        Overrides:
        log in class org.apache.log4j.Category
      • isDebugEnabled

        public boolean isDebugEnabled()
        Overrides:
        isDebugEnabled in class org.apache.log4j.Category
      • isEnabledFor

        public boolean isEnabledFor​(org.apache.log4j.Priority level)
        Overrides:
        isEnabledFor in class org.apache.log4j.Category
      • setLevel

        public void setLevel​(org.apache.log4j.Level level)
        Overrides:
        setLevel in class org.apache.log4j.Category
      • isInfoEnabled

        public boolean isInfoEnabled()
        Overrides:
        isInfoEnabled in class org.apache.log4j.Category