Introduction:
ALL, DEBUG, INFO, WARNING, ERROR, FATAL, OFF, TRACE
Follow the steps to apply the log4j to the java application
Here is my java class
Here is my log4j.xml
Here is my pom.xml file
After applies to the java class output log file is here below
- It is used to view the logging process of your application.
- It has loggers to hold the information, appenders to store logging information in different files, layouts to change the format of an output log file.
- The PatternLayout object is used to display your logging information in a specific pattern
- DailyRollingFileAppender class is used to write the logging information of an application into a file on a daily basis.
ALL, DEBUG, INFO, WARNING, ERROR, FATAL, OFF, TRACE
Here is my java class
package com.java; import org.apache.log4j.BasicConfigurator; import org.apache.log4j.Logger; import org.apache.log4j.xml.DOMConfigurator; /** * * @author kalpana podamekala */ public class LogFileExample { static Logger logger = Logger.getLogger(LogFileExample.class); public static void main(String[] args) { //DOMConfigurator is used to configure logger from xml configuration file DOMConfigurator.configure("log4j.xml"); BasicConfigurator.configure(); //Log in console in and log file logger.debug("Log4j appender configuration is successful !!"); } }
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd"> <log4j:configuration debug="true" xmlns:log4j='http://jakarta.apache.org/log4j/'> <appender name="console" class="org.apache.log4j.ConsoleAppender"> <param name="Target" value="System.out"/> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n" /> </layout> </appender> <appender name="fileAppender" class="org.apache.log4j.RollingFileAppender"> <param name="File" value="demoApplication.log"/> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n" /> </layout> </appender> <root> <priority value ="debug"></priority> <appender-ref ref="console"></appender-ref> <appender-ref ref="fileAppender"></appender-ref> </root> </log4j:configuration>
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.mycompany</groupId> <artifactId>LogFile</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> </dependencies> </project>