Reputation: 6854
I was trying to implement a basic configurator via the following code
package com.myapp.loggingutilities;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.BasicConfigurator;
public class LoggingUtilitiesApplication {
static Logger currentLogger = Logger.getLogger(LoggingUtilitiesApplication.class);
public static void main(String[] args) {
BasicConfigurator.configure();
currentLogger.debug("Application Started here");
LoggerUtilityModel bar = new LoggerUtilityModel();
bar.doIt();
currentLogger.debug("Application ended");
}
}
The class def for bar
is
package com.myapp.loggingutilities;
import org.apache.log4j.Logger;
public class LoggerUtilityModel {
static Logger modelLogger = Logger.getLogger(LoggerUtilityModel.class);
public void doIt() {
modelLogger.debug("OPPS! DID IT AGAIN");
}
}
I am using Log4j2 and the distribution was taken here. I am always getting the SLF4J bridge error when I have the Log4j-to-slf4j and implementation jars in build path:
Exception in thread "main" java.lang.ExceptionInInitializerError
Caused by: java.lang.ClassCastException: org.apache.logging.slf4j.SLF4JLoggerContext cannot be cast to org.apache.logging.log4j.core.LoggerContext
at org.apache.log4j.Logger$PrivateManager.getContext(Logger.java:59)
When I removed them, it was okay. But the log messages were not being printed
I thought BasicConfigurator
ought to be configuring the root logger for DEBUG so all of my log statements should go through (i.e. anything on or above DEBUG level). But I cannot see any log messages on the console when I run the application. Something I have missed?
Upvotes: 1
Views: 5511
Reputation: 36834
BasicConfigurator
is a log4j 1.2 class and cannot be used to configure Log4j 2. Basically, everything in the org.apache.log4j
package is Log4j 1.2 (old) and everything in the org.apache.logging.log4j
namespace is for Log4j 2.
If you are interested in programmatic configuration of Log4j 2, please see the manual page. Personally I find the new simplified XML configuration file format easiest to work with.
The behavior you are seeing (only console logging at ERROR level) is Log4j 2's "default configuration", which is what it does if it can't find a configuration file and hasn't been manually configured.
Upvotes: 1
Reputation: 6854
The problem seems to be the LEVEL set for log4j2.6.2. As per Apache's Log4J-2.6.2 docs, all the default log messages are set for DEBUG.
Also, you CANNOT change the logging level using root logger. You need to change it per logger. Everything is defaulted to ERROR.
Upvotes: 0