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.
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.
putting Line number (%L) into the ConversionPattern is brillant tips : thanks!
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, @mkyong. I am using Ubuntu and in the file path “log4j.appender.file.File=C:\logging.log” I am passing “log4j.appender.file.File=var/log/name/appManager/logging.log”. But no logging file was generated. Am I doing something wrong?
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?
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
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.
thanks
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?
You can get tutorials on java and j2ee on http://techmyguru.com.
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.
Thanks, your examples helped me a lot.
This is the best site to get the instant help on Java
explain me abt log4j properties?
Thank you so much for sharing this! It really help me.
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 ?
Its simple actually , even without logger :
PrintStream psconsoleOut = new PrintStream(System.out);
PrintStream psconsoleOutErr = new PrintStream(System.err);
FileOutputStream fos = new FileOutputStream(new File(“console.txt”));
PrintStream psfile = new PrintStream(fos);
System.setOut(psfile);
System.setErr(psfile);
All System.out and System.err will go directly to console.txt
when finally done , set it back :
System.setOut(psconsoleOut);
System.setErr(psconsoleOutErr);
NIce……
It’s not working.
Then you have not done something right .
Yesterday, while I was at work, my cousin stole
my iPad and tested to see if it can survive a twenty five foot
drop, just so she can be a youtube sensation. My apple ipad is now destroyed and she has 83 views.
I know this is completely off topic but I had
to share it with someone!
tnx
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.
Have you even used log4j ?
put logging line in your JDBC java code or use aspect j to inject log lines
Hi,
can anyone explain the meaning of below statement?
log4j.appender.file.MaxBackupIndex=20
Thanks,
Apply your mind . The answer is already in the line you posted .
Wow, you’re a real help. Great tip.
Very helpful, not. Why do you even bother …
and what it mean?
Thanks for the quick and simple example. And congrats for showing up #1 on Google for “log4j.properties example file”
Mark
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.
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
Yes , you simply use different loggers or dynamically change logger properties !!
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
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
*** Please give a simple example to integrate Spring and Log4j ***
OK i will give you ..
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; }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
Hi, nice info, thanks for the tips 🙂
thank you mk yong