简体   繁体   中英

logger java is not going through System.out

I have a logger using java api, and in the unit test I set the output from the standard to my output and check if the messages are being printed(log, exceptions etc...) my code is

    ByteArrayOutputStream mockedOutput = new ByteArrayOutputStream();
    System.setOut(new PrintStream(mockedOutput));
    System.setErr(new PrintStream(mockedOutput));

but the log is not being captured in the output, if I check the console(eclipse, or running through maven) the log is there meaning the is not going through my output. If replace the log for system.out.print then it works, I also can see exceptions and so on, only things that are being logged using java API is not being captured in the mockedOutput.

Does anyone have an idea?, I am running out of clue.

Include more of your code and logging.properties file because the output depends on when your loggers and therefore handlers are created.

If you are relying on the ConsoleHandler to write to your stream then you have to delay the creation of your logger. This is because the ConsoleHandler will capture a snapshot of System.err .

Without seeing your code you need to ensure that:

  1. You have delayed the creation of your ConsoleHandler until you have remapped System.err.
  2. If you are depending on the root logger then delay the construction of the root logger util you have remapped System.err. This is done by calling Logger.getLogger("") after remapping of System.err.
  3. Don't let your loggers get garbage collected unexpectedly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM