How to split output file with log4j?

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  
BrianMc1958Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ajay-SinghCommented:
change this
 
log4j.appender.A2.datePattern='.'yyyy-MM-dd

 
 
to
 
log4j.appender.A2.datePattern='.'yyyyMMdd
0
Ajay-SinghCommented:
> 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
mrcoffee365Commented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Ajay-SinghCommented:
mrcoffee365,

does DatedFileAppender take care of split BrianMc1958 is asking?
0
cmalakarCommented:
>> 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
mrcoffee365Commented:
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
BrianMc1958Author Commented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.