您遵循什么准则来编写良好的日志记录语句 [英] What guidelines do you adhere to for writing good logging statements

查看:100
本文介绍了您遵循什么准则来编写良好的日志记录语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近在我的项目代码库中找到了一条日志语句,内容为 在这里,我的搜索参数是==> =========== 11/30/2008 === 1 ==== 00:00 AM"

I recently found a log statement in my projects codebase that says "here i am with search parameter==>===========11/30/2008===1====00:00 AM"

在应用程序中编写良好的日志消息时,您遵循什么准则?

what guidelines do you adhere to for writing good log messages in an application?

推荐答案

该`log'语句看起来更像是trace语句.

That `log' statement looks more like a trace statement.

记录:显示正常事件和错误

Logging: show the normal events and errors

跟踪:显示执行路径以及所有日志记录事件

Tracing: show the execution path as well as the all of the logging events

我个人希望通过日志记录来查找程序已完成的工作,并且希望使用trace语句来验证执行路径并找出问题所在.我将跟踪语句路由到一个单独的文件,该文件包含两种类型的上下文日志消息.

Personally, I want logging to find out what work has been done by my program, and I want trace statements to verify the execution path and for figuring out what went wrong. I'll route tracing statements to a separate file which includes both types of log messages for context.

因此,您至少应具有2个日志级别,并且能够关闭跟踪以提高性能.您应该能够将这些事件流路由到其他位置.这样一来,您就可以轻松保留历史记录的日志,而不会因为只想跟踪问题的调试信息而使日志混乱.

So you should have 2 log levels at least, and be able to turn off tracing for performance. You should be able to route these event streams to different locations. This allows you to easily keep logs for historical records, while not having them cluttered with debug info that you only want for tracing problems.

这里的许多答案都集中在您将包含在日志消息中的字段上,但是我认为日志调用的位置和日志记录级别更为重要.如果您使用log4net,则可以通过配置文件随意添加/排除日期戳,但是如果不重新编译就不能放置或删除日志语句,因此考虑一下它们的去向是很有意义的.除了标准字段(例如时间戳和线程ID等)之外,您还想知道进行调用的类和方法的名称.如果您采用Log4net等人的最佳做法,即在关注的类型之后命名记录器,则已经使用了类名.除此之外,我通常还包括方法名称.这对于跟踪特别必要,但是我将其包括在所有日志消息中.

A lot of the answers here focus on the fields that you would include in a log message, but I would argue that the placement and logging level of the log calls are more important. If you use log4net you would be able to include/exclude the date stamp at will via config files, but you won't be able to place or remove log statements without recompiling, so it makes sense to think pretty hard about where they go. Beyond the standard fields, such as timestamp and thread ID, etc, you want to know the class and method name that the call was made from. Log4net et al takes care of the class name already if you go by their best practice of naming your logger after the type you are concerned with. Beyond this, I typically include the method name. This is particularly necessary for tracing, but I include it on all my log messages.

记录:

您想知道有关将要执行的操作的足够信息,以便在出现问题时返回并四处查看.好的候选者是消息ID,电子邮件地址,它们是唯一标识工作项的东西.此类消息应在此类数据可用时立即出现,以便在您浏览日志文件时,您会看到类似试图用y来做x"的信息,然后再出现异常时,我们知道需要查看哪个工作项目才能了解失败的原因.

You want to know enough information about what action is about to be performed to go back and poke around if something goes wrong. Good candidates are message IDs, email addresses, something that uniquely identifies the work item. This sort of message should come as soon as this sort of data is available, so that when you are reading down through a log file, you will see something like 'attempting to do x with y' and then later if we see an exception, we know which work item we need to look at to see why we failed.

记录异常应与尝试"日志消息配对,以便在读取日志时在上下文中有意义该异常消息.这意味着要考虑异常处理的结构.如果您使用的是.net,而只想记录一个异常,而不处理它,则想重新抛出 same 异常,而不是一个新的异常,因此只需'throw'而不是'throw e'其中"e"是您的异常实例.如果没有意义的话,请看一看.

Logging exceptions should be paired with an 'attempt' log message, so that the exception message makes sense in context when reading the log. This means thinking about the structure of exception handling. If you are using .net and you only want to log an exception, not handle it, you want to rethrow the same exception, not a new one, so just do 'throw' not 'throw e' where 'e' is your exception instance. Look this one up if it doesn't make sense.

跟踪:

这实际上更简单,通常我会在感兴趣的方法(例如书挡)的开头和结尾处收到一条消息.在条目中,您可以打印影响程序流程的关键方法参数.在方法末尾记录消息是可选的,通常您会感兴趣的是像堆栈跟踪那样查看跟踪.您可以找出没有它们的执行路径.

This is actually simpler, usually I'll have a message at the beginning and end of a method of interest, like bookends. In the entry you can print critical method arguments that affect the program flow. Logging a message at the end of a method is kind of optional, usually you will be interested in looking at the trace sort of like a stack trace. You can figure out the execution path without them.

性能:

对于字符串性能,如果您使用的是log4net或类似的东西,请使用'* Format'方法.这将在内部使用StringBuilder,这样您就不会一直在支付不可变的字符串罚款.通常,关键是要能够关闭跟踪以提高性能,并且即使日志消息很昂贵,日志也要足够简洁以备不时之需.正确完成后,应该没有太多问题.

For string performance, use the '*Format' methods if you are using log4net or something similar. This internally will use a StringBuilder so that you aren't paying the immutable string penalty all the time. Generally though the key is to be able to turn tracing off for performance, and have logging be terse enough to leave on, even if a log message is expensive. When correctly done, there shouldn't be enough of them to be a problem.

这篇关于您遵循什么准则来编写良好的日志记录语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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