home

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

July 28, 2009 · 31 comments

Hi there! I have created a small eBook explaining about Log4j appenders.  Download the Log4j PDF (200 KB)

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 File Appender to log messages into a file, instead of console.

Log4j File Appenders – example for Logging messages to a file:

In most cases, either Rolling File Appender or Daily Rolling File Appender classes are used in production scenarios for logging messages into a file. A Rolling File Appender 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 Rolling File Appender). The Daily Rolling File Appender is also similar to Rolling File Appender 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 log4j  configuration, we are creating a new log4j 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. Next we need to direct our logs to go into this log4j 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 Posts

{ 27 comments… read them below or add one }

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?

Reply

Veera August 4, 2009 at 12:57 AM

Thanks chaitanya.

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

Reply

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.

Reply

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

Reply

Satya September 8, 2009 at 3:26 PM

here is below my code

<!–

–>

Reply

Ramya September 15, 2009 at 10:04 AM

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

Reply

Veera September 15, 2009 at 4:33 PM

@Ramya

Could you please eloborate more on the problem!?

Reply

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

Reply

Veera September 24, 2009 at 7:15 PM

@Chaitanya

Thank you for notifying the mistake. Corrected it.

Reply

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

Reply

Veera January 5, 2010 at 7:38 AM

yes. my bad.

thanks for noticing it.

Reply

hagbard February 6, 2010 at 2:37 AM

thanks! nice written and easy to understand!

Reply

Gardiary Hiat December 22, 2010 at 2:20 PM

Thanks, it’s makes me clear..

Reply

nas January 5, 2011 at 1:56 PM

really good

Reply

nas January 5, 2011 at 2:05 PM

Hi
this is my lo4j.prop file
log4j.rootLogger=info,nasrollingFile
log4j.appender.nasrollingFile=org.apache.log4j.RollingFileAppender
log4j.appender.nasrollingFile.File=D://StatusManagerJars/nas.log
log4j.appender.nasrollingFile.MaxFileSize=2MB
log4j.appender.nasrollingFile.MaxBackupIndex=2
log4j.appender.nasrollingFile.layout = org.apache.log4j.PatternLayout
log4j.appender.nasrollingFile.layout.ConversionPattern=%p %t %c – %m%n

and following is the simple java class

import org.apache.log4j.Logger;

public class Logs {

static final Logger logger = Logger.getLogger(Logs.class);

public static void main(String[] args) {

logger.warn(“Sample warn message”);
logger.error(“Sample error message”);
logger.fatal(“Sample fatal message”);
logger.debug(“Sample debug message”);
logger.info(“Sample info message”);
System.out.println(“in”);
}
}

and the loggs i get in my log file are

WARN main Logs – Sample warn message
ERROR main Logs – Sample error message
FATAL main Logs – Sample fatal message
INFO main Logs – Sample info message

here my doubt is y im not getting the debug message in my log file

Reply

Veera January 5, 2011 at 2:28 PM

log4j.rootLogger=info,nasrollingFile

change the log level to debug and you’ll get the debug messages too.

Reply

nas January 5, 2011 at 3:05 PM

Ya thanks veera,its working.
I gave the log level as info but then i was getting warn,error, fatal. but y only debug was not coming up? can you tell me this clearly.

Reply

Veera January 5, 2011 at 3:12 PM

The log levels are like this: TRACE, DEBUG, INFO, WARN, ERROR and FATAL. So, if you set your log level as DEBUG and then all DEBUG messages as well as the messages in above level (info, warn etc..) will be logged. the lower level messages will be ignored (trace).

Since you set INFO previously and DEBUG is at lower level than INFO. Thats why you didn’t get DEBUG messages previously.

Reply

nas January 5, 2011 at 3:42 PM

Thats very clear. very good tutorial by you.Easy to learn

Reply

N Manikandan May 16, 2011 at 11:08 AM

Hi,

I would like to know is it possible to write different log file based on method in java class using log4j.

Thanks in advance.

Reply

Arvind May 29, 2011 at 10:05 AM

Hi Veera,

I have a couple of queries for you-

(1) Can i set the log file name and location, as a parameter from within the code? What is the code required to do this…

(2) What all classes have to be imported to use a log file with the above feature?

Regards,
Arvind.

Reply

Utsav July 5, 2011 at 10:30 AM

Hi,
I have to write code which would generate different log files for different users as the login into the application.
Someone please help me with that asap..

Regards Utsav

Reply

Rob September 20, 2011 at 5:27 PM

Great Tutorial. But what if you delete the logfile after log4j already wrote some logs?

log4j is not going to create a new logfile. Instead, all following logs will be lost. Is there any chance to avoid this?

Regards,
Rob.

Reply

A Saber October 27, 2011 at 9:01 AM

Thanks for the tutorial. It was a great help. Regards.

Reply

Avadoot October 31, 2011 at 5:53 PM

Hi veera,

currently m working on log4j rollinf file appender, when i m running single process its rolling over file nicely but when when m runnin two process they are not rolling over file , instead they are writing to the same file .

could u please suggest me what i should do to achieve the rolling over file when multiple process are running and accessin same log file..

Reply

Amruta January 9, 2012 at 6:47 PM

How we can generate a new log file for every month using properties file?
Is there any appender or somthing else which can do this?

Reply

Aditya January 12, 2012 at 1:35 PM

Thank you Veera for sharing such lovely information, i have a query its regarding maven n log4j

My TL was saying that if your workng on maven project means you dont have to download log4j and u dont even have build it also, so can u please tell me how to check the log files of my maven project
How maven does nt need this log4j ?

Thank you :)

Reply

Leave a Comment

{ 4 trackbacks }

Previous post:

Next post: