log4j.properties example

I can’t find many log4j.properties examples, here are a few log4j.properties examples that are used in my project, just for sharing.

1. Output to Console

All logging will be redirected to your console.

log4j.properties

# Root logger option
log4j.rootLogger=INFO, stdout

# Direct log messages to stdout
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
 

3. Output to File

All logging will be redirected to your specified log file.

log4j.properties

# Root logger option
log4j.rootLogger=INFO, file

# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender

#Redirect to Tomcat logs folder
#log4j.appender.file.File=${catalina.home}/logs/logging.log

log4j.appender.file.File=C:\\logigng.log
log4j.appender.file.MaxFileSize=10MB
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

3. Output to Console and File

All logging will be redirected to both log file and console.

log4j.properties

# Root logger option
log4j.rootLogger=INFO, file, stdout

# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=C:\\logging.log
log4j.appender.file.MaxFileSize=10MB
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
 
# Direct log messages to stdout
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
Note
If you prefer XML, please refer to this log4j.xml example.

References

  1. Log4j manual
  2. Log4j PatternLayout manual

About the Author

author image
mkyong
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

avatar
54 Comment threads
19 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
63 Comment authors
ABhishekNikiPrabhuChris NdiranguBrice Recent comment authors
newest oldest most voted
Farrukh
Guest
Farrukh

Any idea why I am getting below warning on my glassfish console
SEVERE: log4j:WARN No appenders could be found for logger (org.displaytag.properties.TableProperties).

I have log4j in my classes folder

log4j.rootLogger=DEBUG, testAppender
log4j.appender.testAppender=com.webmethods.sc.logging.log4j.WmDailyFileAppender
log4j.appender.testAppender.file=log/test1.log
log4j.appender.testAppender.datePattern=’.’yyyy-MM-dd_HH_mm
log4j.appender.testAppender.Append=false
log4j.appender.testAppender.layout=com.webmethods.sc.log4j.PatternLayout
log4j.appender.testAppender.layout.ConversionPattern=%m%n

#Prevent internal log4j DEBUG messages from polluting the output.
log4j.logger.org.apache.log4j.PropertyConfigurator=INFO
log4j.logger.org.apache.log4j.config.PropertySetter=INFO
log4j.logger.org.apache.log4j.FileAppender=INFO

Brice
Guest
Brice

putting Line number (%L) into the ConversionPattern is brillant tips : thanks!

anjan bacchu
Guest
anjan bacchu

hi there,

thanks for the post.

Google can be a good example for searching for a file type with an extension.

You can type “log4j.properties filetype:properties” into the google search box(without the quotes) and you will get quite a few matches.

example

BR,
~A

trackback
Struts + Log4j integration example | Struts

[…] log4j.properties examples – http://www.mkyong.com/logging/log4j-log4j-properties-examples/ […]

trackback
Struts 2 + Log4j integration example

[…] 2. Log4j Appender Create a text file named “log4j.properties“, put it at the root of the project classpath. The log4j.properties or appender file is the Log4j configuration file, it defines how the Log4j logging mechanism work. See more Log4j appender examples. […]

User1
Guest
User1
 

public static String replaceWord(String original, String find, String replacement) {
	    int i = original.indexOf(find);
	    if (i < 0) {
	        return original;  // return original if 'find' is not in it.
	    }
	    String partBefore = original.substring(0, i);
	    String partAfter  = original.substring(i + find.length());
	  
	    return partBefore + replacement + partAfter;
	}



 
veeraababu
Guest
veeraababu

*** Please give a simple example to integrate Spring and Log4j ***

joe
Guest
joe

Hello,

How do I get tomcat logs (access and error)(as well as for webapps) to go syslog?
I have syslog configured to go to a syslog server. Ideally I want the logs I specified to to go that same syslog server. (Syslog server is ArcSight)

Thanks
Joe

Tien Thai
Guest
Tien Thai

My hibernate binding parameters does show in the log when run in Tomcat and WebSphere application 6.1 but does not show in WebSphere 7. By any chance, would you please give me any suggestion you might have. Your help is appreciated.

Tien Thai

Anoumous
Guest
Anoumous

Hi,
I liked your blog.
I have a query – is there any way to log the different methods of a class into different files? (same class I am asking about)

Please do let me know if there is any solution.

Thanks in advance

Autoescuela en Sevilla
Guest
Autoescuela en Sevilla

Thank you Mkyong, I was looking for something similar in order to use with Log4QT.(I think is the same), and I can’t find any esasy example!

Thanks again.

Mark
Guest
Mark

Thanks for the quick and simple example. And congrats for showing up #1 on Google for “log4j.properties example file”

Mark

Vishal
Guest
Vishal

Hi,

can anyone explain the meaning of below statement?

log4j.appender.file.MaxBackupIndex=20

Thanks,

trackback
log4j Tutorial « Slon’s Blog

[…] log4j.properties example […]

satish
Guest
satish

Sir good morning,i have problem with jdbc code with log4j i.e:
when particular operation is executed in DATA BASE TABLE i want to print log messages to a particular log file daily sir can u tell how do it.

BiH Java Developer
Guest
BiH Java Developer

tnx

boo
Guest
boo

It’s not working.

Nitin
Guest
Nitin

hello mkyong,

Thanks for this information. Actually I’m also logging to both stdout & file.
But I want to redirect stdout & stderr to some file, not to console!
How can I achieve this ?

Kevin Sheppard
Guest
Kevin Sheppard

Thank you so much for sharing this! It really help me.

harry
Guest
harry

explain me abt log4j properties?

Raghunadh Parlapalli
Guest
Raghunadh Parlapalli

This is the best site to get the instant help on Java

Michael
Guest
Michael

Thanks, your examples helped me a lot.

prateek
Guest
prateek

Hi
I am using no. of jars as dependencies in project. How can i redirect the logging of some jars in a file and others in some different files? Please suggest.

ppi reclaim
Guest
ppi reclaim

I know this website offers quality dependent posts and additional stuff, is there any other web site which offers these kinds of stuff in quality?

ankit
Guest
ankit

thanks

deep
Guest
deep

What is Console in terms of Linux.

I am facing a weird situation wherein the required spring framework logs are getting logged in Eclipse console but not in File appender files in windows. Some other logs gets logged. its only spring related which are not logged in File but gets logged in eclipse Console.

In my linux production box, I can similarly see that in the Files the spring logs are not populated. So I want to check how about console logs.
Searched around quite a bit but need to unserstand where the console logs re present in Linux/Unix box.

Joseph
Guest
Joseph

I am using Log4j for selenium script, when I am running the script in eclipse it is properly giving the logs, but I am running the script in ANT not getting any log. can you please help me?

Alfonso Baqueiro
Guest
Alfonso Baqueiro

Te rifas bien pinche chinito. Thanks a lot.

Keith McNeill
Guest
Keith McNeill

Mykong: I just wanted to say a thank you generally for your fantastic examples and explanations. You have recently become my ‘Go To Guy’ whenever I don’t understand something Java related, as well as many other topics.

trackback
| ??????