Let us discuss the use of Log4j with the help of below example. In this example we use BasicConfigurator for Log4j configurations. BasicConfigurator use ConsoleAppender and PatternLayout for all loggers.

Example:

Log4jTest:

importorg.apache.log4j.BasicConfigurator;importorg.apache.log4j.Logger;/**
* This class is used to show the use of
* Log4j with the BasicConfigurator.
* @author codesjava
*/publicclass Log4jTest {//Get the Logger object.privatestatic Logger log = Logger.getLogger(Log4jTest.class);publicstaticvoid main(String[] args){//Configuring Log4j,It will log all messages on console.//BasicConfigurator use ConsoleAppender and PatternLayout //for all loggers.
BasicConfigurator.configure();//logger messages
log.debug("Log4j debug message test.");
log.info("Log4j info message test.");}}