How to read console ouput

后端 未结 2 576
情深已故
情深已故 2021-01-29 02:14

Using the Grails framework how would one go about reading console output. What I mean is I have a java library in my grails app that outputs(system.out.println(\"x\")) to the co

相关标签:
2条回答
  • 2021-01-29 02:33

    You can redirect the output with the System.setOut() method. Here is a quick example that redirects System.out to a text pane after five seconds:

    import javax.swing.*;
    import java.awt.*;
    import java.io.IOException;
    import java.io.OutputStream;
    import java.io.PrintStream;
    import java.util.Date;
    
    public class TextCapture {
    
        public static void main(String[] args) throws Exception {
            JFrame jf = new JFrame();
            jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            final JTextArea jta = new JTextArea();
            jta.setPreferredSize(new Dimension(400, 200));
            jf.getContentPane().add(jta);
            jf.pack();
            jf.setVisible(true);
    
            OutputStream textOS = new OutputStream() {
                @Override
                public void write(int b) throws IOException {
                    jta.setText(jta.getText() + ((char)b));
                }
            };
    
            for(int i = 0; i < 20; i++) {
                Thread.sleep(1000);
                if(i == 5) {
                    System.setOut(new PrintStream(textOS));
                }
                System.out.println(String.valueOf(new Date()));
            }
        }
    
    }
    
    0 讨论(0)
  • 2021-01-29 02:44

    We can use log4j configuration, write your app logs in a file, keep reading same file with your app. This should suffice your requirement. Read more on http://grails.org/doc/latest/guide/conf.html section 5.1.2 Logging

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