[英]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:一些忠告:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.