Main Tutorials

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 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
70 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Farrukh
10 years ago

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
6 years ago

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

Pramod
3 years ago

Excellent article. Thanks for sharing!

darshan muchhadiya
4 years ago

Another Sample Log4j.Properties File :

// Here we have defined root logger
log4j.rootLogger=INFO,CONSOLE,R,HTML,TTCC

// Here we define the appender
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.TTCC=org.apache.log4j.RollingFileAppender
log4j.appender.HTML=org.apache.log4j.FileAppender

// Here we define log file location
log4j.appender.R.File=./log/testlog.log
log4j.appender.TTCC.File=./log/testlog1.log
log4j.appender.HTML.File=./log/application.html

// Here we define the layout and pattern
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern= %5p [%t] (%F:%L)- %m%n
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%d – %c -%p – %m%n
log4j.appender.TTCC.layout=org.apache.log4j.TTCCLayout
log4j.appender.TTCC.layout.DateFormat=ISO8601
log4j.appender.HTML.layout=org.apache.log4j.HTMLLayout
log4j.appender.HTML.layout.Title=Application log
log4j.appender.HTML.layout.LocationInfo=true

ABhishek
4 years ago

Hi, yong
I need to set the logger level ( rootLoggerObj.setLevel(Level.DEBUG);) in properties file
can you please give some idea

Niki
4 years ago

Hi How would you set the encoding for log file in the properties file.

Prabhu
4 years ago

How to write the log4j. prop file for using log4j-2* version.

Chris Ndirangu
5 years ago

Hi, please provide us with an example for netbeans + jax-rs + jersey (+ jackson) + log4j2.

selami ozlu
6 years ago

Thanks

sreenivas
6 years ago
Reply to  selami ozlu

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

Brice
6 years ago
Reply to  sreenivas

@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
7 years ago

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
8 years ago

thanks

aryan singh
8 years ago

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
8 years ago

Hi Mkyong,

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

Thanks in Advance

Robinson Gajardo
8 years ago

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
8 years ago

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
8 years ago
Mateen
8 years ago

Thanks

thecodefather
8 years ago

Thank you much!

Naveen
8 years ago

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
8 years ago

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
9 years ago

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
9 years ago

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
9 years ago

El Chinoooo Sabeeeeee!

Aviram
9 years ago

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
9 years ago
Reply to  Aviram

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
9 years ago

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

Marco Bolis
10 years ago

most valuable! thanks

Keith McNeill
10 years ago

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
10 years ago

Te rifas bien pinche chinito. Thanks a lot.

tiki y taka
9 years ago

chinorris como te las sabes todas. You are welcome!

Joseph
10 years ago

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?

deep
10 years ago

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.