Is it possible to write the System.out (OutputStream) directly to a log file like in the "old" log4j?
I only find solutions for log4j, not log4j2
thanks for any help!
It is quite easy using log4j2-iostreams
module. Let's say we want to send all messages from System.out
to a logger with name system.out
with log level INFO
:
System.setOut(
IoBuilder.forLogger(LogManager.getLogger("system.out"))
.setLevel(Level.INFO)
.buildPrintStream()
);
System.out.println("Lorem ipsum");
with the following log4j2.properties
appender.console.type = Console
appender.console.name = STDOUT
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = %d [%p] %c - %m%n
rootLogger.level = info
rootLogger.appenderRef.stdout.ref = STDOUT
we should see the following output in the console:
2017-10-28 12:38:22,623 [INFO] system.out - Lorem ipsum
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With