简体   繁体   English

记录最佳实践:结构/条件记录/过滤

[英]Logging best practices: structure / conditional logging / filtering

I have been working on a project that logs using what are essentially just println statements with a prefixed string tag.我一直在研究一个项目,该项目使用基本上只是带有前缀字符串标记的 println 语句进行记录。 I have been looking into implementing support for an actual logging library such as Logback the past few days and had some questions relating to best-practices about logging in general.在过去的几天里,我一直在研究实现对 Logback 等实际日志库的支持,并且对一般日志记录的最佳实践有一些疑问。 I know a lot of what I'm doing is probably stupid, but I want to change:)我知道我正在做的很多事情可能很愚蠢,但我想改变:)

When I'm extending the code and adding new features, such as testing a new codec, I have been using liberal logging to ensure the code behaves as expected (instead of actual unit tests), and then using constant booleans at the top to disable that logging when the codec is finished (in case it's needed again or a bug is found, I can flip the boolean while testing).当我扩展代码并添加新功能(例如测试新编解码器)时,我一直在使用自由日志记录来确保代码按预期运行(而不是实际的单元测试),然后在顶部使用常量布尔值来禁用在编解码器完成时记录(如果再次需要它或发现错误,我可以在测试时翻转 boolean)。 I don't know if the granularity that debug level provides would be enough and would prefer some way to define levels differently for different features.我不知道debug级别提供的粒度是否足够,并且更喜欢某种方式来为不同的功能定义不同的级别。 Leaving these enabled by default would really bloat the console and probably effect performance -- is this what filters are usually used for?默认情况下启用这些确实会使控制台膨胀并可能影响性能——这就是过滤器通常的用途吗?

I've also found myself in more than one case prepending spaces to my messages so that I can better follow the flow of the code.我还发现自己在不止一种情况下会在我的消息前添加空格,以便我可以更好地遵循代码流。 I've found this to be really helpful.我发现这真的很有帮助。 In a way, the tabbed messages are like a debug-debug level.在某种程度上,选项卡式消息就像一个调试-调试级别。

Doing something
Reading a file
  header of file: ...
  body of file: ...
Back at main

What are good practices for logging?记录日志的好做法是什么? Can someone refer me a good resource that I can dig into or explain if what I'm doing is stupid and why it's stupid?有人可以向我推荐一个很好的资源,我可以深入研究或解释我正在做的事情是否愚蠢以及为什么它是愚蠢的? What are some alternatives?有哪些替代方案? An open source project as an example would be extremely helpful.以开源项目为例将非常有帮助。 Thanks, I appreciate any guidance.谢谢,我很感激任何指导。

Some advice:一些忠告:

  • Never substitute unit tests for logging only.永远不要只用单元测试代替日志记录。
  • Regarding logging you should log whatever makes you find bugs quicker.关于日志记录,您应该记录任何能让您更快发现错误的内容。
  • Log libraries support async logging which will not affect the performance of your application ( log4j2 async logging ).日志库支持异步日志记录,这不会影响应用程序的性能( log4j2 异步日志记录)。 Logback supports async too. Logback 也支持异步。
  • Do not use booleans inside your code to decide to log or not to log.不要在代码中使用布尔值来决定是否记录。 Use the logging levels (TRACE, DEBUG, INFO, WARN, ERROR) and set them accordingly.使用日志级别(TRACE、DEBUG、INFO、WARN、ERROR)并相应地设置它们。 Usually in PROD environment you will use a WARN level and in DEV you can set it on debug.通常在 PROD 环境中,您将使用 WARN 级别,而在 DEV 中,您可以在调试时设置它。
  • Logging on different levels depending on the package is quite useful (you can use appenders to customize this and other stuff).根据 package 在不同级别上登录非常有用(您可以使用附加程序来自定义此内容和其他内容)。
  • In resume, the important is to read the documentation of whatever library you are using在简历中,重要的是阅读您正在使用的任何库的文档

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

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