如何确定使用什么日志级别?

发布于 2024-07-07 17:23:07 字数 171 浏览 7 评论 0原文

日志级别 WARN、ERROR 和 FATAL 非常清楚。 但是什么时候是 DEBUG,什么时候是 INFO?

我见过一些项目在 INFO 级别上冗长得令人恼火,但我也见过过于偏向于 DEBUG 级别的代码。 在这两种情况下,有用的信息都隐藏在噪音中。

确定日志级别的标准是什么?

The log levels WARN, ERROR and FATAL are pretty clear. But when is something DEBUG, and when INFO?

I've seen some projects that are annoyingly verbose on the INFO level, but I've also seen code that favors the DEBUG level too much. In both cases, useful information is hidden in the noise.

What are the criteria for determining log levels?

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

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

发布评论

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

评论(6

夜深人未静 2024-07-14 17:23:07

我认为没有什么硬性规定; 使用 log4j 类型的级别,我的“经验法则”类似于:

  • 致命:应用程序(或至少是一个线程)即将严重死亡。 这是解释为什么发生这种情况的信息所在。
  • 错误:应用程序执行了不应该执行的操作。 这不是用户错误(“无效的搜索查询”); 这是断言失败、网络问题等,可能会中止当前操作
  • WARN:涉及但不会导致操作中止的问题; 数据库池中的连接数变低、操作中异常但预期的超时等。我经常认为“警告”总体上很有用; 例如 grep、group 和 count 来了解影响系统运行状况的因素
  • INFO:正常日志记录,是应用程序正常操作的一部分; 诊断内容,这样您就可以回过头来问“这种广泛的操作多久发生一次?”,或者“用户的数据是如何进入这种状态的?”
  • DEBUG:默认关闭,可以打开以调试特定的意外问题。 您可以在此处记录有关关键方法参数的详细信息或对于查找代码的特定“有问题”区域中可能存在的问题有用的其他信息。
  • TRACE:“说真的,这到底是怎么回事?!?!我需要记录我执行的每一条语句,以便在我发疯之前找到这个@#$@ing内存损坏错误”

不是一成不变的,但大致了解我的想法。

I don't think there are any hard-and-fast rules; using the log4j-type levels, my 'rules of thumb' are something like:

  • FATAL: the app (or at the very least a thread) is about to die horribly. This is where the info explaining why that's happening goes.
  • ERROR: something that the app's doing that it shouldn't. This isn't a user error ('invalid search query'); it's an assertion failure, network problem, etc etc., probably one that is going to abort the current operation
  • WARN: something that's concerning but not causing the operation to abort; # of connections in the DB pool getting low, an unusual-but-expected timeout in an operation, etc. I often think of 'WARN' as something that's useful in aggregate; e.g. grep, group, and count them to get a picture of what's affecting the system health
  • INFO: Normal logging that's part of the normal operation of the app; diagnostic stuff so you can go back and say 'how often did this broad-level operation happen?', or 'how did the user's data get into this state?'
  • DEBUG: Off by default, able to be turned on for debugging specific unexpected problems. This is where you might log detailed information about key method parameters or other information that is useful for finding likely problems in specific 'problematic' areas of the code.
  • TRACE: "Seriously, WTF is going on here?!?! I need to log every single statement I execute to find this @#$@ing memory corruption bug before I go insane"

Not set in stone, but a rough idea of how I think of it.

安穩 2024-07-14 17:23:07

非正式地,我使用这种层次结构,

  • DEBUG - 实际跟踪值
  • INFO - 刚刚发生了一些事情 - 没什么重要的,只是一个标志
  • WARN - 一切正常,但有些事情与预期不完全一样
  • ERROR - 发生了需要修复的事情,但我们可以继续进行其他(独立)活动
  • 致命 - 一个足够严重的问题,我们甚至不应该继续进行

我通常会在记录信息的情况下发布,但前提是我知道日志文件实际上已被审查(并且大小不是问题),否则是警告。

Informally I use this sort of hierarchy,

  • DEBUG - actual trace values
  • INFO - Something just happened - nothing important, just a flag
  • WARN - everything's working, but something isn't quite what was expected
  • ERROR - something has happened that will need to be fixed, but we can carry on and do other (independent) activities
  • FATAL - a serious enough problem that we shouldn't even carry on

I'll generally release with INFO being logged, but only if I know that log files are actually reviewed (and size isn't an issue), otherwise it's WARN.

悲喜皆因你 2024-07-14 17:23:07

考虑谁需要使用每个级别。
在我的代码中,我为开发人员输出保留DEBUG,例如仅对开发人员有帮助的输出。
VERBOSE 用于普通用户需要大量信息时。
INFO 我通常用来显示重大事件(例如发送网页、检查重要的内容)。

FAILWARN 是非常不言自明的。

Think about who needs to use each level.
In my code I keep DEBUG reserved for a developer output, e.g. output that would only help a developer.
VERBOSE is used for a normal user when alot of info is needed.
INFO I use to normally show major events (e.g. sending a webpage, checking something important).

And FAIL and WARN are pretty self explanatory.

甜`诱少女 2024-07-14 17:23:07

我团队的惯例是,如果消息中计算出某些内容,则使用 debug,而 info 用于纯文本。 因此,实际上 info 将向您显示正在发生的事情,而 debug 将显示正在发生的事情的值。

The convention in my team is to use debug if something is calculated in the message, whereas info is used for plain text. So in effect info will show you what's happening and debug will show the values of the things that are happening.

捂风挽笑 2024-07-14 17:23:07

我倾向于将 INFO 定位于用户,向他们提供甚至不是警告的消息。 DEBUG 往往供开发人员使用,我在其中输出消息以帮助跟踪代码流(也包含变量值)。

我还喜欢另一个级别的 DEBUG(DEBUG2?),它提供了调试信息的绝对存储桶负载,例如所有缓冲区的十六进制转储等。

I tend to target INFO towards the user to give them messages that aren't even warnings. DEBUG tends to be for developer use where I output messages to help trace the flow through the code (with values of variables as well).

I also like another level of DEBUG (DEBUG2?) which gives absolute bucketloads of debug information such as hex dumps of all buffers and so on.

眼睛会笑 2024-07-14 17:23:07

不需要 DEBUG2 级别。 这就是“TRACE”的用途。 TRACE 旨在成为绝对最低级别的日志记录,输出您可能希望看到的每条可能的信息。

为了避免信息泛滥,通常不建议您在整个项目中启用跟踪级别日志记录。 相反,请使用“DEBUG”来查找有关错误及其发生位置(因此得名)的一般信息,然后如果您仍然无法弄清楚,则仅针对该组件启用 TRACE。

There's no need for a DEBUG2 level. That's what 'TRACE' is for. TRACE is intended to be the absolute lowest level of logging outputting every possible piece of information you might want to see.

To avoid a deluge of information, it is generally not recommended that you enable trace-level logging across an entire project. Instead use 'DEBUG' to find out general information about the bug and where it occurs (hence the name), and then enable TRACE only for that component if you still can't figure it out.

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