Question

I want to use org.slf4j.Marker in my application. I have already a logback.xml file which works correctly without Markers. Java code is like this:

    public class SimpleActivitiIntegrationService implements ActivitiIntegrationService {
        private static final Marker PROCESS_INTEGRATION_MARKER = MarkerFactory.getMarker("PROCESS_INT");
        private static final Logger LOGGER = LoggerFactory.getLogger(SimpleActivitiIntegrationService.class);
        ...
        public void sendRequest(String s) throws RuntimeException {
            LOGGER.debug(PROCESS_INTEGRATION_MARKER, "Method started:%s", "sendRequest");
    }
}

and the logback.xml file is like this:

<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>${log.path}/${log.file.name}.${log.file.extension}</file>

    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
        <!-- daily rollover -->
        <fileNamePattern>
            ${log.path}/${archieve.dir.name}/${log.file.name}.%d{yyyy-MM-dd}.%i.${log.file.extension}
        </fileNamePattern>

        <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
            <!-- or whenever the file size reaches 10MB -->
            <maxFileSize>10MB</maxFileSize>
        </timeBasedFileNamingAndTriggeringPolicy>

        <!-- keep 30 days' worth of history -->
        <maxHistory>30</maxHistory>
    </rollingPolicy>

    <encoder>
        <pattern>
            %d [%X{contexPath}] [%thread] %-5level %logger{35} %method - %msg%n
        </pattern>
    </encoder>
    <turboFilter class="ch.qos.logback.classic.turbo.MarkerFilter">
        <Name>PROCESS_INT_FILTER</Name>
        <Marker>PROCESS_INT</Marker>
        <OnMatch>ACCEPT</OnMatch>
        <OnMismatch>NEUTRAL</OnMismatch>
    </turboFilter>
</appender>

but I'm definitely not sure about the usage of Marker's in logback.xml file.

Output of this configuration is:

2014-03-21 17:25:17,041 [] [http-bio-8080-exec-1] DEBUG t.c.i.s.a.s.e.SimpleActivitiIntegrationService sendRequest - Method started:%s

What is the problem in my configuration? Thanks in advance.

EDIT: Adding evaluator to the Appender:

<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <evaluator class="ch.qos.logback.classic.boolex.OnMarkerEvaluator">
        <marker>PROCESS_INT</marker>
    </evaluator>
    <!-- encoders are assigned the type ch.qos.logback.classic.encoder.PatternLayoutEncoder
        by default -->
    <encoder>
        <pattern>
            %d [%X{contextPath}] [%thread] %-5level %logger{35} %method - %msg%n
        </pattern>
    </encoder>
</appender>

I got this:

2014-03-25 02:06:58,892 [] [http-bio-8080-exec-1] DEBUG t.c.i.s.a.s.e.SimpleActivitiIntegrationService sendRequest - Response acquired payloadType:%s, respCode:%s

EDIT2:

I got this

2014-03-25 02:06:58,892 [] [http-bio-8080-exec-1] DEBUG t.c.i.s.a.s.e.SimpleActivitiIntegrationService sendRequest - Method started:sendRequest

but I think I should get something starts with PROCESS_INT, am I wrong?

Was it helpful?

Solution

Since you are using a Turbo Filter instead of a regular filter, it is tied to the overall context, not to a specific appender. So, you'll need to define it outside: e.g. after the tag.

If you want to use it on a specific appender, you could use a regular filter.

<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <evaluator class="ch.qos.logback.classic.boolex.OnMarkerEvaluator">
      <marker>PROCESS_INT</marker>
    </evaluator>
... ... etc.    ...  ...

To check for that marker and allow or disallow for that appender.

For the formatting, when you log the message inside Java, instead of the "%s" style formatting, you need to use "{}".

LOGGER.debug(PROCESS_INTEGRATION_MARKER, "Method started:{}", "sendRequest");

Essentially, this is pretty similar to formatting the message yourself, but it could have performance benefits in some situations.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top