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, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.

Comments

Leave a Reply

avatar
newest oldest most voted
Brice
Guest
Brice

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

selami ozlu
Guest
selami ozlu

Thanks

sreenivas
Guest
sreenivas

Hi all where can we find this properties file or where to append this file in a java project? Kindly help

Brice
Guest
Brice

@sreenivas : you can
– put it into “src/main/resources” (result in WEB-INF/classes for a WAR – WebApp archive),
– or in your classpath,
– or set it ; example: “-Dlog4j.configuration=file:..myDirectorylog4j.properties”

pavan
Guest
pavan

Hi All, i have a requirement where the maxFileSize of the log should be 10 MB size, and maxBackupIndex should be taken dynamically. The logs should be rolled back daily when the system date changes and new logs should be created for the next day. Is it possible to use both the techniques in a single configuration file and implement the scenario. Can any one help me on this.

Brian
Guest
Brian

thanks

aryan singh
Guest
aryan singh

if i am setting log4j.rootLogger=ERROR I am getting following warning

log4j:WARN No appenders could be found for logger (com.trianz.logs.HelloExample).

log4j:WARN Please initialize the log4j system properly.

log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

Devang Bagora
Guest
Devang Bagora

Hi Mkyong,

Is there a way to configure log4j properties file as to append ‘Locale’ alongside date ?

Thanks in Advance

Robinson Gajardo
Guest
Robinson Gajardo

Hi, I have a challenge with the log4j. I need to filter some specific words before be written to the log. I have written a function in java that replace that words with asterisks but i don´t know how to instruct the logger that calls my routine before write to the log. My function accept 2 parameters as input, the message and the word to be replaced and returns the message filtered.

Any ideas to accomplish this

Thanks in advance.

Mohd Faiz Md Johan
Guest
Mohd Faiz Md Johan

Hi Mkyong,
i have a requirement to make the log to be rolling over after reaching a maxfilesize. the log4j.properties was set to be rollover after 100MB. The log manage to roll but the archieve for the log was not to be seen in the folder. The log keep been written to the same file even after rooling over. i am using the log4j 1.2.8. is there any missing parameter in below code.

### LOG4J TRANSACTION
log4j.logger.log4j_FE_BR=INFO, log4j_FE_BR
log4j.additivity.log4j_FE_BR=false
log4j.appender.log4j_FE_BR=org.apache.log4j.RollingFileAppender
log4j.appender.log4j_FE_BR.File=C:/eai_logs/LOG4J/log4j_FE-BR.log
log4j.appender.log4j_FE_BR.MaxFileSize=100MB
log4j.appender.log4j_FE_BR.MaxBackupIndex=5
log4j.appender.log4j_FE_BR.DatePattern=’.’yyyy-MM-dd
log4j.appender.log4j_FE_BR.layout=org.apache.log4j.PatternLayout
log4j.appender.log4j_FE_BR.layout.ConversionPattern=%d{ISO8601} %m%n

Thanks & Regards,
FaizJohan

Dániel Hári
Guest
Dániel Hári
Mateen
Guest
Mateen

Thanks

trackback
Spring MVC + Log4j integration example

[…] log4j.properties examples […]

thecodefather
Guest
thecodefather

Thank you much!

Naveen
Guest
Naveen

From application, i print using sysout. those do not show in file. i am able to see on console only.
Can you please let me how can i print those statements in a FIle.

Anand Kumar
Guest
Anand Kumar

Thanks for your information and its very helpful..
Also find the post with multiple log appender – http://javadiscover.blogspot.in/2013/10/log4j-multiple-appender-example.html

boumbh
Guest
boumbh

I was searching for the syntax of level setting based on class / package… I found it here:
http://www.sourcetricks.com/2014/02/log4j-properties-configuration-examples.html

markzona
Guest
markzona

The information is probably not up-to-date anymore. I wasn’t able to run log4j until I used a file called log4j2.xml in src/main/resources (Maven project). It seems that .properties cannot be used anymore. See also http://logging.apache.org/log4j/2.x/faq.html

Tiki Taka2
Guest
Tiki Taka2

El Chinoooo Sabeeeeee!

Aviram
Guest
Aviram

Hi Mykong.
Thanks for the useful information, i use your website for my projects.
I need that my log file will be replaced by a new one if its size is more than 1 MB or
the file creation date is more than 5 minutes.
for the size issue i’m using the “MazFileSize” property .
do you know what should i use for my second condition ?

Thanks
Aviram

deepesh
Guest
deepesh

can u share the code for checking the file size which you are using so that I can have a look as I am trying to achiever the same but t’s not happening.

TIA
Deepesh

Kruger Brent
Guest
Kruger Brent

Thank you so much. It saved me a lot of trouble. After a lot of struggle, got it correct from your website.

Marco Bolis
Guest
Marco Bolis

most valuable! thanks

trackback
| ??????
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.

Alfonso Baqueiro
Guest
Alfonso Baqueiro

Te rifas bien pinche chinito. Thanks a lot.

tiki y taka
Guest
tiki y taka

chinorris como te las sabes todas. You are welcome!

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?

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

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.

ankit
Guest
ankit

thanks

jigsy.com
Guest
jigsy.com

When I originally commented I clicked the “Notify me when new comments are added” checkbox and now each time a comment is added I get several e-mails with the same comment.
Is there any way you can remove me from that service? Thanks a lot!

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?

Subodh
Guest
Subodh

You can get tutorials on java and j2ee on http://techmyguru.com.

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.