[logback-user] Can I have level specific patterns?

Ceki Gülcü ceki at qos.ch
Wed Apr 15 12:23:06 CEST 2015


Hi Andy,

When you write "%a(%x %y)" then %x and %y are child converters for %a. 
So, in the case of "%debug(%level %d %t)", the converters %level,%d and 
%t are children of %debug. %debug is said to be a composite converter 
and the parenthesis () is sort of a composition operator.

For your particular case, you need to sub-class CompositeConverter. See 
the implementation for HighlightingCompositeConverter (and 
ForegroundCompositeConverterBase). Look at the code for 
CompositeConverter ND notice how the convert() method of 
CompositeConverter delegates to child converters and only at the end 
transforms the results.

I hope this helps,

--
Ceki

On 4/15/2015 3:12, Andrew E. Davidson wrote:
> Hi Ceki
>
>
>>
>> Alternatively, you could write a custom conversion specifier. If you
>> had a specifier called %debug which output contents only for level
>> DEBUG, and a %warn specifier which output contents only for level WARN
>> and another specifier %ERROR which output contents for ERROR, your
>> pattern could be written as
>>
>> %debug(%level %d %t) %error(%level) %info(%level) %m%n
>>
>> See http://logback.qos.ch/manual/layouts.html#customConversionSpecifier
>> for documentation on this topic.
>>
>
> I tried your “custom conversion specifier” approach. I think I am
> missing something. In your logback.xml it looks like you are able to
> pass arguments. I used a debugger how ever it did not appear like the
> the ILoggingEvent had anything particularly useful other then the level
> and thread Name values. List<String> optionList= getOptionList(); gives
> me [%level %d %t] how ever the values have not been expanded
>
> Your solution is very close to what I want to do.
>
> Thanks in advance
>
> Andy
>
> public class DebugConverter extends ClassicConverter {
> public DebugConverter() {
> // TODO Auto-generated constructor stub
>      }
>
> @Override
> public String convert(ILoggingEvent event) {
> List<String> optionList = getOptionList();
> Levellevel=event.getLevel();
>          System.err.println("convert() level:" + level);
> return"DebugConverter.convert()";
>      }
> }
>
> <configuration>
> <conversionRuleconversionWord="AST_DEBUG"
> converterClass="com.apple.ast.logging.converter.DebugConverter"/>
>
>
> <appendername="STDOUT"class="ch.qos.logback.core.ConsoleAppender">
> <encoder>
> <!--
>              <pattern>%-5level %date{HH:mm:ss,SSS} [%thread]
> [%class{16}] [line:%L] %msg%n</pattern>
>              -->
> <pattern>%AST_DEBUG{%level %d %t}  %m%n</pattern>
> </encoder>
> </appender>
>
> <rootlevel="DEBUG">
> <appender-refref="STDOUT"/>
> </root>
> </configuration>


More information about the Logback-user mailing list