gpt4 book ai didi

java - 在 unix 重定向之前在 log4j 中捕获 stderr 和 stdout

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:06:25 25 4
gpt4 key购买 nike

我在我的应用程序中使用 log4j 来记录具有不同日志级别的某些包。

但是,stderr 和 stdout 似乎并未在此日志文件中捕获,而是在应用程序以

启动时将它们打印在文件中
java AppName >> out.log

是否可以在 log4j 中捕获 stdout 和 stderr(例如,stdout 作为信息,stderr 作为 WARN/ERROR)以及 out.log 文件?

log4j的设置如下:

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="FileAppender" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="${MDCapture.logfile.dir}/${APP_NAME}.log"/>
<param name="BufferedIO" value="false"/>
<param name="DatePattern" value="'.'yyyy-MMM-dd"/>
<layout class="org.apache.log4j.TTCCLayout">
<param name="DateFormat" value="ISO8601"/>
</layout>
</appender>

<appender name="AsyncAppenders" class="org.apache.log4j.AsyncAppender">
<appender-ref ref="FileAppender"/>
</appender>

<!-- LOGGER PACKAGES -->

<root>
<priority value="info"/>
<appender-ref ref="AsyncAppenders"/>
</root>
</log4j:configuration>

更新:

stackoverflow 错误的堆栈跟踪:

WARN root buffer size limit:65log4j:WARN root buffer size limit:65Exception in thread "Thread-12"
2013-03-22 09:14:24,451 [Thread-12] ERROR root - java.lang.StackOverflowError
2013-03-22 09:14:24,452 [Thread-12] ERROR root - at java.io.BufferedWriter.write(BufferedWriter.java:202)
2013-03-22 09:14:24,452 [Thread-12] ERROR root - at java.io.Writer.write(Writer.java:140)
2013-03-22 09:14:24,452 [Thread-12] ERROR root - at java.io.PrintStream.write(PrintStream.java:475)
2013-03-22 09:14:24,452 [Thread-12] ERROR root - at java.io.PrintStream.print(PrintStream.java:619)
2013-03-22 09:14:24,452 [Thread-12] ERROR root - at java.io.PrintStream.println(PrintStream.java:756)
2013-03-22 09:14:24,452 [Thread-12] ERROR root - at org.apache.log4j.helpers.LogLog.warn(LogLog.java:171)
2013-03-22 09:14:24,452 [Thread-12] ERROR root - at org.apache.log4j.AsyncAppender.append(AsyncAppender.java:175)
2013-03-22 09:14:24,452 [Thread-12] ERROR root - at org.apache.log4j.AppenderSkeleton.doAppend(AppenderSkeleton.java:251)
2013-03-22 09:14:24,452 [Thread-12] ERROR root - at org.apache.log4j.helpers.AppenderAttachableImpl.appendLoopOnAppenders(AppenderAttachableImpl.java:66)


2013-03-22 09:14:24,452 [Thread-12] ERROR root - at org.apache.log4j.Category.callAppenders(Category.java:206)
2013-03-22 09:14:24,452 [Thread-12] ERROR root - at org.apache.log4j.Category.forcedLog(Category.java:391)
2013-03-22 09:14:24,452 [Thread-12] ERROR root - at org.apache.log4j.Category.log(Category.java:838)
2013-03-22 09:14:24,453 [Thread-12] ERROR root - at com.company.something.AClass.LoggingOutputStream.flush(LoggingOutputStream.java:198)
2013-03-22 09:14:24,453 [Thread-12] ERROR root - at java.io.PrintStream.write(PrintStream.java:432)
2013-03-22 09:14:24,453 [Thread-12] ERROR root - at sun.nio.cs.StreamEncoder.writeBytes(StreamEncoder.java:202)
2013-03-22 09:14:24,453 [Thread-12] ERROR root - at sun.nio.cs.StreamEncoder.implFlushBuffer(StreamEncoder.java:272)
2013-03-22 09:14:24,453 [Thread-12] ERROR root - at sun.nio.cs.StreamEncoder.flushBuffer(StreamEncoder.java:85)
2013-03-22 09:14:24,453 [Thread-12] ERROR root - at java.io.OutputStreamWriter.flushBuffer(OutputStreamWriter.java:168)
2013-03-22 09:14:24,453 [Thread-12] ERROR root - at java.io.PrintStream.write(PrintStream.java:477)
2013-03-22 09:14:24,453 [Thread-12] ERROR root - at java.io.PrintStream.print(PrintStream.java:619)
2013-03-22 09:14:24,453 [Thread-12] ERROR root - at java.io.PrintStream.println(PrintStream.java:756)
2013-03-22 09:14:24,453 [Thread-12] ERROR root - at org.apache.log4j.helpers.LogLog.warn(LogLog.java:171)
2013-03-22 09:14:24,453 [Thread-12] ERROR root - at org.apache.log4j.AsyncAppender.append(AsyncAppender.java:175)
2013-03-22 09:14:24,453 [Thread-12] ERROR root - at org.apache.log4j.AppenderSkeleton.doAppend(AppenderSkeleton.java:251)
2013-03-22 09:14:24,453 [Thread-12] ERROR root - at org.apache.log4j.helpers.AppenderAttachableImpl.appendLoopOnAppenders(AppenderAttachableImpl.java:

最佳答案

对于 log4j 2.x:

System.setErr(IoBuilder.forLogger(LogManager.getRootLogger()).setLevel(Level.ERROR).buildPrintStream());
System.setOut(IoBuilder.forLogger(LogManager.getRootLogger()).setLevel(Level.INFO).buildPrintStream());

依赖信息:

  • 组:org.apache.logging.log4j
  • 工件:log4j-iostreams

关于java - 在 unix 重定向之前在 log4j 中捕获 stderr 和 stdout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15530272/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com