Reputation: 687
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();
Upvotes: 5
Views: 1974
Reputation: 20521
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
Upvotes: 0
Reputation: 1
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();
Upvotes: 0
Reputation: 489
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();
Upvotes: 3
Reputation: 311393
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();
Upvotes: 3