Avatar of allelopath
allelopath
 asked on

log4j - multiple logs in one class

I have a problem with multiple logs in one class.
The problem is that the logs are being written, but then when the program is done, the files are being erased (contain nothing). I can see the log files fill up as the program goes along, so I know stuff is being written.

     FileAppender appender1 = null;
     try {
       myAppender1 = new FileAppender("file1.log",false);
     } catch(Exception e) {}

     logger1.addAppender(appender1);
     logger1.setLevel((Level) Level.INFO);
     
     FileAppender appender2 = null;
     try {
       myAppender2 = new FileAppender("file2.log",false);
     } catch(Exception e) {}

     logger2.addAppender(appender2);
     logger2.setLevel((Level) Level.INFO);
     
Java

Avatar of undefined
Last Comment
CEHJ

8/22/2022 - Mon
CEHJ

You need to use the constructor that tells the appender to append to the file or it'll be rewritten
allelopath

ASKER
I think i wasn't clear.
It is ok if the file starts over with each start of the program.
What I am saying is that when the program is finished (but before running again), there is nothing there, but I did see stuff in it while the program is running.

Also, I've noticed that the output in the 2 log files is the same, when in fact I intended for some output to 1 file, and other output to the other.
CEHJ

>>What I am saying is that when the program is finished (but before running again), there is nothing there, but I did see stuff in it while the program is running.

Make sure that no other program is holding the file open while the app is running
Your help has saved me hundreds of hours of internet surfing.
fblack61
allelopath

ASKER
This must be the problem:

  final static Logger logger1 = Logger.getLogger(MyClass.class);
  final static Logger logger2 = Logger.getLogger(MuClass.class);

how do I differentiate these?

allelopath

ASKER
Sorry, thats:

  final static Logger logger1 = Logger.getLogger(MyClass.class);
  final static Logger logger2 = Logger.getLogger(MyClass.class);
allelopath

ASKER
Never mind on the 2nd part:
  final static Logger logger1 = Logger.getLogger("log1");
  final static Logger logger2 = Logger.getLogger("log2");
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
allelopath

ASKER
I've determined that the mulitple loggers is not the cause of the problem.
If I just have one logger, the file is being cleaned out (before starting the program again)
CEHJ

If you have the file held open, the buffers may not be able to be flushed and the file won't get written
ASKER CERTIFIED SOLUTION
Mick Barry

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
allelopath

ASKER
>>If you have the file held open..
how do i close the file or flush the buffers, or something else?

No config file


This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
Mick Barry

> how do i close the file or flush the buffers, or something else?

you don't, and it is not necessary.
SOLUTION
CEHJ

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.