何时使用不同的日志级别 [英] When to use the different log levels

查看:74
本文介绍了何时使用不同的日志级别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

按照死亡顺序记录消息的方式有多种:

There are different ways to log messages, in order of fatality:

  1. FATAL

ERROR

WARN

INFO

DEBUG

TRACE

我该如何决定何时使用哪个?

How do I decide when to use which?

有什么好的启发式方法?

What's a good heuristic to use?

推荐答案

我通常订阅以下约定:

  • 跟踪-仅当我要跟踪"代码并尝试专门查找功能的一个部分时.
  • 调试-具有诊断意义的信息,不仅对开发人员(IT,系统管理员等)更有益于人们.
  • 信息-用于记录的一般有用信息(服务启动/停止,配置假设等).我想要一直可用的信息,但通常在正常情况下并不关心.这是我开箱即用的配置级别.
  • 警告-可能导致应用程序异常的任何事情,但我会自动对其进行恢复. (例如从主服务器切换到备份服务器,重试操作,丢失辅助数据等)
  • 错误-任何对操作致命的错误,但对服务或应用程序不是致命的(无法打开所需的文件,丢失数据等).这些错误将迫使用户(管理员或直接用户)进行干预.这些通常是在我的应用程序中保留的,用于不正确的连接字符串,缺少服务等.
  • 致命-强制关闭服务或应用程序以防止数据丢失(或进一步的数据丢失)的任何错误.我仅将这些保留给最令人发指的错误,以及那些肯定会导致数据损坏或丢失的情况.
  • Trace - Only when I would be "tracing" the code and trying to find one part of a function specifically.
  • Debug - Information that is diagnostically helpful to people more than just developers (IT, sysadmins, etc.).
  • Info - Generally useful information to log (service start/stop, configuration assumptions, etc). Info I want to always have available but usually don't care about under normal circumstances. This is my out-of-the-box config level.
  • Warn - Anything that can potentially cause application oddities, but for which I am automatically recovering. (Such as switching from a primary to backup server, retrying an operation, missing secondary data, etc.)
  • Error - Any error which is fatal to the operation, but not the service or application (can't open a required file, missing data, etc.). These errors will force user (administrator, or direct user) intervention. These are usually reserved (in my apps) for incorrect connection strings, missing services, etc.
  • Fatal - Any error that is forcing a shutdown of the service or application to prevent data loss (or further data loss). I reserve these only for the most heinous errors and situations where there is guaranteed to have been data corruption or loss.

这篇关于何时使用不同的日志级别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