lauantai 9. toukokuuta 2015

Spring-boot and logging with logback

I'm making a website with spring-boot and wanted to log the remote host or IP. Couldn't do it straight away and it took a while to find that I needed a filter to achieve what I wanted.

<appender name="dailyRollingFileAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <File>logs/mylog.log</File>
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
        <FileNamePattern>logs/mylog.%d{yyyy-MM-dd}.log</FileNamePattern>
        <maxHistory>5</maxHistory>
    </rollingPolicy>

    <encoder>
        <Pattern>%d{HH:mm:ss.SSS} %-4r [%thread] %-0level %X{req.remoteHost} %X{req.requestURI} %logger{20} - %msg %n</Pattern>
    </encoder>
</appender>


Before setting MDCInsertingServletFilter, patterns like %X{req.*} didn't work at all.

With spring-boot, you can set the filter with:


@Bean
public MDCInsertingServletFilter mdcInsertingServletFilter() {
    return new MDCInsertingServletFilter();
}

and the patterns work nicely.

Ei kommentteja:

Lähetä kommentti

Huomaa: vain tämän blogin jäsen voi lisätä kommentin.