?
Solved

How to split output file with log4j?

Posted on 2008-02-06
7
Medium Priority
?
1,878 Views
Last Modified: 2010-07-27
Dear Experts,

I just set up log4j for the first time.  (You all told me to do it about five years ago.  I'm a little slow.)

I have it set up in .properties config file, using DailyRollingFileAppender, like this:

  log4j.rootCategory=INFO, A2
  log4j.appender.A2=org.apache.log4j.DailyRollingFileAppender
  log4j.appender.A2.file=MyFirst.log
  log4j.appender.A2.datePattern='.'yyyy-MM-dd
  log4j.appender.A2.append=true
  log4j.appender.A2.layout=org.apache.log4j.PatternLayout
  log4j.appender.A2.layout.ConversionPattern=%-5p %d{ISO8601} [%t] - %m%n


 What I'd actually like it to do is save the old file as log_yymmdd, and start a new one with a new date.

In addition, I'd like to be able to tell it to split the log file when my application starts.  So maybe it becomes log_yymmdd_001.  Is that possible?  I assume I'd need to do it within the Java, which is fine, but I have no idea how...

Thanks!
BrianMc1958  
0
Comment
Question by:BrianMc1958
7 Comments
 
LVL 23

Assisted Solution

by:Ajay-Singh
Ajay-Singh earned 400 total points
ID: 20837212
change this
 
log4j.appender.A2.datePattern='.'yyyy-MM-dd

 
 
to
 
log4j.appender.A2.datePattern='.'yyyyMMdd
0
 
LVL 23

Expert Comment

by:Ajay-Singh
ID: 20837219
> In addition, I'd like to be able to tell it to split the log file when my
application starts.
 
What do you mean by spliting? At a given point of time, there will be only
one file on the old location for a date.
0
 
LVL 27

Accepted Solution

by:
mrcoffee365 earned 1200 total points
ID: 20838409
If you're using the older log4j (DailyRollingFileAppender is deprecated in 1.3, so I'm assuming that you are) we found that the best way to get daily rolling files was to use another library from minaret.biz:
http://minaret.biz/tips/datedFileAppender.html

In that case, once you follow the directions and download their class, your log4j.properties file would look something like this:

log4j.appender.A2=biz.minaret.log4j.DatedFileAppender
log4j.appender.A2.layout=org.apache.log4j.PatternLayout
log4j.appender.A2.Directory=yourwebapp/Root/logs
log4j.appender.A2.Prefix=log4jYourWebappName.
log4j.appender.A2.Append=true
log4j.appender.A2.layout.ConversionPattern=%d{MM/dd/yy HH:mm:ss} [%c{4}]:(%x):%-5p: %m%n
log4j.appender.A2.threshold=DEBUG

If you're using log4j 1.3, then the RollingFileAppender requires the properties to be in a .xml file, according to the minaret.biz people:
http://minaret.biz/tips/log4j.html
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
LVL 23

Expert Comment

by:Ajay-Singh
ID: 20838450
mrcoffee365,

does DatedFileAppender take care of split BrianMc1958 is asking?
0
 
LVL 23

Assisted Solution

by:cmalakar
cmalakar earned 400 total points
ID: 20839313
>> I'd like to be able to tell it to split the log file when my application starts.

Are you looking for rotating the file, even when the application starts..
then I think, there is no way to tell this in log4j..

We can rename the last log file ourselves, when application starts,  before initializing the logger..
0
 
LVL 27

Assisted Solution

by:mrcoffee365
mrcoffee365 earned 1200 total points
ID: 20842490
Ajay-Singh:  Log4j will split files, but it depends on what BrianMc1958 wants to do.  DatedFileAppender does what I said it will do, which is rollover log4j files.

cmalakar:  You should try minaret.biz DatedFileAppender, or the RollingFileAppender from the latest Log4j, if you would like Log4j to rename the last log and start a new one based on date.
0
 

Author Comment

by:BrianMc1958
ID: 20853501
Sorry my question was a little confusing.  I was starting with the functionality of RollingFileAppender, but in addition to that, I wanted to be able to have the output file re-start on my command, in the middle of the day, if necessary.  

For anyone reading this, I found the answer on a site called "Experts Exchange".  Some guy named CEHJ.  Don't have the link handy, but this was my final result:

      public static void splitLog()
      {
            Appender t = LogManager.getRootLogger().getAppender("MyAppenderName");
            FileAppender fileAppender = (FileAppender) t;
            String newName = "Log" + MyUtil.getTimeStampTwentyFourHours() + ".txt";
            fileAppender.setFile(newName);
            fileAppender.activateOptions();
      }  

Thanks for your help, everybody...
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
In this post we will learn different types of Android Layout and some basics of an Android App.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month7 days, 10 hours left to enroll

607 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question