Turning on Log4j 2.4.1 API Logging

Q

How to turn on trace logging inside Log4j 2 API? I am having trouble using Log4j 2.4.1 and want to know what's going on inside Log4j API.

✍: Quest

A

Yes, you can turn on the trace logging inside the Log4j 2 API. It will generate log messages at the "trace" level in the console showing you what's going on inside the Log4j 2 API.

1. Modify the configuration file with <Configuration status="trace">. See the example, log4j2_api_trace_config.xml, below:

<?xml version="1.0" encoding="UTF-8"?>
<!-- Copyright (c) 2015 FYIcenter.com -->
<Configuration status="trace">
 <Appenders>
  <Console name="Screen" target="SYSTEM_OUT">
   <PatternLayout 
    pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
  </Console>
 </Appenders>
 <Loggers>
  <Logger name="HelloLog4j2" level="debug" additivity="false">
   <AppenderRef ref="Screen"/>
  </Logger>
  <Root level="error">
   <AppenderRef ref="Screen"/>
  </Root>
 </Loggers>
</Configuration>

2. Run your program with the modified configuration file:

fyicenter>java -classpath .;\local\lib\log4j-api-2.4.1.jar;
   \local\lib\log4j-core-2.4.1.jar 
   "-Dlog4j.configurationFile=log4j2_api_trace_config.xml" 
   HelloLog4j2

...,537 main DEBUG Initializing configuration XmlConfiguration
   [location=fyicenter\log4j2_api_trace_config.xml]
...,539 main DEBUG PluginManager 'Core' found 77 plugins
...,539 main DEBUG PluginManager 'Level' found 0 plugins
...,540 main DEBUG PluginManager 'Lookup' found 13 plugins
...,542 main DEBUG Building Plugin[name=layout, class=org.apache
   .logging.log4j.core.layout.PatternLayout]. Searching for builder 
   factory method...
...,545 main DEBUG Found builder factory method [newBuilder]: public
    static org.apache.logging.log4j.core.layout.PatternLayout$Builder
    org.apache.logging.log4j.core.layout.PatternLayout.newBuilder().
...,551 main DEBUG TypeConverterRegistry initializing.
...,551 main DEBUG PluginManager 'TypeConverter' found 21 plugins
...,561 main DEBUG Calling build() on class class org.apache.logging
   .log4j.core.layout.PatternLayout$Builder for element PatternLayout
   with params(pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - 
   %msg%n", null, Configuration(fyicenter\log4j2_api_trace_config
   .xml), null, charset="null", alwaysWriteExceptions="null", 
   noConsoleNoAnsi="null", header="null", footer="null")
...,562 main DEBUG PluginManager 'Converter' found 38 plugins
...,563 main DEBUG Built Plugin[name=layout] OK from builder factory 
   method.
...,563 main DEBUG Building Plugin[name=appender, class=org.apache
   .logging.log4j.core.appender.ConsoleAppender]. Searching for 
   builder factory method...
...,564 main DEBUG Not in a ServletContext environment, thus not 
   loading WebLookup plugin.
...,565 main DEBUG PluginManager 'Converter' found 38 plugins
...,566 main DEBUG Jansi is not installed, cannot find org.fusesource
   .jansi.WindowsAnsiOutputStream
...,566 main DEBUG Found builder factory method [newBuilder]: 
   public static org.apache.logging.log4j.core.appender
   .ConsoleAppender$Builder org.apache.logging.log4j.core.appender
   .ConsoleAppender.newBuilder().
...,570 main DEBUG No PluginVisitorStrategy found on annotation
   [interface org.apache.logging.log4j.core.config.plugins.validation
   .constraints.Required]. Ignoring.
...,571 main DEBUG Encountered type [org.apache.logging.log4j.core
   .layout.PatternLayout] which can only be checked for null.
...,572 main DEBUG No PluginVisitorStrategy found on annotation
   [interface org.apache.logging.log4j.core.config.plugins.validation
   .constraints.Required]. Ignoring.
...,573 main DEBUG Encountered type [org.apache.logging.log4j
   .core.appender.ConsoleAppender$Target] which can only be checked 
   for null.
...,573 main DEBUG No PluginVisitorStrategy found on annotation
   [interface org.apache.logging.log4j.core.config.plugins
   .validation.constraints.Required]. Ignoring.
