
Is it possible to have a logbook-text.xml file that defines two different patterns? For example if the level is debug, I want the level, time stamp, thread, file, line number, and message. If the level is error or warn, I want the level and message If the level is info I just want the message Thanks Andy

There is no out-of-the-box support for level-specific patterns in logback at this time. You could of course write your own layout which does what you describe. See http://logback.qos.ch/manual/layouts.html#writingYourOwnLayout for documentation on this topic. 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. On 4/10/2015 21:36, Andrew E. Davidson wrote:
Is it possible to have a logbook-text.xml file that defines two different patterns? For example
if the level is debug, I want the level, time stamp, thread, file, line number, and message.
If the level is error or warn, I want the level and message
If the level is info I just want the message
Thanks
Andy

Thanks Ceki Andy
On Apr 10, 2015, at 2:58 PM, Ceki Gülcü <ceki@qos.ch> wrote:
There is no out-of-the-box support for level-specific patterns in logback at this time.
You could of course write your own layout which does what you describe. See http://logback.qos.ch/manual/layouts.html#writingYourOwnLayout for documentation on this topic.
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.
On 4/10/2015 21:36, Andrew E. Davidson wrote:
Is it possible to have a logbook-text.xml file that defines two different patterns? For example
if the level is debug, I want the level, time stamp, thread, file, line number, and message.
If the level is error or warn, I want the level and message
If the level is info I just want the message
Thanks
Andy
Logback-user mailing list Logback-user@qos.ch http://mailman.qos.ch/mailman/listinfo/logback-user

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>

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>

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@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@qos.ch http://mailman.qos.ch/mailman/listinfo/logback-user
participants (2)
-
Andrew E. Davidson
-
Ceki Gülcü