我在我的应用程序中使用log4j来记录具有不同日志级别的某些包.
但是,stderr和stdout似乎没有在此日志文件中捕获,而是在应用程序启动时将它们打印在文件中
java AppName >> out.log
是否有可能在log4j中捕获stdout和stderr(例如stdout as info和stderr as 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错误的Stacktrace:
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:
解决方法
要将stdout和stderr重定向到log4j,请尝试以下方法:
System.setErr( new PrintStream( new LoggingOutputStream( Logger.getRootLogger( ),Level.ERROR ),true); System.setOut( new PrintStream( new LoggingOutputStream( Logger.getRootLogger( ),Level.INFO ),true);
对于LoggingOutputStream,您可以从this example开始
要让log4j将您想要的内容打印到控制台(如果需要,将其捕获回文件/节点),启用ConsoleAppender到适当的级别