Main Tutorials

log4j.xml Example

Here’s an XML version of log4j properties file, just for sharing.

1. Output to Console

Redirect the logging to console.

log4j.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!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">
	    <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>
		<level value="DEBUG" />
		<appender-ref ref="console" />
	</root>

</log4j:configuration>

2. Output to File

Redirect the logging to a file..

log4j.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration debug="true"
	xmlns:log4j='http://jakarta.apache.org/log4j/'>

	<appender name="file" class="org.apache.log4j.RollingFileAppender">
	   <param name="append" value="false" />
	   <param name="maxFileSize" value="10KB" />
	   <param name="maxBackupIndex" value="5" />
	   <!-- For Tomcat -->
	   <param name="file" value="${catalina.home}/logs/myStruts1App.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>
		<level value="ERROR" />
		<appender-ref ref="file" />
	</root>

</log4j:configuration>

An example of log4j rolling files.

struts1-log4j-file

3. Output to Console and File

Full example to output the logging to both console and file.

log4j.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!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">
	    <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="file" class="org.apache.log4j.RollingFileAppender">
	    <param name="append" value="false" />
	    <param name="maxFileSize" value="10MB" />
	    <param name="maxBackupIndex" value="10" />
	    <param name="file" value="${catalina.home}/logs/myStruts1App.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>
		<level value="DEBUG" />
		<appender-ref ref="console" />
		<appender-ref ref="file" />
	</root>

</log4j:configuration>

References

  1. Log4j 1.2 Manual
  2. Log4j PatternLayout JavaDoc
  3. log4j.properties examples

About Author

author image
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

Subscribe
Notify of
24 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Kumaran
4 years ago

can i have the sample of log4j.xml for HttpAppender with HTTP POST request

Jesse Zhou
7 years ago

Where to put log4j.xml file?

kong789
4 years ago
Reply to  Jesse Zhou

put it in your project root folder

Michael
3 years ago

Hi, when trying to do this I get

20:45:23,493 FATAL [main] Main – getAssociatedStylesheets failed
org.eclipse.wst.xsl.jaxp.debug.invoker.TransformationException: getAssociatedStylesheets failed

what can I do to have it working?
I work in Eclipse..

Patrick Tolentino
5 years ago

How do you use it? What to code in a java class to use the log4j xml file?

rnallamothu
6 years ago

i have a pecular problem.
i am using log4j1.15 version. can i change the .log file based on the project name because i have multiple wars on one tomcat. and should use a common log4j.xml file.
By using what feature can i resolve this issue.
i tried with System.setProperty featured. it didnt work.
My projects or JSF and struts based application.
can you please help out .
Thanks and regard
Rnalllamothu

rnallamothu
6 years ago

i have a pecular problem.
i am using log4j1.15 version. can i change the .log file based on the project name because i have multiple wars on one tomcat. and should use a common log4j.xml file.
By using what feature can i resolve this issue.
i tried with System.setProperty featured. it didnt work.
My projects or JSF and struts based application.
Thanks and regard
Rnalllamothu

Kiran
6 years ago

I am using Toamcat with windows machine. RollingAppender doesn’t roll to create a new file. Instead it just overwrites the older one. Is it require any permission to create new backup file?

Kalpana Ramanan
7 years ago

If I am configuring using Jboss will the below code works ?

will the above works if it is Jboss server

Fahad Ahmad
7 years ago

thanks for it,
in my scenario, i want to write logger.info in console and .log file, but it is writing DEBUG,ERROR,WARNING but not
INFO, anyone have any idea?
Thank you,

Rehman
1 year ago
Reply to  Fahad Ahmad

you need to set the start level are info=true, instead of Debug=true.
Priority levels are Info<Debug<Warn<Error<Fatal

ketansing pawar
7 years ago

hello sir ,
I am able to generate log files on windows but not able to generate it on Ubuntu ? please help me .

Jaydev
7 years ago

Is it possible to configure a date wise RollingFileAppender with this ?

Boom3k
7 years ago

Everything is working for me except the appender doesn’t roll to create a new file. Instead it just overwrites the older one.

Jagruti Frank
8 years ago

Hi Mkyong, i have one issue, it created the file .log.1 and .log.2 but they all are deleted once maxBackupIndex is reached.

I am not getting any previous files, is there any particular config?
Thank you

Henrri Trujillo Romero
8 years ago

Hi, I’m Spanish speaker.
The log4j.xml file, how do you charge? because until now I used the log4j.properties in each project and the idea is to use only one log4j.xml for multiple projects , I’m using Apache Tomcat as the application server . I hope you can help me .

Sebastian Szlachetka
8 years ago

Thanks. You made my day 🙂

Raghavendra Mudenur
8 years ago

Thank you , it helped me a lot as I am new to Log4j. i used “Output to console and file” . Now I am working on a project which uses hibernate , I want all hibernate related INFO logs to be stored in file how can I do that can you help me please. and Here I also get some information about Log4j , How can I hide that too.

Gourav jain
9 years ago

Nothing is displaying in my log text file.

Valentine Golubev
9 years ago

nothing about specific class logging

Rahul
9 years ago

When multiple files are generated they do not open. We explicitly have to right click it, then select open with notepad. Is there a way to avoid this. This problem occurs because it appends 1,2…. at the end for new file

Confused
9 years ago

I have exactly what you have here and I only see log output to a file. Nothing appears in the console.

Gaurav Singh
9 years ago

what is ${catalina.home} here.
I need to redirect the log file on my hosting site location

Boom3k
7 years ago
Reply to  Gaurav Singh

${catalina.home} is a reference to your Tomcat home directory. That line can be changed, example: he uses “${catalina.home}/logs/myStruts1App.log” but I use “d:/devstuff/logs/application.log”.