How to redirect System.out to a log file using Logback?

How can I use Logback to capture System.out messages in a Java program?

For example, I would like to use this code:

System.out.println("test: console out to file instead");

... and output it to a file.

Can this be done using the logback.xml configuration file?

+4
source share
2 answers

There's a little jar that does this for you: http://projects.lidalia.org.uk/sysout-over-slf4j/index.html

, FAQ: http://projects.lidalia.org.uk/sysout-over-slf4j/faq.html - logback stdout, StackOverFlowError s: D

, System.out System.err PrintWriter (? ). PrintWriter - , , .

+4

, , ..

<appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
  <target>System.out</target>
  <filter class="com.foo.StdOutFilter" />
  ...
</appender>
<logger name="mylogger" level="debug">
   <appender-ref ref="stdout" />
</logger>
-1

Source: https://habr.com/ru/post/1652625/


All Articles