Yes, it is possible. 

The naming of the loggers is not forced to be class names. You can choose 
whatever you want. Using class names (including package path) is just a comfort 
convenience which offers a bulk of advantages.

So you are free to define 4 different loggers with distinct names and configure 
their appenders like you want to. 

Logger.getLogger("myname").getAppenders() delivers you an enumeration of all 
appenders which are attached to this logger. 

Annother way to achieve your purpose is to use MDC, where you can store context 
informations which can be used by a self written filter (only XML 
configuration). It would be convenient to write a wrapper with functions 
covering all your logging needs. I.e.:

public void info_1( Logger log, String msg )
{
    MCD.put( "appender_target", "1" );
    try
    {
        log.INFO( msg );
    }
    finally
    {
        MDC.remove( "appender_target" );
    } 
}

Your custom filters which are attached to the different appenders consult this 
MDC entry and either allow or deny the log output.

With this approach you still can use the advantages of the hierarchical 
structure of log4j.

Heri


> -----Original Message-----
> From: Bruno Le Tual [mailto:[EMAIL PROTECTED]
> Sent: Thursday, December 29, 2005 12:25 AM
> To: log4j-user@logging.apache.org
> Subject: Dispatch messages to multiple files from the same class with
> several loggers ?
> 
> 
> 
> Hi,
> 
> is it possible ?
> 
> package chapter1;
> import org.apache.log4j.Logger;
> public class HelloWorld1
> {
>   static Logger logger1 = Logger.getLogger("something");
>   static Logger logger2 = Logger.getLogger("something else");
>   static Logger logger3 = Logger.getLogger("something else else");
>   static Logger logger4 = Logger.getLogger("something else 
> else else");
> 
>   static public void main(String[] args)
>   {
>     // TODO, some loggers init stuff ???? HOW ?
>     // ... and finally :
>     logger1.info("Hello world. 1");
>     logger2.info("Hello world. 2");
>     logger3.info("Hello world. 3");
>     logger4.info("Hello world. 4");
>   }
> }
> 
> I would like to output these logger messages in 4 files.
> 
> file1.log --> "Hello world. 1"
> file2.log --> "Hello world. 2"
> file3.log --> "Hello world. 3"
> file4.log --> "Hello world. 4"
> 
> 
> I would like also to rename "fileX.log" at runtime and not 
> just configure
> them in log4j.properties
> or log4j.xml.
> 
> 
> IS IT POSSIBLE from one class to instantiate several loggers 
> to dispatch
> messages in differents files?
> (Without duplicating messages and for ALL levels : info, 
> warn, error ...)
> 
> I googled for a long time but I did not find a good answer ....
> 
> 
> Thanks a lot,
> 
> 
>  Bruno.
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> 
> 

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to