...,574 main DEBUG Calling build() on class class org.apache.logging
   .log4j.core.appender.ConsoleAppender$Builder for element Console 
   with params(PatternLayout(%d{HH:mm:ss.SSS} [%t] %-5level 
   %logger{36} - %msg%n), null, target="SYSTEM_OUT", name="Screen", 
   follow="null", ignoreExceptions="null")
...,575 main DEBUG Jansi is not installed, cannot find org
   .fusesource.jansi.WindowsAnsiOutputStream
...,576 main DEBUG Built Plugin[name=appender] OK from builder 
   factory method.
...,576 main DEBUG Building Plugin[name=appenders, class=org.apache
   .logging.log4j.core.config.AppendersPlugin]. Searching for 
   builder factory method...
...,578 main DEBUG No builder factory method found in class org
   .apache.logging.log4j.core.config.AppendersPlugin. Going to try 
   finding a factory method instead.
...,578 main DEBUG Still building Plugin[name=appenders, class=org
   .apache.logging.log4j.core.config.AppendersPlugin]. Searching for 
   factory method...
...,579 main DEBUG Found factory method [createAppenders]: public 
   static java.util.concurrent.ConcurrentMap org.apache.logging.log4j
   .core.config.AppendersPlugin.createAppenders(org.apache.logging
   .log4j.core.Appender[]).
...,579 main DEBUG Calling createAppenders on class org.apache
   .logging.log4j.core.config.AppendersPlugin for element Appenders 
   with params(={Screen})
...,580 main DEBUG Built Plugin[name=appenders] OK from 
   factorymethod.
...,580 main DEBUG Building Plugin[name=AppenderRef, class=org
   .apache.logging.log4j.core.config.AppenderRef]. Searching for 
   builder factory method...
...,581 main DEBUG No builder factory method found in class org
   .apache.logging.log4j.core.config.AppenderRef. Going to try 
   finding a factory method instead.
...,581 main DEBUG Still building Plugin[name=AppenderRef, class=org
   .apache.logging.log4j.core.config.AppenderRef]. Searching for 
   factory method...
...,582 main DEBUG Found factory method [createAppenderRef]: public 
   static org.apache.logging.log4j.core.config.AppenderRef org.apache
   .logging.log4j.core.config.AppenderRef.createAppenderRef(java.lang
   .String,org.apache.logging.log4j.Level,org.apache.logging.log4j
   .core.Filter).
...,587 main DEBUG Calling createAppenderRef on class org.apache
   .logging.log4j.core.config.AppenderRef for element AppenderRef 
   with params(ref="Screen", level="null", null)
...,588 main DEBUG Built Plugin[name=AppenderRef] OK from factory 
   method.
...,588 main DEBUG Building Plugin[name=logger, class=org.apache
   .logging.log4j.core.config.LoggerConfig]. Searching for builder 
   factory method...
...,589 main DEBUG No builder factory method found in class org
   .apache.logging.log4j.core.config.LoggerConfig. Going to try 
   finding a factory method instead.
...,589 main DEBUG Still building Plugin[name=logger, class=org
   .apache.logging.log4j.core.config.LoggerConfig]. Searching for 
   factory method...
...,590 main DEBUG Found factory method [createLogger]: public 
   static org.apache.logging.log4j.core.config.LoggerConfig org
   .apache.logging.log4j.core.config.LoggerConfig.createLogger(java
   .lang.String,org.apache.logging.log4j.Level,java.lang.String,java
   .lang.String,org.apache.logging.log4j.core.config.AppenderRef[],
   org.apache.logging.log4j.core.config.Property[],org.apache.logging
   .log4j.core.config.Configuration,org.apache.logging.log4j.core
   .Filter).
...,591 main DEBUG Calling createLogger on class org.apache.logging
   .log4j.core.config.LoggerConfig for element Logger with params
   (additivity="false", level="DEBUG", name="HelloLog4j2", 
   includeLocation="null", ={Screen}, ={}, Configuration(fyicenter
   \log4j2_api_trace_config.xml), null)
...,593 main DEBUG Built Plugin[name=logger] OK from factory method.
...,593 main DEBUG Building Plugin[name=AppenderRef, class=org
   .apache.logging.log4j.core.config.AppenderRef]. Searching for 
   builder factory method...
...,594 main DEBUG No builder factory method found in class org
   .apache.logging.log4j.core.config.AppenderRef. Going to try 
   finding a factory method instead.
