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

Andrew E. Davidson adavidson2 at apple.com
Wed Apr 15 03:12:36 CEST 2015


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();
        Level level = event.getLevel();
        System.err.println("convert() level:" + level);
        return "DebugConverter.convert()";
    }
}

<configuration>
	<conversionRule conversionWord="AST_DEBUG" 
                  converterClass="com.apple.ast.logging.converter.DebugConverter" />
                  
    <appender name="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>

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

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.qos.ch/pipermail/logback-user/attachments/20150414/5405283f/attachment.html>


More information about the Logback-user mailing list