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.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...

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.

change this

> 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.
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

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

log4j.appender.A2.layout.ConversionPattern=%d{MM/dd/yy HH:mm:ss} [%c{4}]:(%x):%-5p: %m%n

If you're using log4j 1.3, then the RollingFileAppender requires the properties to be in a .xml file, according to the people:

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.


does DatedFileAppender take care of split BrianMc1958 is asking?
>> 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..
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 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.
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";

Thanks for your help, everybody...
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

From novice to tech pro — start learning today.