> From: Ariel [mailto:[email protected]]
> 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: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to