4

I'm using tinylog for my logging needs and were wondering if anybody knows a way to log to a file and the console. When I use the configuration below I only get output on the console. When I remove .writer(new ConsoleWriter())the logging is done only to file (as one would expect).

Configurator.currentConfig()
                          .level(LoggingLevel.valueOf("TRACE"))
                          .writer(new RollingFileWriter(file,10))
                          .writer(new ConsoleWriter())
                          .activate();
joschi
  • 12,746
  • 4
  • 44
  • 50
cete3
  • 647
  • 6
  • 19
  • 2
    On a sidenote, to anybody who's looking for really easy logging - I can highly recommend tinylog. – cete3 Oct 04 '13 at 11:27

4 Answers4

3

AFAIK this is not possible with out-of-the-box tinylog, but you can always implement your own composite writer like this:

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);
      }
   }
}

And then use it like this:

Configurator.currentConfig()
                          .level(LoggingLevel.valueOf("TRACE"))
                          .writer(new MultiWriter(Arrays.asList(
                             new RollingFileWriter(file,10), new ConsoleWriter()))
                          .activate();
Mureinik
  • 297,002
  • 52
  • 306
  • 350
  • Of course you'd probably want to do something more robust (i.e., safe) than just saving a List that was passed to the constructor, but I preferred to keep the answer's code minimal so it'll be easier to read. – Mureinik Oct 04 '13 at 11:38
  • Thanks for the elaborate answer. I figured as much, but just wanted to be sure before I started writing my own `Writer`. – cete3 Oct 04 '13 at 12:58
3

According to the docs, this is possible (now), the 'trick' being the the call to addWriter() instead of multiple calls to the writer() method.

Quoting http://www.tinylog.org/configuration#writers:

Multiple writers can be used in parallel. For example, it is possible to write log entries to the console and to a log file simultaneously. Example:

Configurator.currentConfig()    
  .writer(new ConsoleWriter())    
  .addWriter(new FileWriter("log.txt"))    
  .activate();
jotomo
  • 489
  • 4
  • 9
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();
0

For tinylog v2, it is also possible:

Example:

writer1       = console
writer1.level = debug

writer2       = file
writer2.level = info
writer2.file  = log.txt

Multiple writers can be used in parallel. For example, it is possible to write log entries to the console and to a log file. Each writer must be defined via a unique property that starts with "writer". These can be meaningful names, such as writerConsole or writerFile, or simply consecutively numbered names.

Source: https://tinylog.org/v2/configuration/#writers

koppor
  • 19,079
  • 15
  • 119
  • 161