One technique would be to assign a name to each one of the clients.  Set up each name as a category in your log4j.properties (remember that you don't have to use classname as your Category names).  The client would pass this name in requests to the server.  When a service begins processing the clients request, it should use the passed name in a call to Category.getInstance().  Unfortunately, all the classes with which the service collaborates must also be aware of the service. (As an alternative, you could simply pass the Category instance around, possibly in the Constructors...)  For most class structures this is an unrealistic pre-condition.
 
Another option (if you can guarantee that each request runs in it's own thread) is to use the NDC (Nested Diagnostic Context).  Use NDC.push(clientName) at the beginning of your service (and make sure %x is part your PatternLayout).  Now, even though everything in your app server is sharing the same properties file (and thus the same destinations), all your messages will have the name of the requesting client in them as well.  At least then you can sort/filter on this name.  [Don't forget the NDC.pop() at the end of your request processing.  Read the javadoc on this stuff.]
 
This second option is what it looks like we'll be doing in a J2EE server...  (Still in the preliminary stages.)
 
-----Original Message-----
From: KIRKLAND,BRIAN (HP-PaloAlto,ex1) [mailto:[EMAIL PROTECTED]]
Sent: Wednesday, 17.October 2001 18:11
To: '[EMAIL PROTECTED]'
Subject: Setting up filter for multiple output files

Hi,
 
I am trying to set up Log4J to output messages to different files, depending on the client. Maybe I should give a little background. I wrapped Log4J and am running it as a service on my app server. Multiple "client" (which are actually other services on the app server) will use it for logging purposes. We want messages from each of the services to go to different output files, but I am having problems setting this up. The issue goes away if I run multiple instances of the Log service in different JVMs, but that is not possible. 
 
I am able to get the outcome I want with the code below, but I don't want to call PropertyConfigurator.configure every call, for performance reasons....I am sure you can see why.
 
Any suggestions? Does anyone have a sample configuration that does this?
 
Thanks,
 
 
    ...
        String instanceName = "foo";
    ...
        Category cat = Category.getInstance(instanceName);
    ...
        // get the appropriate configFile for the instance with name = instanceName
        String configFile = ....
 
        this.debug(configFile, "message");
    ...

    public void debug(String configFile, String message)
        throws LoggingUtilException
    {
        PropertyConfigurator.configure(configFile);
        cat.debug(message); 
    }

 
 

~Brian Kirkland
Advanced Technologies Group
Hewlett-Packard
(650) 314-4069
ecardFile: Brian Kirkland
 
A Life? Cool! Where can I download one of those from?

 

 

Reply via email to