logging-log4j-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Curt Arnold <carn...@apache.org>
Subject Re: Create multiple appenders objects
Date Fri, 12 Oct 2007 01:36:28 GMT

On Oct 11, 2007, at 6:46 PM, Julio S. G. wrote:
>> On 10/11/07, Julio S. G. <juliosg@gmail.com> wrote:
>>> Hi,
>>>
>>> I'm trying to create several appenders and unfortunately it's not  
>>> working.
>>> To create the objects I'm using something like new
>>> DailyRollingFileAppender() with corrects parameters. Then I  
>>> create the
>>> Logger using the appender name.
>>> Finally, when I use the Logger object to write, it doesn't work.
>>> I'll put the code here and if anyone could help me I really  
>>> appreciate.
>>>
>>>         Layout layout1 = new PatternLayout("%m%n");
>>>         Layout layout2 = new PatternLayout("%m%n");
>>>         DailyRollingFileAppender app1 = new
>>> DailyRollingFileAppender(layout1, "/file1.log", "'.'yyyyMMddHH");
>>>         DailyRollingFileAppender app2 = new
>>> DailyRollingFileAppender(layout2, "/file2.log", "'.'yyyyMMddHH");
>>>         app1.setThreshold(Level.DEBUG);
>>>         app2.setThreshold(Level.DEBUG);
>>>         app1.setName("APPENDER1");
>>>         app2.setName("APPENDER2");
>>>

//  as previously mentioned
app1.activateOptions();
app2.activateOptions();


>>>         Logger logger1 = Logger.getLogger(app1.getName());
>>>         Logger logger2 = Logger.getLogger(app2.getName());


That the appender and logger have the same name in no way connects  
them.  At this point, you have two appenders called "APPENDER1" and  
"APPENDER2" with DEBUG threshold and two loggers named "APPENDER1"  
and "APPENDER2" with no explicit threshold which will default to DEBUG.

It is common and more efficient to set the threshold on the logger  
since that is checked before a LoggingEvent is created.  The  
threshold of the Appender is only checked after the logger threshold  
is checked, the LoggingEvent is created and then dispatched to the  
Appender.

Logger names are generally (but not necessarily) based on class  
names.  While "APPENDER1" and "APPENDER2" are totally legal logger  
names, they are definitely unusual.  In general, you'd want to attach  
at least one appender to the root logger, so that anything that is  
logged with an unexpected logger goes somewhere.  If you want a  
particular set of messages to go to one and only one appender, the  
general pattern is to attach one appender to the root logger and the  
other appender to a particular logger and set that loggers additivity  
to false so the message doesn't get passed down the hierarchy.   
Something like:

Logger rootLogger = Logger.getRootLogger();
rootLogger.addAppender(app1);
Logger logger1 = Logger.getLogger("com.example.myapp.Logger1");
Logger logger2 = Logger.getLogger("com.example.myapp.Logger2");
logger2.setAdditivity(false);
logger2.addAppender(app2);


>>>         logger1.info("TEST 1!!!");
>>>         logger2.info("TEST 2!!!");
>>>





---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-user-help@logging.apache.org


Mime
View raw message