Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9276599
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T16:53:38+00:00 2026-06-18T16:53:38+00:00

I have some legacy code (or rather some code we don’t control but we

  • 0

I have some legacy code (or rather some code we don’t control but we have to use) that writes a lot of statements to system.out/err.

At the same time, we are using a framework that uses a custom logging system wrapped around log4j (again, unfortunately we don’t control this).

So I’m trying to redirect the out and err stream to a custom PrintStream that will use the logging system. I was reading about the System.setLog() and System.setErr() methods but the problem is that I would need to write my own PrintStream class that wraps around the logging system in use. That would be a huge headache.

Is there a simple way to achieve this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T16:53:39+00:00Added an answer on June 18, 2026 at 4:53 pm

    Just to add to Rick’s and Mikhail’s solutions, which are really the only option in this scenario, I wanted to give an example of how creating a custom OutputStream can potentially lead to not so easy to detect/fix problems. Here’s some code:

    import java.io.IOException;
    import java.io.OutputStream;
    import java.io.PrintStream;
    import org.apache.log4j.Logger;
    
    public class RecursiveLogging {
      /**
       * log4j.properties file:
       * 
       * log4j.rootLogger=DEBUG, A1
       * log4j.appender.A1=org.apache.log4j.ConsoleAppender
       * log4j.appender.A1.layout=org.apache.log4j.PatternLayout
       * log4j.appender.A1.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
       * 
       */
      public static void main(String[] args) {
        // Logger.getLogger(RecursiveLogging.class).info("This initializes log4j!");
        System.setOut(new PrintStream(new CustomOutputStream()));
        System.out.println("This message causes a stack overflow exception!");
      }
    }
    
    class CustomOutputStream extends OutputStream {
      @Override
      public final void write(int b) throws IOException {
        // the correct way of doing this would be using a buffer
        // to store characters until a newline is encountered,
        // this implementation is for illustration only
        Logger.getLogger(CustomOutputStream.class).info((char) b);
      }
    }
    

    This example shows the pitfalls of using a custom output stream. For simplicity the write() function uses a log4j logger, but this can be replaced with any custom logging facility (such as the one in my scenario). The main function creates a PrintStream that wraps a CustomOutputStream and set the output stream to point to it. Then it executes a System.out.println() statement. This statement is redirected to the CustomOutputStream which redirects it to a logger. Unfortunately, since the logger is lazy initialized, it will acquire a copy of the console output stream (as per the log4j configuration file which defines a ConsoleAppender) too late, i.e., the output stream will point to the CustomOutputStream we just created causing a redirection loop and thus a StackOverflowError at runtime.

    Now, with log4j this is easy to fix: we just need to initialize the log4j framework before we call System.setOut(), e.g., by uncommenting the first line of the main function. Luckily for me, the custom logging facility I have to deal with is just a wrapper around log4j and I know it will get initialized before it’s too late. However, in the case of a totally custom logging facility that uses System.out/err under the cover, unless the source code is accessible, it’s impossible to tell if and where direct calls to System.out/err are performed instead of calls to a PrintStream reference acquired during initialization. The only work around I can think of for this particular case would be to retrieve the function call stack and detect redirection loops, since the write() functions should not be recursive.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Very strange question but I have some legacy code that interacts with another application
I have some legacy code that I am now trying to re-use under Spring.
I have some legacy code that was used to monitor my applications cpu,memory etc
I have some legacy C++ code that I am trying to understand a bit
We have some legacy ASP.NET code that detects if a request is secure, and
I need to integrate some legacy 32-bit code - for which I don't have
I have some legacy code that uses Interlocked.Equals to compare values. The values may
There is some old legacy vbscript code we have that needs some sort of
We have some C# code that uses COM (its calling legacy code). When an
We have some legacy code that compiles in Delphi 6. There are plans to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.