简体   繁体   中英

Directing log events to different log files based on MDC

I am new to java and need to place logs using slf4j, I need to put sensitive data logs into a different file.

Example:

package in.com.my.service.here

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

public static void main( args[] ) {
    Logger log = LoggerFactory.getLogger(POSServiceImpl.class);

    log.info("log without MDC");

    MDC.put('isEncrypted',"true");

    log.info("log line 1");
    log.info("log line 2");
    log.info("log line 3");

    MDC.clear();   

    log.info("log again without MDC");
}

Important: please provide the logback.xml configuration such that the logs under MDC are store in different file when isEncrypted value is true.

<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="true">
    <timestamp key="bySecond" datePattern="yyyy-MM-dd_HHmmss" />

    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{5} - %msg%n
            </pattern>
        </encoder>
    </appender>

    <appender name="ROLLING"
        class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${log.folder}/terminal-${bySecond}.log</file>
        <rollingPolicy
            class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            <fileNamePattern>${log.folder}/Archive-%d{yyyy-MM-dd}.%i.log
            </fileNamePattern>
            <maxFileSize>10MB</maxFileSize>
            <maxHistory>30</maxHistory>
            <totalSizeCap>1GB</totalSizeCap>
        </rollingPolicy>
        <encoder>
            <pattern>My ID [%X{myId}] %d{HH:mm:ss.SSS} [%thread]
                %-5level %logger{5} - %msg%n</pattern>
        </encoder>
    </appender>


    <logger name="in.com.my.service" level="debug"
        additivity="false">
        <appender-ref ref="ROLLING" />
        <appender-ref ref="STDOUT" />
    </logger>
    <!-- By default, the level of the root level is set to DEBUG -->
    <root level="info">
        <appender-ref ref="ROLLING" />
        <appender-ref ref="STDOUT" />
    </root>
</configuration>

This looks like a candidate for Logback's SiftingAppender , javadoc . For example:

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

    <appender name="SIFTER" class="ch.qos.logback.classic.sift.SiftingAppender">
        <discriminator>
            <key>encryptedStatus</key>
            <defaultValue></defaultValue>
        </discriminator>
        <sift>
            <appender name="FILE${encryptedStatus}" class="ch.qos.logback.core.FileAppender">
                <file>File${encryptedStatus}.log</file>
                <layout class="ch.qos.logback.classic.PatternLayout">
                    <pattern>...</pattern>
                </layout>
            </appender>
        </sift>
    </appender>

    <root level="ALL">
        <appender-ref ref="SIFTER" />
    </root>
</configuration>

The value of encryptedStatus will be substituted into the log file name. So, log events which contain the MDC value encryptedStatus=Sensitive will be written to FileSensitive.log and log events which do not contain a MDC attribute named encryptedStatus will be written to File.log .

You'll likely want to change the values of file name and the suffix for the sensitive files etc but the above extract shows you how you can direct log events to specific log files based on an MDC attribute.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM