3

我正在使用tinylog来满足我的日志记录需求,并且想知道是否有人知道一种登录文件控制台的方法。当我使用下面的配置时,我只能在控制台上获得输出。当我删除.writer(new ConsoleWriter())日志记录仅对文件进行(正如人们所期望的那样)。

Configurator.currentConfig()
                          .level(LoggingLevel.valueOf("TRACE"))
                          .writer(new RollingFileWriter(file,10))
                          .writer(new ConsoleWriter())
                          .activate();
4

3 回答 3

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();
于 2013-10-04T11:34:09.917 回答
3

根据文档,这是可能的(现在),“技巧”是调用addWriter()而不是多次调用writer()方法。

引用http://www.tinylog.org/configuration#writers

可以并行使用多个写入器。例如,可以将日志条目同时写入控制台和日志文件。例子:

Configurator.currentConfig()    
  .writer(new ConsoleWriter())    
  .addWriter(new FileWriter("log.txt"))    
  .activate();
于 2015-12-12T11:25:16.603 回答
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();
于 2017-08-08T14:34:49.350 回答