Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

catch stderr and stdout in log4j before unix redirection

I'm using log4j in my application to log certain packages with a different log level.

However, stderr and stdout do not seem to be captured in this logfile, and instead they are being printed in a file when the app starts with

java AppName >> out.log

is it possible to catch stdout and stderr in log4j (e.g stdout as info and stderr as WARN/ERROR ) as well as in the out.log file?

Settings for log4j are as follows:

<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>

UPDATE:

Stacktrace for stackoverflow error:

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:
like image 221
kkudi Avatar asked Mar 20 '13 17:03

kkudi


2 Answers

For log4j 2.x:

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

Dependency Information:

  • group: org.apache.logging.log4j
  • artifact: log4j-iostreams
like image 101
Raviteja Avatar answered Oct 19 '22 23:10

Raviteja


To redirecting stdout and stderr to log4j, try something like this:

System.setErr( new PrintStream( new LoggingOutputStream( Logger.getRootLogger(  ), Level.ERROR ), true);

System.setOut( new PrintStream( new LoggingOutputStream( Logger.getRootLogger(  ), Level.INFO ), true);

For LoggingOutputStream you can start from this example

To get log4j to print what you want to the console (and capture this back into a file/node if you want), enable a ConsoleAppender to the proper level

like image 4
Bruno Grieder Avatar answered Oct 19 '22 23:10

Bruno Grieder