How do I change java logging console output from std err to std out?

后端 未结 13 1579
旧巷少年郎
旧巷少年郎 2020-11-27 06:50

I\'m using the standard ConsoleHandler from java.util.logging and by default the console output is directed to the error stream (i.e. System.

13条回答
  •  遥遥无期
    2020-11-27 07:21

    When we create a new ConsoleHandler object, default output stream is "system.err". Sadly Java doesn't provide any public method for ConsoleHandler class to set output stream. So it can be set only at the time of object creation. As ConsoleHandler class extends StreamHandler, which has a protected method "setOutputStream" to set output stream explicitly. To set output Stream for ConsoleHandler just override this method at the time of new call for object creation.

    ConsoleHandler consoleHandler = new ConsoleHandler (){
                @Override
                protected synchronized void setOutputStream(OutputStream out) throws SecurityException {
                    super.setOutputStream(System.out);
                }
            };
    

提交回复
热议问题