如何在 java.util.logging 中使用自定义日志处理程序?
这就是我想做的:
Logger logger = LogManager.getLogManager().getLogger(Logger.GLOBAL_LOGGER_NAME);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
Handler handler = new StreamHandler(stream, new SimpleFormatter());
logger.addHandler(handler);
org.slf4j.LoggerFactory.getLogger("com.example").info("foo");
logger.removeHandler(handler);
assert stream.toString().contains("foo");
它是 SLF4J 和 JUL 的混合体。脚本末尾的 stream
为空。为什么?
This is what I'm trying to do:
Logger logger = LogManager.getLogManager().getLogger(Logger.GLOBAL_LOGGER_NAME);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
Handler handler = new StreamHandler(stream, new SimpleFormatter());
logger.addHandler(handler);
org.slf4j.LoggerFactory.getLogger("com.example").info("foo");
logger.removeHandler(handler);
assert stream.toString().contains("foo");
It's a mix of SLF4J and JUL. stream
is empty at the end of the script. Why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果记录被格式化,StreamHandler 仅写入格式化程序“head”。如果处理程序关闭,则有条件地写入“head”和“tail”。所有这些似乎都表明 StreamHandler 没有消耗 LogRecord。 SimpleFormatter 对头部和尾部使用空字符串,因此这对故障排除没有帮助。
The StreamHandler only writes the formatter 'head' if a record is formatted. Conditionally the 'head' and 'tail' are written if the handler is closed. All of that seems to indicate that no LogRecord was consumed by the StreamHandler. The SimpleFormatter uses empty string for both the head and tail so that doesn't help in troubleshooting.