Log4j 2. How get log4j's debug messages?
Asked Answered
T

4

17

As far as i understand log4j can handle system property -Dlog4j.debug. If you run your app with it you will get log4j's debug output.

Example: java -Dlog4j.debug -jar test.jar

Is there something similar for log4j 2?

Townsfolk answered 24/10, 2013 at 19:4 Comment(1)
java -Dlog4j2.debug -jar test.jarHie
O
14

Update January 2018:

From Log4j 2.10, this is easy: just run your program with system property log4j2.debug (no value needed; an empty string is fine).


The current (log4j-2.1) documentation on the status logger is a bit confusing. Basically:

  • Until a configuration is found, status logger level can be controlled with system property org.apache.logging.log4j.simplelog.StatusLogger.level.
  • After a configuration is found, status logger level can be controlled in the configuration file with the "status" attribute, for example: <Configuration status="trace">.

UPDATE: the documentation was improved in log4j-2.2.

Olmos answered 19/2, 2015 at 3:39 Comment(0)
C
6

It can be confusing, the nearest equivilent of the Log4J 1.x command line argument -Dlog4j.debug is -Dorg.apache.logging.log4j.simplelog.StatusLogger.level=trace which sets the Log4J 2.x "status logger" level to trace and provides detailed output about the logging configuration.

Log4J 1.x allows you to manually specify the location of the configuration file on the command line using -Dlog4j.configuration=file:///var/lib/tomcat7/log4j.xml where the configuration file is located at /var/lib/tomcat7/log4j.xml. In Log4J 2.x there is a subtle difference in the argument -Dlog4j.configurationFile=file:///var/lib/tomcat7/log4j.xml, 'configurationFile' rather than 'configuration'.

Obviously you need to ensure that your configuration file is suitible for the version of Log4J being used, the XML structure differs between 1.x and 2.x.

Cedrickceevah answered 13/5, 2015 at 10:17 Comment(0)
R
1

I've had a frustrating amount of difficulty getting Log4J2 up and running, and printing the StatusLogger is no exception. In theory you can set it in the config file with the status field, however I've not been able to make that work thusfar.

You can run the following at the beginning of your main method however:

StatusConsoleListener listener = new StatusConsoleListener(Level.ALL);
StatusLogger.getLogger().registerListener(listener);
LogManager.getLogger(LogManager.ROOT_LOGGER_NAME); // initialize logger

Note your main() class cannot have any static Loggers, or they'll be initialized before this is called, meaning the loading status messages won't print.

Rayon answered 9/12, 2013 at 6:51 Comment(0)
A
0

In case someone needs to set DEBUG level programmatically

// for your custom logger
Configurator.setLevel("com.name.of.logger", Level.DEBUG);
// for root logger
Configurator.setRootLevel(Level.DEBUG);

OR without imports

org.apache.logging.log4j.core.config.Configurator.setLevel(
    "com.name.of.logger", 
    org.apache.logging.log4j.Level.DEBUG
);
Assess answered 27/5, 2020 at 17:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.