logging-log4j-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Michael Erskine <michael.ersk...@ketech.com>
Subject RE: I need to make logging in a complete application
Date Wed, 07 Jan 2009 10:11:22 GMT
> From: Ariel [mailto:isaacrc82@gmail.com]
> Subject: I need to make logging in a complete application
> I am newbie with log4j, I have made a distributed and multithreaded
> application where all the logging is made displaying de mesagges in the
> console using System.out.println. Now I want to redirect those messages
> to a
> file to have a trace of all the messages. Is there anyway to telling
> log4j
> that all the System.out messages write them into a file ???

Sure, redirect standard output to PrintStream subclass that does the logging for you: this
should get you started...

        // Redirect output
        PrintStream ps = new PrintStream(new StdoutStderrStream(
            new ByteArrayOutputStream()));
        savedStdout = System.out;
        savedStderr = System.err;
        System.setOut(ps);
        System.setErr(ps);



    private class StdoutStderrStream extends FilterOutputStream {
        public StdoutStderrStream(OutputStream s) {
            super(s);
        }
        public void write(byte b[]) throws IOException {
            // log here in threadsafe manner
        }
        public void write(byte b[], int off, int len) throws IOException {
            // log here in threadsafe manner
        }
    }

Enjoy!

Michael Erskine.


---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-user-help@logging.apache.org


Mime
View raw message