io.irain.reactor.logging.appender.LoggingJsonFileAppender Maven / Gradle / Ivy
The newest version!
package io.irain.reactor.logging.appender;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.rolling.RollingFileAppender;
import cn.hutool.json.JSONUtil;
import io.irain.reactor.logging.properties.LoggingProperties;
import io.irain.reactor.logging.utils.LogStashUtil;
import io.irain.reactor.logging.utils.LoggingUtil;
import lombok.extern.slf4j.Slf4j;
import net.logstash.logback.encoder.LoggingEventCompositeJsonEncoder;
import org.slf4j.LoggerFactory;
import org.springframework.boot.logging.LoggingSystemProperty;
import org.springframework.core.env.Environment;
import java.util.HashMap;
import java.util.Map;
/**
* json 日志输出,json 日志只输出 all.log
*
* @author youta
*/
@Slf4j
public class LoggingJsonFileAppender implements ILoggingAppender {
private final LoggingProperties properties;
private final String logAllFile;
private final String customFieldsJson;
char SLASH = '/';
/**
* 构造器
* @param environment environment
* @param properties properties
*/
public LoggingJsonFileAppender(Environment environment,
LoggingProperties properties) {
this.properties = properties;
// 1. 服务名和环境和日志目录
String appName = environment.getRequiredProperty("spring.application.name");
String profile = environment.getRequiredProperty("spring.profiles.active");
// 2. 文件日志格式
String fileLogPattern = environment.resolvePlaceholders(LoggingUtil.DEFAULT_FILE_LOG_PATTERN);
System.setProperty(LoggingSystemProperty.FILE_PATTERN.getEnvironmentVariableName(), fileLogPattern);
// 3. 生成日志文件的文件
String logDir = environment.getProperty("logging.file.path", LoggingUtil.DEFAULT_LOG_DIR);
this.logAllFile = logDir + SLASH + appName + SLASH + LoggingUtil.LOG_FILE_ALL;
// 4. json 自定义字段
Map customFields = new HashMap<>(4);
customFields.put("appName", appName);
customFields.put("profile", profile);
this.customFieldsJson = JSONUtil.toJsonStr(customFields);
LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
this.start(context);
}
@Override
public void start(LoggerContext context) {
log.info("JsonFile logging start.");
reload(context);
}
@Override
public void reset(LoggerContext context) {
log.info("JsonFile logging start.");
reload(context);
}
private void reload(LoggerContext context) {
LoggingProperties.Files files = properties.getFiles();
if (files.isEnabled() && files.isUseJsonFormat()) {
addAllFileAppender(context, logAllFile, customFieldsJson);
}
}
/**
* addJsonConsoleAppender.
*
* @param context a {@link LoggerContext} object.
* @param customFields a {@link String} object.
*/
private static void addAllFileAppender(LoggerContext context,
String logFile,
String customFields) {
final RollingFileAppender allFileAppender = new RollingFileAppender<>();
allFileAppender.setContext(context);
allFileAppender.setEncoder(compositeJsonEncoder(context, customFields));
LoggingFileAppender.setFileAppender(context, logFile, allFileAppender);
}
private static LoggingEventCompositeJsonEncoder compositeJsonEncoder(LoggerContext context,
String customFields) {
final LoggingEventCompositeJsonEncoder compositeJsonEncoder = new LoggingEventCompositeJsonEncoder();
compositeJsonEncoder.setContext(context);
compositeJsonEncoder.setProviders(LogStashUtil.jsonProviders(context, customFields));
compositeJsonEncoder.start();
return compositeJsonEncoder;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy