This needs a filter on linefeeds, but how about something like:

import java.io.*;
import org.apache.log4j.Logger;

public class Log4jOutputStream extends ByteArrayOutputStream {

  Logger logger;
        
  public Log4jOutputStream(Logger myLogger) {
    logger = myLogger;
  }
        
  public void flush() {
        
    // check size to avoid printing empty strings on close
    if (size() != 0) {
      String temp = toString();
      logger.info(temp);
      reset();
    }
  }
}

To use:

  Logger logger = Logger.getLogger("myTest");
  System.setOut(new PrintStream(new Log4jOutputStream(logger), true));

Herm Greider
LexisNexis

-----Original Message-----
From: Max Rydahl Andersen [mailto:[EMAIL PROTECTED]]
Sent: Thursday, July 25, 2002 3:13 AM
To: Log4J Users List; Jacob Kjome
Subject: Re: The definitive guide for directing System.out/err ?


That is just a way to get printStackTrace to print to a stringwriter...

What if some external library (e.g. the JDK it self) writes to
System.out/System.err ? I cannot
recompile the jdk and replace all system.out/err/printstacktraces with that
source snippet :)

Thanx anyway :)

/max

----- Original Message -----
From: "Jacob Kjome" <[EMAIL PROTECTED]>
To: "Log4J Users List" <[EMAIL PROTECTED]>
Sent: Wednesday, July 24, 2002 5:16 PM
Subject: Re: The definitive guide for directing System.out/err ?


> Hello Max,
>
> I don't know about a definitive guide, but this is one possible way of
> doing things:
>
> StringWriter sw = new StringWriter();
> ex.printStackTrace(new PrintWriter(sw));
> logger.error("" + sw);
>
>
> Jake
>
> Wednesday, July 24, 2002, 7:30:39 AM, you wrote:
>
> MRA> Hi,
>
> MRA> Looking in the archives i found a handfull of threads discussing the
> MRA> possibility of letting println's (etc.) going to System.out/err being
sent
> MRA> to a log4j catagory instead.
>
> MRA> Anyone that have gathered a complete an definitive guide for how to
do this
> MRA> safely and without running into infinte recursion problems when
something in
> MRA> log4j fails and then tries to write to System.err/out....
>
> MRA> /max
>
>
> MRA> --
> MRA> To unsubscribe, e-mail:
<mailto:[EMAIL PROTECTED]>
> MRA> For additional commands, e-mail:
<mailto:[EMAIL PROTECTED]>
>
>
>
> --
> Best regards,
>  Jacob                            mailto:[EMAIL PROTECTED]
>
>
> --
> To unsubscribe, e-mail:
<mailto:[EMAIL PROTECTED]>
> For additional commands, e-mail:
<mailto:[EMAIL PROTECTED]>
>
>


--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to