Put log4j.xml or log4j.xml file in the resource folder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2019-07-30 16:00:09 DEBUG Log4jXmlConfigurationExample:18 - Log4j appender configuration is successful !! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Root logger option | |
log4j.rootLogger=DEBUG, stdout, file | |
# Redirect log messages to console | |
log4j.appender.stdout=org.apache.log4j.ConsoleAppender | |
log4j.appender.stdout.Target=System.out | |
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout | |
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n | |
# Redirect log messages to a log file, support file rolling. | |
log4j.appender.file=org.apache.log4j.RollingFileAppender | |
log4j.appender.file.File=C:\\Users\\diya-mutturajh\\test123\\Logs\\log4j-application.log | |
log4j.appender.file.MaxFileSize=5MB | |
log4j.appender.file.MaxBackupIndex=10 | |
log4j.appender.file.layout=org.apache.log4j.PatternLayout | |
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package test1231; | |
import org.apache.log4j.Logger; | |
import org.apache.log4j.xml.DOMConfigurator; | |
public class Log4JExample | |
{ | |
static Logger logger = Logger.getLogger(Log4JExample.class); | |
public static void main(String[] args) | |
{ | |
//DOMConfigurator is used to configure logger from xml configuration file | |
DOMConfigurator.configure("C:\\Users\\mutturajh\\test123\\src\\test\\java\\test1231\\log4j.xml"); | |
//Log in console in and log file | |
logger.debug("Log4j appender configuration is successful !!"); | |
} | |
} |