Write System.out for a file with log4j2

Is it possible to write System.out (OutputStream) directly to the log file, for example in the "old" log4j?

I only find solutions for log4j, not log4j2

thanks for any help!

+3


source to share


2 answers


The upcoming 2.1 release has a new log4j-iostreams module that can do this and more. It should be soon.



If you are in a hurry, you can check the latest source from the wizard and create a 2.1 snapshot.

+1


source


It's pretty easy to use the module log4j2-iostreams

. Let's say we want to send all messages from System.out

to a log System.out

-level named logger 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

      

+1


source







All Articles