Patrick
Patrick

Reputation: 12744

How to use request param as log identifier in logback

In my controller I get an id as request param. Is there a way to use this id to identify all the log entries of this particular request?

@ResponseBody
@RequestMapping(value="/anyRequest", method = RequestMethod.GET)
public String doAnything(@RequestParam(value="Id", required = true) long id) {
    logger.info(id);
    return "";
}

Means anywhere of this entry should be the id value:

2017-02-28 08:30:41.035 INFO 23050 --- [http-bio-1084-exec-20] AnyServiceImpl ...

Im using logback and have this configuration:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>

    <springProfile name="dev">
       <property name="FILE_PATH" value="C:\\DATA\\temp" />
    </springProfile>

    <appender name="FILE-AUDIT"
        class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${FILE_PATH}/service.log</file>
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <Pattern>
                %d{yyyy-MM-dd HH:mm:ss.SSS} ${LOG_LEVEL_PATTERN:-%5p} ${PID:- } --- [%t] %-40.40logger{39} : %m%n
            </Pattern>
        </encoder>

        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!-- rollover daily -->
            <fileNamePattern>service.%d{yyyy-MM-dd}.%i.log
            </fileNamePattern>
            <timeBasedFileNamingAndTriggeringPolicy
                class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <maxFileSize>25MB</maxFileSize>
            </timeBasedFileNamingAndTriggeringPolicy>
            <maxHistory>90</maxHistory>
        </rollingPolicy>
    </appender>

    <appender name="consoleAppender" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <charset>UTF-8</charset>
            <Pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} ${LOG_LEVEL_PATTERN:-%5p} ${PID:- } --- [%t] %-40.40logger{39} : %m%n</Pattern>
        </encoder>
    </appender>

    <logger name="Service" level="info"
        additivity="false">
        <appender-ref ref="FILE-AUDIT" />
        <appender-ref ref="consoleAppender" />
    </logger>

    <root level="info">
        <appender-ref ref="FILE-AUDIT" />
        <appender-ref ref="consoleAppender" />
    </root>

</configuration>

And I use the logger in this way:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

private final Logger logger = LoggerFactory.getLogger(this.getClass());

Is there any Spring out of the box magic?

Upvotes: 2

Views: 2806

Answers (1)

Monzurul Shimul
Monzurul Shimul

Reputation: 8396

Use MDC to achieve this.

Controller Method:

    @ResponseBody
    @RequestMapping(value="/anyRequest", method = RequestMethod.GET)
    public String doAnything(@RequestParam(value="Id", required = true) long id) {
        try {
            MDC.put("id", id);
            logger.info(id);
            return "";
        } finally {
            org.slf4j.MDC.clear();
        }
    }

Logback.xml:

<Pattern>[%X{id}] %d{yyyy-MM-dd HH:mm:ss.SSS} ${LOG_LEVEL_PATTERN:-%5p} ${PID:- } --- [%t] %-40.40logger{39} : %m%n</Pattern>

Upvotes: 2

Related Questions