Java:可以很好地格式化日志输出的库

发布于 2024-08-24 02:35:43 字数 1557 浏览 8 评论 0原文

我找不到一个允许以比通常看到的更好的方式格式化日志输出语句的库。我记得的功能之一是它可以根据日志语句发生位置的“嵌套性”来“偏移”日志消息。

也就是说,而不是这样:

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)。但我记得看到过格式如此清晰的日志输出,它们确实比我以前见过的任何东西都要好得多,而且除了简单更好之外,它们还更容易阅读,因为它们再现了代码的一些逻辑组织。

但我再也找不到那个图书馆是什么了。

我很确定它与 log4jsl4j 完全兼容。

I cannot find back a library that allowed to format log output statements in a much nicer way than what is usually seen. One of the feature I remember is that it could 'offset' the log message depending on the 'nestedness' of where the log statement was occuring.

That is, instead of this:

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

It would shows something like this:

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

This is an example I just made up (VeryLongCamelCaseClassNamesNotMine). But I remember seeing such cleanly formatted log output and they were really much nicer than anything I had seen before and, in addition to being just plain nicer, they were also easier to read for they reproduced some of the logical organization of the code.

Yet I cannot find anymore what that library was.

I'm pretty sure it was fully compatible with log4j or sl4j.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

请帮我爱他 2024-08-31 02:35:43

我在您的代码示例中看到两件事:

  1. 根据最长名称填充类名
  2. 根据“嵌套”修改消息。

对于 1. 我几乎不知道这是如何做到的,因为你永远不知道将来会登录哪个类。当然,您可以根据记录的最长类名添加填充,但是一旦记录了更长的类名,该文件将不会像您的示例那样漂亮。

对于 2. 可以实现一个过滤器(请参阅此处了解有关过滤器的 logback 文档)将研究调用者数据并添加某种“嵌套在”前缀,就像您在示例中编写的那样。我想这并不是一项太难的任务。

希望这会有所帮助...尽管我没有向您提供您正在寻找的库的链接...:-(

I see two things in your code samples:

  1. Padding of the classnames according to the longest name
  2. Messages modified depending on the "nesting".

For 1. I hardly see how this can be done, since you never know which class will log in the future. Sure, you can add padding according to the longest class name that has ever been logged, but the file will not look as nice as your sample once a longer class name is logged.

For 2. One could implement a filter (see here for logback documentation about filter) that would study the caller data and add some kind of "is nested in" prefix like you wrote in your example. This would not be too hard a task, I guess.

Hope this helps... although I do not provide you with the link to the lib you are looking for... :-(

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文