Log4j Tutorial : How to send the log messages to a File

by Veera on July 28, 2009

in How To, Java

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

  1. Add Log4j logging support to your project. Read this article for how to do it.
  2. 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
  3. 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.
  4. 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
  5. 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.

Related Articles (System generated)

Follow me on Twitter to get notified whenever I update this blog.

{ 4 trackbacks }

Java Blog | Log4j Tutorial : How to send log messages to different log files? | Veerasundar
August 6, 2009 at 11:56 PM
Java Blog | Log4j Tutorial : Adding Log4j logging to your project | Veerasundar
August 7, 2009 at 12:00 AM
Log4j Tutorial: Additivity – what and why? - Veerasundar
August 14, 2009 at 10:21 PM
How to create a new log file for each time the application runs? - Veerasundar
August 19, 2009 at 9:43 PM

{ 12 comments }

chaitanya August 3, 2009 at 10:50 AM

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?

Veera August 4, 2009 at 12:57 AM

Thanks chaitanya.

Yes. One more part is left, which I’ll be publishing soon.

Prabhu August 31, 2009 at 7:38 PM

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.

satya September 8, 2009 at 3:24 PM

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

Satya September 8, 2009 at 3:26 PM

here is below my code

<!–

–>

Ramya September 15, 2009 at 10:04 AM

using log4j the file is created but not writing logs into the file.

Veera September 15, 2009 at 4:33 PM

@Ramya

Could you please eloborate more on the problem!?

chaitanya September 24, 2009 at 11:25 AM

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

Veera September 24, 2009 at 7:15 PM

@Chaitanya

Thank you for notifying the mistake. Corrected it.

huu2uan January 5, 2010 at 7:22 AM

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

Veera January 5, 2010 at 7:38 AM

yes. my bad.

thanks for noticing it.

hagbard February 6, 2010 at 2:37 AM

thanks! nice written and easy to understand!

Comments on this entry are closed.

Previous post:

Next post: