简体   繁体   English

记录器 java 没有通过 System.out

[英]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我有一个使用 java api 的记录器,在单元测试中,我将标准的输出设置为我的输出,并检查是否正在打印消息(日志、异常等...)我的代码是

    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.但是日志没有在输出中被捕获,如果我检查控制台(eclipse,或通过 maven 运行),日志在那里意味着没有通过我的输出。 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.如果替换 system.out.print 的日志,那么它就可以工作,我也可以看到异常等等,只有使用 java API 记录的内容不会在 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.包含更多代码和 logging.properties 文件,因为输出取决于您的记录器和处理程序的创建时间。

If you are relying on the ConsoleHandler to write to your stream then you have to delay the creation of your logger.如果您依赖 ConsoleHandler 写入流,则必须延迟记录器的创建。 This is because the ConsoleHandler will capture a snapshot of System.err .这是因为ConsoleHandler 将捕获 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.您延迟了 ConsoleHandler 的创建,直到您重新映射 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.如果您依赖根记录器,则延迟构建您重新映射 System.err 的根记录器实用程序。 This is done by calling Logger.getLogger("") after remapping of System.err.这是通过在重新映射 System.err 之后调用Logger.getLogger("")完成的。
  3. Don't let your loggers get garbage collected unexpectedly.不要让您的记录器意外收集垃圾

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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