我们有一个申请。我们有条件。基于条件,如果是真的那么我们会将一些日志消息写入一个文件,否则我们会将消息记录到另一个文件。
并且应该根据条件而不是基于日志级别进行日志记录。
使用yaml文件在dropwizard中如何实现?
答案 0 :(得分:2)
支持开箱即用。这是我的例子:
server:
rootPath: /api/*
requestLog:
appenders: []
applicationConnectors:
- type: http
port: 9085
logging:
level: INFO
loggers:
"my-log-1":
level: DEBUG
additive: false
appenders:
- type: file
currentLogFilename: /home/artur/var/log/test1.log
archivedLogFilenamePattern: /home/artur/var/log/test1.log%d.log.gz
archivedFileCount: 5
logFormat: '[%level] %msg%n'
"my-log-2":
level: DEBUG
additive: false
appenders:
- type: file
currentLogFilename: /home/artur/var/log/test2.log
archivedLogFilenamePattern: /home/artur/var/log/test2.log%d.log.gz
archivedFileCount: 5
注意:您无法在配置中使用选项卡。
此配置创建2个记录器。首先称为" my-log-1",第二个称为" my-log-2"。
您现在可以在java类中创建这些记录器,例如在我的应用程序中:
public class Application extends io.dropwizard.Application<Configuration>{
private static final Logger log = Logger.getLogger("my-log-1");
private static final Logger log2 = Logger.getLogger("my-log-2");
@Override
public void run(Configuration configuration, Environment environment) throws Exception {
log.info("Test1"); // writes to first file
log2.info("Test2"); // logs to seconds file
}
public static void main(String[] args) throws Exception {
new Application().run("server", "/home/artur/dev/repo/sandbox/src/main/resources/config/test.yaml");
}
}
请注意文件顶部的两个记录器及其创建。
您现在可以像任何记录器一样使用它们。添加条件并注销:
int random = new Random().nextInt();
if(random % 2 == 0) {
log.info("Test1"); // writes to first file
} else {
log2.info("Test2"); // logs to seconds file
}
我希望能回答你的问题,
感谢,
阿图尔