logging-log4j-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Omer Dogan <pesm...@yahoo.com>
Subject Multiple Appenders additivity
Date Fri, 22 Aug 2008 07:18:09 GMT
When i was trying to log on console with additivity, i realise log4j dont care if parent categories
have appropriate level to log the event.
is this a design choice and are there any other way to prevent the appenders from logging
smaller level of logging events.

Best of i can do is changing the source-code of log4j


Scenerio 
I want to write all aplication logs to a file and
I want to write only error logs to console 
and I dont want repeated log statements like "consolelogger.error("error 1"); filelogger.error("error
1")"
    
--- my log4j configutration ---
log4j.rootLogger=ERROR,consol

log4j.logger.consol            =ERROR,consol
log4j.appender.consol       =org.apache.log4j.ConsoleAppender
log4j.additivity.consol        =true
...

log4j.logger.Application        =DEBUG,Application
log4j.appender.Application    =org.apache.log4j.DailyRollingFileAppender
log4j.additivity.Application     =true
...

--- Code
logger.info("log 1 - LEVEL_INFO")
logger.error("log 2 - LEVEL_ERROR")
...

while i have expecting , with the code above,

log file would contains "log 1" and "log 2"
console would contains only "log 2 "

instead i have got the console and log file contains both "log 1" and "log 2 " info

then i check the source code  and i found the problem on callAppenders() method 
and change the beheavior of the method.

-- old one 
public void callAppenders(LoggingEvent event) {
        int writes = 0;
        for (Category c = this; c != null; c = c.parent) {            
            // Protected against simultaneous call to addAppender,
            // removeAppender,...
            synchronized (c) {
                if (c.aai != null) {
                    writes += c.aai.appendLoopOnAppenders(event);
                }
                if (!c.additive) {
                    break;
                }
            }
        }

-- i change like this
public void callAppenders(LoggingEvent event) {
        int writes = 0;

        for (Category c = this; c != null; c = c.parent) {
            if (!event.getLevel().isGreaterOrEqual(c.getLevel()))
                continue;
            // Protected against simultaneous call to addAppender,
            // removeAppender,...
            synchronized (c) {
                if (c.aai != null) {
                    writes += c.aai.appendLoopOnAppenders(event);
                }
                if (!c.additive) {
                    break;
                }
            }
        }

thanks.


      

---------------------------------------------------------------------
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