All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.icthh.xm.commons.logging.configurable.LogMaskingConfiguration Maven / Gradle / Ivy

There is a newer version: 4.0.12
Show newest version
package com.icthh.xm.commons.logging.configurable;

import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.Layout;
import ch.qos.logback.core.OutputStreamAppender;
import ch.qos.logback.core.encoder.Encoder;
import ch.qos.logback.core.encoder.LayoutWrappingEncoder;
import com.icthh.xm.commons.logging.config.LoggingConfigService;
import com.icthh.xm.commons.logging.util.MaskingLayout;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;

import static org.springframework.core.Ordered.HIGHEST_PRECEDENCE;

@Slf4j
@Configuration
@Order(HIGHEST_PRECEDENCE)
public class LogMaskingConfiguration {

    public LogMaskingConfiguration(LoggingConfigService loggingConfigService) {
        log.info("Init MaskingPatternLayout");
        LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
        context.getLogger("ROOT").iteratorForAppenders().forEachRemaining(it -> {
            if (it instanceof OutputStreamAppender) {
                var appender = (OutputStreamAppender) it;
                Encoder encoder = appender.getEncoder();
                if (encoder instanceof LayoutWrappingEncoder) {
                    LayoutWrappingEncoder layoutEncoder = (LayoutWrappingEncoder) encoder;
                    Layout layout = layoutEncoder.getLayout();

                    LayoutWrappingEncoder maskingEncoder = new LayoutWrappingEncoder<>();
                    maskingEncoder.setLayout(new MaskingLayout(layout, loggingConfigService));
                    maskingEncoder.setContext(context);
                    maskingEncoder.setCharset(layoutEncoder.getCharset());
                    maskingEncoder.start();

                    appender.setEncoder(maskingEncoder);
                }
            }
        });
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy