[logback-user] Log method name and line number only for exceptions

ceki ceki at qos.ch
Wed Jul 4 21:12:40 CEST 2012


Hello,

Yes, it is possible to limit the computation of caller info by a user
defined criteria, including the presence of an exception. The %caller
conversion word [1] takes an evaluator as a second parameter. Quoting
from the docs:

   This conversion word can also use evaluators to test logging events
   against a given criterion before creating the output. For example,
   using %caller{3, CALLER_DISPLAY_EVAL} will display three lines of
   stacktrace, only if the evaluator called CALLER_DISPLAY_EVAL returns a
   positive answer.

Evaluators are described at [2]. See also [3].

Here is a sample configuration file for your use case:

<configuration>
   <evaluator name="CALLER_EVAL">
     <expression>throwable != null</expression>
   </evaluator>

   <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
     <encoder>
       <pattern>
         [%thread] %level - %msg%n%caller{6, CALLER_EVAL}
       </pattern>
     </encoder>
   </appender>

   <root level="DEBUG">
     <appender-ref ref="STDOUT" />
   </root>
</configuration>

I hope this helps,

-- 
Ceki
http://twitter.com/#!/ceki

[1] http://logback.qos.ch/manual/layouts.html#conversionWord
[2] http://logback.qos.ch/manual/layouts.html#Evaluators
[3] http://logback.qos.ch/manual/filters.html#JaninoEventEvaluator

On 04.07.2012 17:21, lopardo . wrote:
> Hi. Is it possible to log method name and line number only for
> exceptions? Or set a different pattern, but only for exceptions?
>
> That way I could log more info, even if it's slower to process, for some
> exceptions.
>
> Thanks.
>
> Regards,
> lopardo.
>




More information about the Logback-user mailing list