You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-user@logging.apache.org by Timo Nentwig <tc...@nitwit.de> on 2003/02/22 09:14:22 UTC
Multiple logfiles for multiple instances?
hi!
public class Blah
{
static logger = Logger.getLogger (Blah.class)
long ID;
public Blah (long ID)
{
this.ID = ID;
}
public void go()
{
logger.info ("blah");
}
}
The above class may be instantiated multiple times. You might even imagine a
multi-threaded environment.
Is it - with an xml configuration file, not hard coded! - possible to define
an <appender> which might e.g. create a logfile (name: e.g. ID or NDC) for
each instance of Blah?
Imagine a web test application which does simulate multiple http clients
which send request to the webserver. That particular http client would be a
class (just like Blah) and I want to have a log file for each http client. I
can do this by putting the following into the class' constructor but I don't
want to hard-code it:
try
{
FileAppender appender = new FileAppender (new SimpleLayout(), "./logs/" +
ip + ".log", false);
logger.addAppender (appender);
logger.setLevel (Level.ALL);
}
catch (Exception e)
{
System.err.println ("FATAL - " + e.getMessage());
}
Thanks!
Timo
--
http://nitwit.de/
http://www.jeigh.com/werk/admin/
---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: log4j-user-help@jakarta.apache.org
Re: Multiple logfiles for multiple instances?
Posted by donald larmee <dl...@utopiansoft.com>.
There are probably a dozen ways to do this, but here is a possible approach:
- Push the IP of the client into the NDC.
- Create a custom MultipleFileAppender that would examine the NDC
contents against a Map of open files. If it finds the NDC in the map, it
writes the message to the file associated w/ the IP. If it does not find
the IP in the map, it creates/opens a new file and puts the IP into the
Map, so that subsequent log invocations would go to the appropriate file.
- The base file name/pattern to use for the file name would be passed in
as an attribute in the config file.
The obvious risk here would be that your Map of open Files would grow
indefinitely, unless some mechanism was put in place to clean the Map...
perhaps by linking the IP address to the Session, and then having a
HttpSessionListener/whatever clean the Map when the Session Expires....
I would say, however, it would seem easier to use the NDC to distinguish
the logging outputs of your various clients and write to single file, and
then just filter the results upon viewing time.
-d
At 09:14 AM 2/22/2003 +0100, Timo Nentwig wrote:
>hi!
>
>public class Blah
>{
> static logger = Logger.getLogger (Blah.class)
> long ID;
>
>
> public Blah (long ID)
> {
> this.ID = ID;
> }
>
> public void go()
> {
> logger.info ("blah");
> }
>}
>
>The above class may be instantiated multiple times. You might even imagine a
>multi-threaded environment.
>
>Is it - with an xml configuration file, not hard coded! - possible to define
>an <appender> which might e.g. create a logfile (name: e.g. ID or NDC) for
>each instance of Blah?
>
>Imagine a web test application which does simulate multiple http clients
>which send request to the webserver. That particular http client would be a
>class (just like Blah) and I want to have a log file for each http client. I
>can do this by putting the following into the class' constructor but I don't
>want to hard-code it:
>
> try
> {
> FileAppender appender = new FileAppender (new SimpleLayout(), "./logs/" +
>ip + ".log", false);
> logger.addAppender (appender);
> logger.setLevel (Level.ALL);
> }
> catch (Exception e)
> {
> System.err.println ("FATAL - " + e.getMessage());
> }
>
>Thanks!
>Timo
>--
>http://nitwit.de/
>http://www.jeigh.com/werk/admin/
>
>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: log4j-user-unsubscribe@jakarta.apache.org
>For additional commands, e-mail: log4j-user-help@jakarta.apache.org
+-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-+
donald h. larmee dlarmee@utopiansoft.com
804.301.UTOP
utopian software concepts, inc.
www.utopiansoft.com
+-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-+
---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: log4j-user-help@jakarta.apache.org