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.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 Details

    • slf4jLogger

      protected final org.slf4j.Logger slf4jLogger
  • Constructor Details

    • HybrisLogger

      protected HybrisLogger(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 Details

    • 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 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(Object message)
      Overrides:
      debug in class org.apache.log4j.Category
    • debug

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

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

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

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

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

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

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

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

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

      public void log(String callerFQCN, org.apache.log4j.Priority level, Object message, Throwable t)
      Overrides:
      log in class org.apache.log4j.Category
    • log

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

      public void log(org.apache.log4j.Priority priority, Object message, 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
    • getFilters

      public static List<HybrisLogFilter> getFilters()