...,595 main DEBUG Still building Plugin[name=AppenderRef, class
   =org.apache.logging.log4j.core.config.AppenderRef]. Searching for 
   factory method...
...,595 main DEBUG Found factory method [createAppenderRef]: 
   public static org.apache.logging.log4j.core.config.AppenderRef 
   org.apache.logging.log4j.core.config.AppenderRef
   .createAppenderRef(java.lang.String,org.apache.logging.log4j
   .Level,org.apache.logging.log4j.core.Filter).
...,596 main DEBUG Calling createAppenderRef on class org.apache
   .logging.log4j.core.config.AppenderRef for element AppenderRef 
   with params(ref="Screen", level="null", null)
...,596 main DEBUG Built Plugin[name=AppenderRef] OK from factory
   method.
...,597 main DEBUG Building Plugin[name=root, class=org.apache
   .logging.log4j.core.config.LoggerConfig$RootLogger]. Searching 
   for builder factory method...
...,597 main DEBUG No builder factory method found in class org
   .apache.logging.log4j.core.config.LoggerConfig$RootLogger. Going 
   to try finding a factory method instead.
...,597 main DEBUG Still building Plugin[name=root, class=org
   .apache.logging.log4j.core.config.LoggerConfig$RootLogger]. 
   Searching for factory method...
...,598 main DEBUG Found factory method [createLogger]: public 
   static org.apache.logging.log4j.core.config.LoggerConfig org
   .apache.logging.log4j.core.config.LoggerConfig$RootLogger
   .createLogger(java.lang.String,org.apache.logging.log4j.Level,
   java.lang.String,org.apache.logging.log4j.core.config
   .AppenderRef[],org.apache.logging.log4j.core.config.Property[],
   org.apache.logging.log4j.core.config.Configuration,org.apache
   .logging.log4j.core.Filter).
...,599 main DEBUG Calling createLogger on class org.apache.logging
   .log4j.core.config.LoggerConfig$RootLogger for element Root with 
   params(additivity="null", level="ERROR", includeLocation="null", 
   ={Screen}, ={}, Configuration(fyicenter\log4j2_api_trace_config
   .xml), null)
...,600 main DEBUG Built Plugin[name=root] OK from factory method.
...,600 main DEBUG Building Plugin[name=loggers, class=org.apache
   .logging.log4j.core.config.LoggersPlugin]. Searching for builder 
   factory method...
...,601 main DEBUG No builder factory method found in class org
   .apache.logging.log4j.core.config.LoggersPlugin. Going to try 
   finding a factory method instead.
...,601 main DEBUG Still building Plugin[name=loggers, class=org
   .apache.logging.log4j.core.config.LoggersPlugin]. Searching for 
   factory method...
...,602 main DEBUG Found factory method [createLoggers]: 
   publicstatic org.apache.logging.log4j.core.config.Loggers 
   org.apache.logging.log4j.core.config.LoggersPlugin.createLoggers
   (org.apache.logging.log4j.core.config.LoggerConfig[]).
...,602 main DEBUG Calling createLoggers on class org.apache
   .logging.log4j.core.config.LoggersPlugin for element Loggers 
   with params(={HelloLog4j2, root})
...,603 main DEBUG Built Plugin[name=loggers] OK from factory
   method.
...,604 main DEBUG Configuration XmlConfiguration[location
   =fyicenter\log4j2_api_trace_config.xml] initialized
...,604 main DEBUG Starting configuration XmlConfiguration
   [location=fyicenter\log4j2_api_trace_config.xml]
...,604 main DEBUG Started configuration XmlConfiguration
   [location=fyicenter\log4j2_api_trace_config.xml] OK.
...,605 main TRACE Stopping org.apache.logging.log4j.core.config
   .DefaultConfiguration@14d3b51...
...,605 main TRACE AbstractConfiguration notified 0 
   ReliabilityStrategies that config will be stopped.
...,605 main TRACE AbstractConfiguration stopped 0
   AsyncLoggerConfigs.
...,606 main TRACE AbstractConfiguration stopped 0 AsyncAppenders.
...,606 main TRACE AbstractConfiguration notified 0 
   ReliabilityStrategies that appenders will be stopped.
...,607 main TRACE AbstractConfiguration stopped 1 Appenders.
...,607 main TRACE AbstractConfiguration stopped 0 LoggerConfigs.
...,607 main DEBUG Stopped org.apache.logging.log4j.core.config
   .DefaultConfiguration@14d3b51 OK
