aspose file tools*
The moose likes Other Open Source Projects and the fly likes Logging to more than one file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Products » Other Open Source Projects
Bookmark "Logging to more than one file" Watch "Logging to more than one file" New topic
Author

Logging to more than one file

Cemil Ozdemir
Greenhorn

Joined: Nov 23, 2006
Posts: 11
Hi,

Can someone please help with a problem i have.

I already have a log4j logger installed and in use in my application.

My application creates files based from a database,
and for that purpose i have a normal debug logger installed.

But for arhiving purpose i need another log file that should only contain the
name of the file and the date.

So after my the file has been written, i need to save the name of the file and the date to a different log file.

How would I do that ?

Hope my question is understandable.


Best Regards
Cemil �zdemir

[ January 17, 2007: Message edited by: Cemil Ozdemir ]
[ January 17, 2007: Message edited by: Cemil Ozdemir ]
Carol Enderlin
drifter
Ranch Hand

Joined: Oct 10, 2000
Posts: 1364
How? Log to another Logger configured with another Appender.

My first thought reading your question was that "archiving" doesn't sound like a typical use for log4j type of logging.

But for arhiving purpose i need another log file that should only contain the
name of the file and the date.


Why do I say that? See this FAQ: Is log4j a reliable logging system?
No. log4j is not reliable. It is a best-effort fail-stop logging system.

By fail-stop, we mean that log4j will not throw unexpected exceptions at run-time potentially causing your application to crash. If for any reason, log4j throws an uncaught exception, please send an email to the log4j-user@logging.apache.org mailing list. Uncaught exceptions are handled as serious bugs requiring immediate attention.

Moreover, log4j will not revert to System.out or System.err when its designated output stream is not opened, is not writable or becomes full. This avoids corrupting an otherwise working program by flooding the user's terminal because logging fails. However, log4j will output a single message to System.err indicating that logging can not be performed.
Cemil Ozdemir
Greenhorn

Joined: Nov 23, 2006
Posts: 11
Hi Carol,

Thanks for the answer.

But even though log4j, is not meant for archiving, we have decided to use it any way(with the consequences in mind).

But i was looking for an answer for this:

Let us say i have two appenders, X and Y defined in my log4j properties file.

Then in my code i log to the file this way:
private static final Logger log = Logger.getLogger(MyClass.class);
...
log.debug("test");

Now how can i distinct in my code which appendar/file i am writing to ?

Maybe it is a silly question but i am not that familiar with log4j.

Best Regards
Cemil �zdemir
Carol Enderlin
drifter
Ranch Hand

Joined: Oct 10, 2000
Posts: 1364
Probably the easiest way is to associate the different appenders with different loggers.

If it is done in the same class then you'll need to create a second logger in that class with a different name. e.g., either something like "archive" or "archive." + class.getName() ; then associate the archive appender with the "archive" logger.

If they are not in the same class, then what part are you having trouble with? Show properties file and be specific about what help you need.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Logging to more than one file