[logback-user] Different Log Levels For Different Appenders?

Ceki Gulcu listid at qos.ch
Thu Aug 28 17:17:01 CEST 2008



Contrary to log4j, logback appenders do not automatically inherit a threshold 
filter. You probably don't care but this has to do with the fact that the 
functionality offered by log4j is assumed by two separate modules, logback-core 
and logback-classic. Logback-core, where several appenders (e.g. FileAppender) 
are defined, does not really know about logging. Both the Logger and Level 
classes are defined in logback-classic and not in logback-core. In order to 
avoid duplicated code, FileAppender is defined only in logback-core and not in 
logback-classic. It's LoggingEvent specific layouts and filters defined in 
logback-classic that give a FileAppender instance its "logging-aware" behavior.

Anyway, to cut a long story short, it could have been possible to redefine 
FileAppender in logback-classic in order to add a threshold-filter. But as it is 
possible to accomplish the same by setting a "c.q.l.clas.filter.ThresholdFilter"
in a configuration file, we traded shorter configuration syntax in favor of not 
not duplicating code.

Anyway, see logback-examples/src/main/java/chapter6/ThresholdFilterConfig.xml 
for an example. It really has the same effect as the threshold keyword in log4j.

Rob Ross wrote:
> I also wanted to do exactly what you are trying to do. I discovered  
> the only way to do this is with a filter on the appender, instead of  
> being able to set the Level on the appender as you could do with log4j.
> 
> I don't know yet which method is "better", but the ability to set a  
> filter on the appender does give me the same end-result as with  
> log4j, so I am satisfied with this method.
> 
> 
> Rob Ross, Lead Software Engineer
> E! Networks
> 
> ---------------------------------------------------
> "Beware of he who would deny you access to information, for in his  
> heart he dreams himself your master." -- Commissioner Pravin Lal
> 
> 
> 
> On Aug 28, 2008, at 7:01 AM, Eric Faden wrote:
> 
>> That doesn't exactly solve what I am trying to do.  The problem isn't
>> that I can't get the correct level on a specific logger, the  
>> problem is
>> more that I want to have a specific logger go to two different  
>> appenders
>> and log at different levels based on the appender.  For example I want
>> to have the root logger log at level INFO to the Console.  I then want
>> to have the logger for some class Blah log to the appender for root
>> (Console) at the level of root (INFO), but also log to a file  
>> Foo.txt at
>> level Debug.  So far as I can tell the only way to do this is to  
>> set the
>> level of Blah to debug, have a file appender, and also put a filter on
>> the console appender to filter out things below INFO.  Does that make
>> sense what I am trying to do?  Basically I want a single logger to log
>> to different levels to different appenders.
>>
>> -Eric
>>
>> Ceki Gulcu wrote:
>>> Hello Eric,
>>>
>>> I think this is described in chapter 3 of the manual. See "Example  
>>> 3.9: Logger
>>> level sample (logback-examples/src/main/java/chapter3/ 
>>> sample4.xml)". The list
>>> numbering in that chapter only works with Firefox. So on other  
>>> browsers you
>>> might see all examples numbered as "3.". See also the next section  
>>> in the same
>>> chapter, on "Configuring Appenders", in particular, the cumulative  
>>> addition of
>>> appenders. It might be the other "cleaner" way, you are looking for.
>>>
>>> HTH,
>>>
>>> Eric Faden wrote:
>>>
>>>> So suppose I set my root logger to error, but I want to have another
>>>> logger operate at debug level.  When I do that it makes that loggers
>>>> level debug for all appenders.  For example take the following
>>>>
>>>> <configuration>
>>>>
>>>>   <appender name="*FILE*"
>>>>      ....
>>>>   </appender>
>>>>
>>>>   <appender name="*STDOUT*"
>>>>      ...
>>>>   </appender>
>>>>
>>>>   <logger name="mylogger">
>>>>     <level value="debug" />
>>>> **    <appender-ref ref="FILE" />**
>>>>   </logger>
>>>>
>>>>   <root>
>>>>     <level value="error" />*
>>>>     <appender-ref ref="STDOUT" />*
>>>>   </root>
>>>> </configuration>
>>>>
>>>> In this example mylogger would output at debug level to BOTH STDOUT
>>>> and FILE.  Is it possible to only get mylogger to output at debug
>>>> level to FILE and error to STDOUT?  I realize that I can add a  
>>>> filter
>>>> to the STDOUT appender to filter anything below ERROR, but I was
>>>> curious if there was another way to do this, possibly cleaner?
>>>>
>>>> -Eric
>>>>
>>>
>> _______________________________________________
>> Logback-user mailing list
>> Logback-user at qos.ch
>> http://qos.ch/mailman/listinfo/logback-user
> 
> _______________________________________________
> Logback-user mailing list
> Logback-user at qos.ch
> http://qos.ch/mailman/listinfo/logback-user

-- 
Ceki Gülcü


More information about the Logback-user mailing list