我创建了一个 loggingFraction 方法来实现这个目标。像那样:
protected LoggingFraction logging() {
String logName = swarm.stageConfig().resolve("application.name").getValue();
String category = swarm.stageConfig().resolve("logger.category").getValue();
String levelName =swarm.stageConfig().resolve("logger.level").getValue();
final String logFile = System.getProperty("user.dir") + File.separator+
"target"+File.separator+
logName+".log";
LoggingFraction loggingFraction = new LoggingFraction()
.periodicSizeRotatingFileHandler(logName,(h)->{
h.level(Level.valueOf(levelName))
.append(true)
.suffix(".yyyy-MM-dd")
.rotateSize("30m")
.enabled(true)
.encoding("UTF-8")
.maxBackupIndex(2);
Map<String,String> fileSpec = new HashMap<>();
fileSpec.put("path", logFile);
h.file(fileSpec);
}).logger(GetinApp.class.getPackage().getName(),(l)->{
l.level(Level.valueOf(levelName))
.handler(logName);
});;
})
List<String> categories = Arrays.asList(category.split(","));
categories.forEach(c->{
loggingFraction.logger(c.trim(),l->{
l.level(Level.valueOf(levelName)).handler(logName);
});
});
return loggingFraction;
}
所以我可以在project-stages.yml上声明我自己的属性,例如:
project-name:
logger:
level: DEBUG
category: com.example.com, org.anotherexample.com