In the previous post of the Log4j tutorial series, I wrote about the very basics of adding Log4j logging to your project. In this post, I’ll explain how to create a FileAppender to log messages into a file, instead of console.
Log4j FileAppender – example for Logging messages to a file:
In most cases, either RollingFileAppender or DailyRollingFileAppender classes are used in production scenarios for logging messages into a file. A RollingFileAppender will send all the log messages to the configured file, until the file reaches a certain size. Once the log file reaches a maximum size, the current file will be backed up and a new log file will be created (thus the name RollingFileAppender). The DailyRollingFileAppender is also similar to RollingFileAppender except that the rolling happens at given frequency. Below is an example for using RollingFileAppender
- Add Log4j logging support to your project. Read this article for how to do it.
- In your log4j.properties file, add below lines:
log4j.appender.rollingFile=org.apache.log4j.RollingFileAppender log4j.appender.rollingFile.File=D:/myapp/mylog.log log4j.appender.rollingFile.MaxFileSize=2MB log4j.appender.rollingFile.MaxBackupIndex=2 log4j.appender.rollingFile.layout = org.apache.log4j.PatternLayout log4j.appender.rollingFile.layout.ConversionPattern=%p %t %c - %m%n
- As you can see from the above configuration, we are creating a new appender in the name of rollingFile and setting it’s options. I guess most of the options are self explanatory. The MaxBackupIndex tells the log4j to keep a maximum of 2 backup files for the log mylog.log. If the log file is exceeding the MaximumFileSize, then the contents will be copied to a backup file and the logging will be added to a new empty mylog.log file. From the above configuration, there can be a maximum of 2 backup files can be created.
- Now the appender is created. Next we need to direct our logs to go into this appender. Let’s direct all the logs to go into this appender.
log4j.rootLogger = INFO, rollingFile
- That’s it. From now on, when you log something from your Java class, the log message will go into the mylog.log file.
Next: How to send log messages to different log files?
Previous: Adding Log4j logging to your project.
{ 4 trackbacks }
{ 12 comments }
Thansk for this simple and quick tutorial. It helped me lot in recollecting my basics.
Also are there anymore parts of this tutorial to be published?
Thanks chaitanya.
Yes. One more part is left, which I’ll be publishing soon.
Hi Veera,
How to log the file to the context path folder; To be clear, let say ‘xxxx’ is my context path, which maps to ‘myApp/web/’ and my log file has to be logged in to ‘myApp/web/logs/Log.log’…. hope you would have understand what i need….
Please let me know your thoughts…
Regards,
Prabhu.
Hi iam currently using log4j.xml to configure my Standalone Application, this is not a Web Application .My requirement is loggs should be rolledover on DailyBases , but here i made it to be minute wise. And here is my code
> http://jakarta.apache.org/log4j/“>
in this Rolling the logg file is happening in correct way according to DatePattern which i set, lke it could be day or minute but here the problem is iam unable to see the old backup logger file. So Veera Sundar please help me as soon as possible.. ..
Thanks
Satyanaryana
here is below my code
<!–
–>
using log4j the file is created but not writing logs into the file.
@Ramya
Could you please eloborate more on the problem!?
Veera, small correction in the above code.
In the point 4, for log4j.rootLogger , the second value should be simply rollingFile
not rollingFileAppender.
So final code is:
log4j.rootLogger = INFO, rollingFile
Regards
Chaitanya
@Chaitanya
Thank you for notifying the mistake. Corrected it.
i think you lost 'layout' in line log4j.appender.rollingFile.ConversionPattern=%p %t %c – %m%n
log4j.appender.rollingFile.layout.ConversionPattern=%p %t %c – %m%n
yes. my bad.
thanks for noticing it.
thanks! nice written and easy to understand!
Comments on this entry are closed.