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

Andrew E. Davidson adavidson2 at apple.com
Wed Apr 15 23:06:35 CEST 2015


SWEET!

thanks Ceki. My guess is other people will want to do the same thing. bellow is my working code. 

background. I have a CLI java application. I want to generate all output using logger instead of System.out or System.error. I want logger.info() to be un decorated. All other levels of logging should level, class and line #

Thanks


Andy

public class DebugConverter extends CompositeConverter<ILoggingEvent> {

    public DebugConverter() {
        // TODO Auto-generated constructor stub
    }

    @Override
    protected String transform(ILoggingEvent event, String in) {
        if (Level.INFO == event.getLevel()) {
            return "";
        }
        StringBuilder sb = new StringBuilder();
        sb.append(in);
        String ret =  sb.toString();
        return ret;
    }
}

<configuration>
	<conversionRule conversionWord="AST_DEBUG" 
                  converterClass="com.apple.ast.logging.converter.DebugConverter" />
                  
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%AST_DEBUG(%-5level [%class{16}] [line:%L]) %m%n</pattern>
        </encoder>
    </appender>

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

    @Test
    public void testLogger() {
        logger.debug("debug {}", "testLogger");
        logger.info("info {}", "testLogger");
        logger.warn("warn {}", "testLogger");
        logger.error("error {}", "testLogger");
    }


generates following output

DEBUG [c.a.a.CustomDelimitedFileSourceTest] [line:120] debug testLogger
 info testLogger
WARN  [c.a.a.CustomDelimitedFileSourceTest] [line:122] warn testLogger
ERROR [c.a.a.CustomDelimitedFileSourceTest] [line:123] error testLogger



> On Apr 15, 2015, at 3:23 AM, Ceki Gülcü <ceki at qos.ch> wrote:
> 
> 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>
> _______________________________________________
> Logback-user mailing list
> Logback-user at qos.ch
> http://mailman.qos.ch/mailman/listinfo/logback-user

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.qos.ch/pipermail/logback-user/attachments/20150415/13d04328/attachment-0001.html>


More information about the Logback-user mailing list