...,648 main DEBUG Registering MBean org.apache.logging.log4j2:
   type=4687246
...,651 main DEBUG Registering MBean org.apache.logging.log4j2:
   type=4687246,component=StatusLogger
...,652 main DEBUG Registering MBean org.apache.logging.log4j2:
   type=4687246,component=ContextSelector
...,654 main DEBUG Registering MBean org.apache.logging.log4j2:
   type=4687246,component=Loggers,name=
...,654 main DEBUG Registering MBean org.apache.logging.log4j2:
   type=4687246,component=Loggers,name=HelloLog4j2
...,656 main DEBUG Registering MBean org.apache.logging.log4j2:
   type=4687246,component=Appenders,name=Screen
...,658 main DEBUG Using default SystemClock for timestamps
...,661 main DEBUG Could not set AsyncLogger NanoClock. Ignoring: 
   java.lang.NoClassDefFoundError: com/lmax/disruptor/WaitStrategy
...,661 main DEBUG Reconfiguration complete for context
   [name=4687246] at URI null (org.apache.logging.log4j.core
   .LoggerContext@1c51ef0) with optional ClassLoader: null
...,661 main DEBUG Shutdown hook enabled. Registering a new one.
...,662 main DEBUG LoggerContext[name=4687246, org.apache.logging
   .log4j.core.LoggerContext@1c51ef0] started OK.
....666 [main] FATAL HelloLog4j2 - Hello - fatal
....667 [main] ERROR HelloLog4j2 - Hello - error
....667 [main] WARN  HelloLog4j2 - Hello - warn
....667 [main] INFO  HelloLog4j2 - Hello - info
....667 [main] DEBUG HelloLog4j2 - Hello - debug
...,668 pool-1-thread-1 DEBUG Stopping LoggerContext[name=4687246, 
   org.apache.logging.log4j.core.LoggerContext@1c51ef0]
...,668 pool-1-thread-1 DEBUG Stopping LoggerContext[name=4687246, 
   org.apache.logging.log4j.core.LoggerContext@1c51ef0]...
...,669 pool-1-thread-1 DEBUG Unregistering MBean org.apache
   .logging.log4j2:type=4687246
...,669 pool-1-thread-1 DEBUG Unregistering MBean org.apache
   .logging.log4j2:type=4687246,component=StatusLogger
...,670 pool-1-thread-1 DEBUG Unregistering MBean org.apache
   .logging.log4j2:type=4687246,component=ContextSelector
...,670 pool-1-thread-1 DEBUG Unregistering MBean org.apache
   .logging.log4j2:type=4687246,component=Loggers,name=
...,670 pool-1-thread-1 DEBUG Unregistering MBean org.apache
   .logging.log4j2:type=4687246,component=Loggers,name=HelloLog4j2
...,671 pool-1-thread-1 DEBUG Unregistering MBean org.apache
   .logging.log4j2:type=4687246,component=Appenders,name=Screen
...,672 pool-1-thread-1 TRACE Stopping XmlConfiguration
   [location=fyicenter\log4j2_api_trace_config.xml]...
...,672 pool-1-thread-1 TRACE AbstractConfiguration notified 2 
   ReliabilityStrategies that config will be stopped.
...,672 pool-1-thread-1 TRACE AbstractConfiguration stopped 0 
   AsyncLoggerConfigs.
...,673 pool-1-thread-1 TRACE AbstractConfiguration stopped 0 
   AsyncAppenders.
...,673 pool-1-thread-1 TRACE AbstractConfiguration notified 2 
   ReliabilityStrategies that appenders will be stopped.
...,673 pool-1-thread-1 TRACE AbstractConfiguration stopped 1 
   Appenders.
...,674 pool-1-thread-1 TRACE AbstractConfiguration stopped 2 
   LoggerConfigs.
...,674 pool-1-thread-1 DEBUG Stopped XmlConfiguration[location
   =fyicenter\log4j2_api_trace_config.xml] OK
...,674 pool-1-thread-1 DEBUG Stopped LoggerContext[name=4687246, 
   org.apache.logging.log4j.core.LoggerContext@1c51ef0]...

The above session shows that how the Log4j 2 engine got started and stoped. It is very useful to find out why your Log4j 2 configuration is not working.

Back to FAQ for Apache Log4j 2.4.1.

2015-11-22, 2982👍, 1💬