在内存中“列表附加器”; 对于 log4j
log4j 是否有一个仅存储日志记录事件列表的附加程序(用于单元测试,以验证没有写入错误日志)?
Is there an appender for log4j that only stores a list of the logging events (to be used in unit tests, to verify no error logs were written) ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
有一个 MemoryAppender,但它不是标准的一部分log4j 库。
您可以轻松编写自己的代码,但如果您仅将它们用于单元测试,我可能会模拟记录器并断言不会对其进行任何调用。 重写目标类中的 getLogger() 方法或直接在类型上设置模拟 Logger。
使用 Jmock(记忆中的示例,如有错误,敬请谅解):
There is a MemoryAppender, but it's not part of the standard log4j library.
You could easily write your own, But if you are only using them for unit tests I would probably mock the Logger and assert no calls are made to it. Override the getLogger() method in the target class or set the mock Logger directly on the type.
Using Jmock (example from memory, sorry for any errors):
我不相信有。 不过,您可以轻松地自己编写。 这是一个合适的教程。
I don't believe there is. You can write your own easily, though. Here's a suitable tutorial.
这是记录器附加程序的示例实现,它将所有事件存储在内存中(在
StringBuilder
中); 然后可以使用 getResult 检索内容:例如,您必须将此附加器添加到现有的 Logger(
MyClass
是使用Logger
来记录您想要捕获的事件):This is an example implementation of a logger appender, that stores all the events in memory (in a
StringBuilder
); the contents can then be retrieved withgetResult
:You then have to add this appender to an existing
Logger
, for example (MyClass
is the class that uses aLogger
to log events you want to capture):