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
- 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 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.
- Next we need to direct our logs to go into this log4j 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.


{ 27 comments… read them below or add one }
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!
Thanks, it’s makes me clear..
really good
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
log4j.rootLogger=info,nasrollingFile
change the log level to debug and you’ll get the debug messages too.
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.
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.
Thats very clear. very good tutorial by you.Easy to learn
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.
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.
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
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.
Thanks for the tutorial. It was a great help. Regards.
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..
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?
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
{ 4 trackbacks }