spring boot 使用代码配置 logback 并动态输出到不同文件
1.用于存放log的参数@Datapublic class CommonLogConfigEntity {private String baseDir;private Integer maxHistory;private String maxFileSize;}@Datapublic class CommonLogProperties {private static final String ba
·
1.用于存放log的参数
@Data
public class CommonLogConfigEntity {
private String baseDir;
private Integer maxHistory;
private String maxFileSize;
}
@Data
public class CommonLogProperties {
private static final String baseDir = "logs";
private static final Integer maxHistory = 30;
private static final String maxFileSize = "50MB";
private static final Map<String, CommonLogConfigEntity> config = new HashMap<>();
public static CommonLogConfigEntity get(String loggerName) {
CommonLogConfigEntity configEntity = config.get(loggerName);
if (configEntity == null) {
configEntity = new CommonLogConfigEntity();
configEntity.setBaseDir(baseDir);
configEntity.setMaxFileSize(maxFileSize);
configEntity.setMaxHistory(maxHistory);
}
return configEntity;
}
public static void put(String loggerName, String baseDir, Integer maxHistory, String maxFileSize) {
CommonLogConfigEntity configEntity = new CommonLogConfigEntity();
configEntity.setBaseDir(baseDir);
configEntity.setMaxFileSize(maxFileSize);
configEntity.setMaxHistory(maxHistory);
config.put(loggerName, configEntity);
}
}
2.自定义LoggerFactory
public class CommonLoggerFactory {
private static final Map<String, Logger> loggers = new HashMap<>();
public static Logger getLogger(String name) {
Logger logger = loggers.get(name);
if (logger != null) {
return logger;
}
synchronized (CommonLoggerFactory.class) {
logger = loggers.get(name);
if (logger != null) {
return logger;
}
logger = createLogger(name);
loggers.put(name, logger);
}
return logger;
}
public static Logger createLogger(String loggerName) {
CommonLogConfigEntity configEntity = CommonLogProperties.get(loggerName);
LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
RollingFileAppender<ILoggingEvent> rollingFileAppender = new RollingFileAppender<>();
rollingFileAppender.setContext(loggerContext);
rollingFileAppender.setAppend(true);
rollingFileAppender.setName(loggerName + "LogAppender");
rollingFileAppender.setFile(configEntity.getBaseDir() + "/" + loggerName + ".log");
SizeAndTimeBasedRollingPolicy rollingPolicy = new SizeAndTimeBasedRollingPolicy<>();
rollingPolicy.setFileNamePattern(configEntity.getBaseDir() + "/" + loggerName + ".%d{yyyy-MM-dd}.%i.log");
rollingPolicy.setMaxHistory(configEntity.getMaxHistory());
rollingPolicy.setContext(loggerContext);
rollingPolicy.setParent(rollingFileAppender);
rollingPolicy.setMaxFileSize(FileSize.valueOf(configEntity.getMaxFileSize()));
rollingPolicy.setCleanHistoryOnStart(true);
rollingPolicy.start();
rollingFileAppender.setRollingPolicy(rollingPolicy);
PatternLayoutEncoder encoder = new PatternLayoutEncoder();
encoder.setPattern("%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %logger{50} %msg%n");
encoder.setCharset(Charset.forName("UTF-8"));
encoder.setContext(loggerContext);
encoder.start();
rollingFileAppender.setEncoder(encoder);
rollingFileAppender.start();
Logger logger = loggerContext.getLogger(loggerName);
logger.addAppender(rollingFileAppender);
return logger;
}
public static void registerLogger(String loggerName, String baseDir, Integer maxHistory, String maxFileSize) {
CommonLogProperties.put(loggerName, baseDir, maxHistory, maxFileSize);
}
}
3.初始化Logger对象
在CommonLogProperties 中预设了日志的三个参数,切割日志的大小(KB,MB,GB),保存的时间和保存的目
private static final String baseDir = "logs";
private static final Integer maxHistory = 30;
private static final String maxFileSize = "50MB";
当有需要自定这些参数时,可以重新注册一个自定义参数的logger
CommonLoggerFactory.registerLogger("api","logs/",1,"50MB");
更多推荐
已为社区贡献1条内容
所有评论(0)