Log4j Configuration:

          Configuring log4j involves assigning the LEVEL,defining Appender, and specifying Layout objects in a configuration file.

1)BasicConfigurator

2)PropertyConfigurator

3)DomConfigurator

BasicConfigurator:

If are do not create a property file or xml file for log4j properties then we use BasicConfigurator class.

The BasicConfigurator.configure() method by default it uses the  Console Appender and Pattern Layout for log statements.

Program:

package com.dustbin.test;

import org.apache.log4j.Logger;

public class Sample {
static Logger logger=Logger.getLogger(Sample.class);
public static void main(String[] args) {

logger.info("info message");
logger.warn("warn message");
logger.error("error message");
logger.fatal("fatal message");
logger.debug("debug msg");

}

}

Output:
We got an exception like…

log4j:WARN No appenders could be found for logger (com.dustbin.test.Sample).
log4j:WARN Please initialize the log4j system properly.

0 comments:

Post a Comment