aspose file tools*
The moose likes Other Open Source Projects and the fly likes Logging into different files based on parameters Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Products » Other Open Source Projects
Bookmark "Logging into different files based on parameters" Watch "Logging into different files based on parameters" New topic
Author

Logging into different files based on parameters

Vishnu Vardhan
Greenhorn

Joined: Nov 05, 2003
Posts: 14
I want to log into two different files based on a value of a varaiable in my Java program

Example : if(value = "abc") log into abcd.txt
else if (value = "xyz") log into xyzz.txt

Can any one help me out please.

Vishnu
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18658
    
    8

The if-statements seem like a good start. But you have a problem? I can't see what it is. Can you explain a bit more?
Vishnu Vardhan
Greenhorn

Joined: Nov 05, 2003
Posts: 14
Hi Paul,

Actually my Java program handles requests from two systems. System A and system B. Currently I am logging all the logs into a single file using log4J.

No I want to seperate it. When I process requests from system A they must go to a.log and when I process requests from system B they must go to b.log.

I hope it is clear now.

Thanks
-Vishnu
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10207
    
166

You can have 2 separate loggers in your java code. Each logger will use a separate appender. To avoid code duplication(of the if/else statements in each class where you are using the logger), i would suggest, you create your own wrapper over the log4j's Logger. Something like:



And the classes using the logger will look like:





Your log4j.xml will look like:



The above method looks simple. However, you might want to have a look at some other ways to tackle this, mentioned at Logging Separation and see if they are better suited in your application
[ July 12, 2006: Message edited by: jaikiran pai ]

[My Blog] [JavaRanch Journal]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Logging into different files based on parameters