Let us discuss the use of Log4j with the help of below example. In this example we use log4j.properties file for Log4j configurations.
Example:
Log4jTest.java
import org.apache.log4j.Logger; /** * This class is used to show the use of * Log4j with the log4j.properties file. * @author w3schools */ public class Log4jTest { //Get the Logger object. private static Logger log = Logger.getLogger(Log4jTest.class); public static void main(String[] args) { //logger messages log.debug("Log4j debug message test."); log.info("Log4j info message test."); } } |
log4j.properties
#log4j.rootCategory=INFO,DEBUG,A1,LFS log4j.rootLogger=DEBUG, CA #Set Console Appender log4j.appender.CA=org.apache.log4j.ConsoleAppender #CA uses PatternLayout log4j.appender.CA.layout=org.apache.log4j.PatternLayout log4j.appender.A1.layout.ConversionPattern=%m%n |
Output:
Log4j debug message test.
Log4j info message test. |
Download this example.
Next Topic: Log4j example using log4j xml file.
Previous Topic: Log4j example.