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.
If you prefer XML, please refer to this log4j.xml example.
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
putting Line number (%L) into the ConversionPattern is brillant tips : thanks!
Excellent article. Thanks for sharing!
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
Hi, yong
I need to set the logger level ( rootLoggerObj.setLevel(Level.DEBUG);) in properties file
can you please give some idea
Hi How would you set the encoding for log file in the properties file.
How to write the log4j. prop file for using log4j-2* version.
Hi, please provide us with an example for netbeans + jax-rs + jersey (+ jackson) + log4j2.
Thanks
Hi all where can we find this properties file or where to append this file in a java project? Kindly help
@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”
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.
thanks
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.
Hi Mkyong,
Is there a way to configure log4j properties file as to append ‘Locale’ alongside date ?
Thanks in Advance
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.
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
SImple and nice example! Here is online tester for patterns: http://log4jtester.com/?p=%25d%7Byyyy-MM-dd+HH%3Amm%3Ass%7D+%25-5p+%25c%7B1%7D%3A%25L+-+%25m%25n
Thanks
Thank you much!
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.
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
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
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
El Chinoooo Sabeeeeee!
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
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
Thank you so much. It saved me a lot of trouble. After a lot of struggle, got it correct from your website.
most valuable! thanks
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.
Te rifas bien pinche chinito. Thanks a lot.
chinorris como te las sabes todas. You are welcome!
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?
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.