tags:

views:

67

answers:

2

We have multiple log files like database log, weblog, quartzlog in our application.

Any log from files under package /app/database will go to database log. Any log from files under package /app/offline will go to quartzlog log.

What we need now is - want to direct the log staments from one of the java file under /app/database to be outputted to quartzlog instead of database log.

How can we select a particular log file in java file?

A: 

You need to define the appropriate appender that logs in the desired file. Read this short introduction to see how you can do it.

Then in the configuration file, you can instruct all messages from a specific package to go in the selected appender:

log4j.logger.my.package = DEBUG, myFileAppender

EDIT:

I believe that in log4j only package resolution is possible - you can't use an appender per file or method. You could try to work around this by adding an extra layer on top of log4j or implementing your own appender.

For example, instead of log.debug use:

my.loggerproxy.log.debug(message);

If you only need to do it from a single method, then the above will be enough. Just instruct the logger proxy package to be logged in a different file.

kgiannakakis
Thank you. Yes we have this already. But what I need is output log statements from one particular method(Say a method in delegate) to go in another output file(quartz).
EDIT: Thankk you for explaining. I will explore more
Any pointers on adding an extra layer on top of log4 would help. I could not figure it out. Thanks
Just create a my.loggerproxy.Log class with static methods debug, info and so on. These methods will call log.debug. You will need one class per log file - not an elegant solution, but it may work for you.
kgiannakakis
Thank you. I will try this approach
Are you talking about extending Log4J logger class? Ijust read that it is not recommended. Am I missing anything
A: 

I can suggest a product which solves this particular problem - logFaces. Your log data goes into centralized storage from all applications and hosts. With a few clicks of a button you can extract a log file according to your rules and based on the format you like. For example, you can make a log file containing data from last week coming from data layer classes on particular host, including warnings and more sever messages. And much more..

Dima