system.out.println redirection in java

前端 未结 3 584
时光取名叫无心
时光取名叫无心 2021-01-26 11:28

I want to catch the println and redirect to a file and print it

here is my code, I wish to print 123 /n 456

but that doesn\'t work and I think I need something t

相关标签:
3条回答
  • 2021-01-26 11:45

    Way back in the day, before I started using log4j or other dedicated loggers, I use to use a technique similar to the one below.

    Essentially it's a proxy PrintStream which echos content back to the original PrintStream as well as writing the content to some other OutputStream (such as to a file).

    You could also put in flag that would turn of the echoing to the console by simply not using old.write(b) when set to true. This a technique I've employed to stop applications the spew copious amounts of crap to the stdout which can slow the application down, especially when you're running in a graphical environment...

    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.OutputStream;
    import java.io.PrintStream;
    
    public class RedirectStdOut {
    
        public static void main(String[] args) {
    
            OutputStream os = null;
    
            try {
    
                os = new FileOutputStream(new File("Log.txt"));
                LoggingPrintStream lps = new LoggingPrintStream(os);
    
                System.out.println("This is going to the console only...");
                lps.install();
                System.out.println("This is going to console and file");
                System.out.println("Fun times");
                lps.uinstall();
                System.out.println("This is going to console only...");
    
            } catch (IOException exp) {
                exp.printStackTrace();
            } finally {
                try {
                    os.close();
                } catch (Exception e) {
                }
            }
    
        }
    
        public static class LoggingPrintStream {
    
            private OutputStream os;
            private PrintStream old;
    
            public LoggingPrintStream(OutputStream os) {
                this.os = os;
            }
    
            public void install() {
                old = System.out;
                System.setOut(new PrintStream(new OutputStream() {
                    @Override
                    public void write(int b) throws IOException {
                        old.write(b);
                        os.write(b);
                    }
                }));
            }
    
            public void uinstall() throws IOException {
                try {
                    os.flush();
                } finally {
                    try {
                        os.close();
                    } catch (Exception e) {
                    }
                    System.setOut(old);
                }
            }
    
        }
    
    }
    

    I was trying to figure out if I could chain the streams, but my heads not up to the task today (Friday afternoon)

    0 讨论(0)
  • 2021-01-26 11:52

    Redirect System.out.println to Log4J, while keeping class name information

    the above post explains how to do this to some extent.

    0 讨论(0)
  • 2021-01-26 12:00

    You should redirect the out stream to a file like this:

     System.setOut(new PrintStream(new File("<your log file>"));
    

    this will redirect all your system.out in the log file. hope this helps.

    0 讨论(0)
提交回复
热议问题