我正在使用tinylog来满足我的日志记录需求,并且想知道是否有人知道如何登录文件和控制台。当我使用下面的配置时,我只在控制台上输出。当我删除.writer(new ConsoleWriter())
时,日志记录仅对文件进行(正如人们所期望的那样)。
Configurator.currentConfig()
.level(LoggingLevel.valueOf("TRACE"))
.writer(new RollingFileWriter(file,10))
.writer(new ConsoleWriter())
.activate();
答案 0 :(得分:3)
AFAIK使用开箱即用的tinylog是不可能的,但你可以随时实现自己的复合作家:
public class MultiWriter implements LoggingWriter {
private List<LoggingWriter> writers;
public MultiWriter(List<LoggingWriter> writers) {
this.writers = writers;
}
@Override
public void write(LoggingLevel level, String logEntry) {
for (LoggingWriter writer : writers) {
writer.write (level, logEntry);
}
}
}
然后像这样使用它:
Configurator.currentConfig()
.level(LoggingLevel.valueOf("TRACE"))
.writer(new MultiWriter(Arrays.asList(
new RollingFileWriter(file,10), new ConsoleWriter()))
.activate();
答案 1 :(得分:3)
根据文档,这是可能的(现在),&#39;技巧&#39;是对 addWriter()的调用,而不是对 writer()方法的多次调用。
引用http://www.tinylog.org/configuration#writers:
可以并行使用多个写入器。例如,可以将日志条目同时写入控制台和日志文件。 例如:
Configurator.currentConfig()
.writer(new ConsoleWriter())
.addWriter(new FileWriter("log.txt"))
.activate();
答案 2 :(得分:0)
You can use multiple file writers to write logs on different logging level.
According to docs
1. Using property file
tinylog.writer1 = console
tinylog.writer1.level = trace
tinylog.writer2 = file
tinylog.writer2.filename = log.txt
tinylog.writer2.level = info
2. Using java code
Configurator.currentConfig()
.writer(new ConsoleWriter(), Level.TRACE)
.addWriter(new FileWriter("log.txt"), Level.INFO)
.activate();