[logback-user] Evaluator and Layout Pattern

Ceki Gulcu listid at qos.ch
Tue Oct 14 18:41:48 CEST 2008


Hi Ekke,

Here is the code for MarkerConverter:

======= Start MarkerConverter.hava ========
package ch.qos.logback.classic.pattern;

import org.slf4j.Marker;

import ch.qos.logback.classic.spi.LoggingEvent;

/**
  * Return the event's marker value(s).
  *
  * @author Sébastien Pennec
  */
public class MarkerConverter extends ClassicConverter {

   private static String EMPTY = "";

   public String convert(LoggingEvent le) {
     Marker marker = le.getMarker();
     if (marker == null) {
       return EMPTY;
     } else {
       return marker.toString();
     }
   }
}
======= End   MarkerConverter.hava ========

Studying the code above, I would say that you have two options. First,
just remove the [] from the conversion pattern. So instead of writing

"%d %level [%marker] - %msg%n"

as you conversion pattern, just write

"%d %level %marker - %msg%n"

You second option is to write your own converter for handling
markers. You could modify it to return an empty string if there is no
marker, and to return "["+marker.toString()+"]" if there is a marker
in the event. Here is sample code:

package de.gentz-software.logback;

import org.slf4j.Marker;
import ch.qos.logback.classic.spi.LoggingEvent;

public class MyMarkerConverter extends ClassicConverter {

   private static String EMPTY = "";

   public String convert(LoggingEvent le) {
     Marker marker = le.getMarker();
     if (marker == null) {
       return EMPTY;
     } else {
       return "["+marker.toString()+"]";
     }
   }
}


You also need to let logback know about your conversion word. Here is
how:

   <conversionRule conversionWord="myMarker"
            converterClass="de.gentz-software.logback.MyMarkerConverter"/>

Here is a sample config file:

<configuration>
   <conversionRule conversionWord="myMarker"
           converterClass="de.gentz-software.logback.MyMarkerConverter" />
	
   <appender name="STDOUT"
     class="ch.qos.logback.core.ConsoleAppender">
     <layout class="ch.qos.logback.classic.PatternLayout">
       <Pattern>%-4relative [%thread] %myMarker - %msg%n</Pattern>
     </layout>
   </appender>

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


I hope this helps,

ekkehard wrote:
> ekkehard schrieb:
>> Ceki Gulcu schrieb:
>>> Hello Ekke,
>>>
>>> MarkerConverter which handles the %marker conversion word, does not take an 
>>> evaluator as an option. May I ask what is it that you are trying to accomplish?
>>>
>>>   
>> if there's no marker, then I dont want to print
>> -[]-
>>
>> but if theres a marker I want to print
>> -[%marker{HAS_MARKER}]-
>>
>> ekke
>>   
>>
> I mean
> 
> -[%marker]-
> 
> (just learned from your mail that I cannot use an evakluator there ;-)

-- 
Ceki Gülcü
Logback: The reliable, generic, fast and flexible logging framework for Java.
http://logback.qos.ch


More information about the Logback-user mailing list