我找不到允许以比通常看到的更好的方式格式化日志输出语句的库。我记得的一个功能是它可以根据日志语句发生位置的“嵌套”来“偏移”日志消息。
也就是说,不是这样的:
DEBUG | DefaultBeanDefinitionDocumentReader.java| 86 | Loading bean definitions
DEBUG | AbstractAutowireCapableBeanFactory.java| 411 | Finished creating instance of bean 'MS-SQL'
DEBUG | DefaultSingletonBeanRegistry.java| 213 | Creating shared instance of singleton bean 'MySQL'
DEBUG | AutowireCapableBeanFactory.java| 383 | Creating instance of bean 'MySQL'
DEBUG | AutowireCapableBeanFactory.java| 459 | Eagerly caching bean 'MySQL' to allow for resolving potential circular references
DEBUG | AutowireCapableBeanFactory.java| 789 | Another debug message
它会显示这样的东西:
DEBUG | DefaultBeanDefinitionDocumentReader.java| 86 | Loading bean definitions
DEBUG | AbstractAutowireCapableBeanFactory.java | 411 | Finished creating instance of bean 'MS-SQL'
DEBUG | DefaultSingletonBeanRegistry.java | 213 | Creating shared instance of singleton bean 'MySQL'
DEBUG | AutowireCapableBeanFactory.java | 383 | Creating instance of bean 'MySQL'
DEBUG | AutowireCapableBeanFactory.java | 459 | |__ Eagerly caching bean 'MySQL' to allow for resolving potential circular references
DEBUG | AutowireCapableBeanFactory.java | 789 | |__ Another debug message
这是我刚刚编造的示例 (VeryLongCamelCaseClassNamesNotMine)。但我记得看到过格式清晰的日志输出,它们真的比我以前见过的任何东西都好得多,而且除了更好之外,它们也更容易阅读,因为它们再现了代码的一些逻辑组织。
但我再也找不到那个图书馆是什么了。
我很确定它与 log4j 或 sl4j 完全兼容。
最佳答案
我在您的代码示例中看到两件事:
- 根据最长的名称填充类名
- 根据“嵌套”修改消息。
对于 1. 我几乎看不出这是如何做到的,因为你永远不知道将来 登录哪个类。当然,您可以根据 曾经 记录的最长类名添加填充,但是一旦记录了更长的类名,文件看起来就不会像您的样本那么好.
对于 2。可以实现一个过滤器(请参阅 here 以了解有关过滤器的 logback 文档),该过滤器将研究调用者数据并添加某种“嵌套在”前缀,就像您在示例中所写的那样。我想这不会是一项太难的任务。
希望这会有所帮助...虽然我没有为您提供指向您正在寻找的库的链接...:-(
关于Java:可以很好地格式化日志输出的库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2393675/