[logback-user] Setting log LEVEL on a per <appender-ref/> basis

ceki ceki at qos.ch
Sat May 5 23:09:22 CEST 2012

Hello Shane,

There are probably multiple approaches to this problem. It should be not 
too difficult to write a RoutingAppender which can perform the type of 
routing operation you describe. Here is how the config file would look like:

<appender name="A"></appender>
<appender name="B"></appender>

<appender name="ROUTING-ONE" class="...RoutingAppender">
   <reference name="A" level="INFO"/>
   <reference name="B" level="DEBUG"/>

<appender name="ROUTING-TWO" class="...RoutingAppender">
   <reference name="A" level="WARN"/>
   <reference name="B" level="TRACE"/>

<logger name="com.myorg.myapp.subpackage.one">
   <appender-ref ref="ROUTING-ONE"/>

<logger name="com.myorg.myapp.subpackage.two">
   <appender-ref ref="ROUTING-TWO"/>

By the way, could you provide more detail for your use case?


On 05.05.2012 22:48, Shane Kelly wrote:
> Folks,
> I've been tinkering with both log4j and more recently logback and I was
> wondering whether or not it is possible to configure it in such a way
> that it would be possible for a single logger to write to multiple
> appenders using different LEVEL restrictions on a per-appender-ref
> basis. I appreciate that it is possible to specify a LEVEL attribute for
> each individual logger - but then any message which meets the logger
> LEVEL criteria will be sent to all referenced appenders. I'm also aware
> that you can define level thresholds and filters on a per-appender basis
> - but what I was hoping for was rather than define lots of very similar
> appender configurations (each with slightly different thresholds) that I
> could define a smaller number of core appenders and have messages of
> certain levels filtered out even before the appender is called.
> To help illustrate the concept I'm aiming for, I've included a
> pseudo-configuration file below. You'll notice that I've omitted the
> LEVEL attribute from the <logger/> nodes and added LEVEL attributes to
> each of the <appender-ref> nodes - this doesn't work obviously, but I
> was wondering if there was some other way of emulating the behaviour
> that I'm after without cluttering up the logback configuration too
> much...? You can see from the configuration that for
> com.myorg.myapp.subpackage.one I would like to apply different log level
> restrictions depending on the destination appender. You'll also notice
> that for com.myorg.myapp.subpackage.two I want to reuse the same
> appenders, but also use different level restrictions.... hope that makes
> it clear enough...
> ---------
> <appender name="A">
>    ...
>    ...
> </appender>
> <appender name="B">
>    ...
>    ...
> </appender>
> <logger name="com.myorg.myapp.subpackage.one">
> <appender-ref ref="A" LEVEL="INFO"/>
> <appender-ref ref="B" LEVEL="DEBUG"/>
> </logger>
> <logger name="com.myorg.myapp.subpackage.two">
> <appender-ref ref="A" LEVEL="WARN"/>
> <appender-ref ref="B" LEVEL="TRACE"/>
> </logger>
> ----------
> Regards,
> Shane

More information about the Logback-user mailing list