I am new to log4j and trying to use log4j programmatically instead of using log4j.xml OR log4j.properties file.
But when I am running the code, It is only logging Fatal and Error messages and skipping other messages.
I have tried changing "rootlogger.setLevel(Level.DEBUG);" to "rootlogger.setLevel(Level.All);" but it is giving same output.
Could someone please help here?
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;
import org.apache.log4j.ConsoleAppender;
public class ProgrammaticLog4jExample {
private static final Logger logger = Logger.getLogger(ProgrammaticLog4jExample.class);
public static void main(String[] args) {
// creates pattern layout
String conversionPattern = "%-7p %d [%t] %c %x - %m%n";
PatternLayout layout = new PatternLayout(conversionPattern);
// creates console appender
ConsoleAppender consoleAppender = new ConsoleAppender();
consoleAppender.setLayout(layout);
consoleAppender.activateOptions();
// configure the root logger
Logger rootlogger = Logger.getRootLogger();
rootlogger.setLevel(Level.DEBUG);
rootlogger.addAppender(consoleAppender);
// creates a custom logger and messages
logger.debug("this is a debug log message");
logger.info("this is a information message");
logger.warn("this is a warning message");
logger.fatal("this is the fatal message");
logger.error("This is the error message");
}
}
output:
19:07:58.003 [main] FATAL log4jExample.ProgrammaticLog4jExample - this is the fatal message
19:07:58.006 [main] ERROR log4jExample.ProgrammaticLog4jExample - This is the error message
output Image:
I am using below jar files for log4j in my project classpath:
Aha...After doing some more experiments, I have got the answer.
Added "logger.setLevel(Level.DEBUG);" and I am able to get all the logs now. :-)
import org.apache.log4j.ConsoleAppender;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;
public class ProgrammaticLog4jExample {
private static final Logger logger = Logger.getLogger(ProgrammaticLog4jExample.class);
public static void main(String[] args) {
// creates pattern layout
String conversionPattern = "%-7p %d [%t] %c %x - %m%n";
PatternLayout layout = new PatternLayout(conversionPattern);
// creates console appender
ConsoleAppender consoleAppender = new ConsoleAppender();
consoleAppender.setLayout(layout);
consoleAppender.activateOptions();
// configure the root logger
Logger rootlogger = Logger.getRootLogger();
rootlogger.setLevel(Level.DEBUG);
rootlogger.addAppender(consoleAppender);
logger.setLevel(Level.DEBUG);
// creates a custom logger and messages
// Logger logger = Logger.getLogger(ProgrammaticLog4jExample.class.getName());
logger.debug("this is a debug log message");
logger.info("this is a information message");
logger.warn("this is a warning message");
logger.fatal("this is the fatal message");
logger.error("This is the error message");
}
}
Output Image:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.