org.zodiac.monitor.logging.LogBackConfiguration Maven / Gradle / Ivy
package org.zodiac.monitor.logging;
import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.core.Appender;
import ch.qos.logback.core.AsyncAppenderBase;
import ch.qos.logback.core.CoreConstants;
import ch.qos.logback.core.OutputStreamAppender;
import ch.qos.logback.core.encoder.Encoder;
import ch.qos.logback.core.encoder.LayoutWrappingEncoder;
import ch.qos.logback.core.spi.AppenderAttachable;
import org.zodiac.commons.constants.SystemPropertiesConstants;
import org.zodiac.monitor.logging.appender.logback.AdvancedKafkaAppender;
import org.zodiac.monitor.logging.appender.logback.layout.CustomJsonLayout;
import org.zodiac.monitor.logging.mask.LogbackSensitiveDataConverter;
import org.springframework.core.env.Environment;
import org.springframework.util.ReflectionUtils;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
public class LogBackConfiguration extends LogConfiguration {
private static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(LogBackConfiguration.class);
private static boolean SKYWALLKING_ENABLE = true;
static {
try {
Class.forName("org.apache.skywalking.apm.toolkit.log.logback.v1.x.TraceIdPatternLogbackLayout");
} catch (ClassNotFoundException ignored) {
SKYWALLKING_ENABLE = false;
}
}
public LogBackConfiguration(Environment env) {
super(env);
}
private void createBizLogger() {
if (env.containsProperty(SystemPropertiesConstants.Zodiac.MANAGEMENT_EXPORT_LOG_KAFKA_BOOTSTRAPSERVERS)) {
LoggerContext content = (LoggerContext)org.slf4j.LoggerFactory.getILoggerFactory();
AdvancedKafkaAppender kafkaAppender = new AdvancedKafkaAppender();
kafkaAppender.setLayout(new CustomJsonLayout());
kafkaAppender.setTopic(getKafkaTopic());
kafkaAppender.setBootstrapServers(getBootstrapservers());
kafkaAppender.setContext(content);
kafkaAppender.start();
Logger logger = (Logger)org.slf4j.LoggerFactory.getLogger("BizLogger");
logger.addAppender(kafkaAppender);
logger.setLevel(Level.INFO);
logger.setAdditive(false);
}
}
@Override
public void init() {
if (env.containsProperty(LogbackSensitiveDataConverter.ENV_KEY_REGEX) || Boolean.TRUE.toString()
.equals(env.getProperty(SystemPropertiesConstants.Zodiac.MANAGEMENT_EXPORT_LOGS_SENSITIVE_ENABLE_GLOBAL))) {
addSensitiveDataConverter();
}
LoggerContext loggerContext = (LoggerContext)org.slf4j.LoggerFactory.getILoggerFactory();
if (loggerContext == null)
return;
List loggerList = loggerContext.getLoggerList();
Logger loggerKafka = loggerContext.getLogger("org.apache.kafka.clients.producer.ProducerConfig");
if (loggerKafka != null) {
loggerKafka.setLevel(Level.ERROR);
}
createBizLogger();
for (Logger logger : loggerList) {
AppenderAttachable appenderAttachable = logger;
setLayout(loggerContext, appenderAttachable.iteratorForAppenders());
}
}
public static void addSensitiveDataConverter() {
LoggerContext loggerContext = (LoggerContext)org.slf4j.LoggerFactory.getILoggerFactory();
if (loggerContext == null) {
log.error("loggerContext is null. 无法添加脱敏规则 !");
}
try {
Map ruleRegistry = (Map)loggerContext.getObject(CoreConstants.PATTERN_RULE_REGISTRY);
if (ruleRegistry == null) {
ruleRegistry = new HashMap();
loggerContext.putObject(CoreConstants.PATTERN_RULE_REGISTRY, ruleRegistry);
}
String conversionWord = "msg";
String converterClass = LogbackSensitiveDataConverter.class.getCanonicalName();
ruleRegistry.put(conversionWord, converterClass);
} catch (Exception oops) {
log.error(oops.getMessage(), oops);
}
}
private void setLayout(LoggerContext loggerContext, Iterator> iterator) {
if (!SKYWALLKING_ENABLE) {
return;
}
while (iterator.hasNext()) {
Appender appender = iterator.next();
if (appender instanceof OutputStreamAppender) {
setLayout(loggerContext, (OutputStreamAppender>)appender);
} else if (appender instanceof AsyncAppenderBase) {
AsyncAppenderBase asyncAppenderBase = (AsyncAppenderBase)appender;
setLayout(loggerContext, asyncAppenderBase.iteratorForAppenders());
}
}
}
private void setLayout(LoggerContext loggerContext, OutputStreamAppender outputStreamAppender) {
Encoder> encoder = outputStreamAppender.getEncoder();
if (encoder instanceof LayoutWrappingEncoder) {
org.apache.skywalking.apm.toolkit.log.logback.v1.x.TraceIdPatternLogbackLayout traceIdLayOut =
new org.apache.skywalking.apm.toolkit.log.logback.v1.x.TraceIdPatternLogbackLayout();
traceIdLayOut.setContext(loggerContext);
traceIdLayOut.setPattern(getLogBackPattern());
traceIdLayOut.start();
Field field = ReflectionUtils.findField(encoder.getClass(), "layout");
field.setAccessible(true);
ReflectionUtils.setField(field, encoder, traceIdLayOut);
}
}
}