com.icthh.xm.commons.logging.configurable.LogMaskingConfiguration Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of xm-commons-logging-configurable Show documentation
Show all versions of xm-commons-logging-configurable Show documentation
xm-commons-logging-configurable
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);
}
}
});
}
}