记录错误的最佳做法是什么?

时间:2021-11-15 20:02:19

Many times I saw logging of errors like these:

很多次我看到记录这样的错误:

System.out.println("Method aMethod with parameters a:"+a+" b: "+b);
print("Error in line 88");

so.. What are the best practices to log an error?

那么..记录错误的最佳做法是什么?

EDIT:

This is java but could be C/C++, basic, etc.

这是java但可能是C / C ++,基本等。

8 个解决方案

#1


9  

Apache Commons Logging is not intended for applications general logging. It's intended to be used by libraries or APIs that don't want to force a logging implementation on the API's user.

Apache Commons Logging不适用于应用程序常规日志记录。它旨在供不希望强制API用户使用日志记录的库或API使用。

There are also classloading issues with Commons Logging.

Commons Logging也存在类加载问题。

Pick one of the [many] logging api's, the most widely used probably being log4j or the Java Logging API.

选择[很多]日志api中的一个,最广泛使用的可能是log4j或Java Logging API。

If you want implementation independence, you might want to consider SLF4J, by the original author of log4j.

如果您希望实现独立性,您可能需要考虑log4j的原作者SLF4J。

Having picked an implementation, then use the logging levels/severity within that implementation consistently, so that searching/filtering logs is easier.

选择了一个实现,然后一致地使用该实现中的日志记录级别/严重性,以便更容易搜索/过滤日志。

#2


23  

Logging directly to the console is horrendous and frankly, the mark of an inexperienced developer. The only reason to do this sort of thing is 1) he or she is unaware of other approaches, and/or 2) the developer has not thought one bit about what will happen when his/her code is deployed to a production site, and how the application will be maintained at that point. Dealing with an application that is logging 1GB/day or more of completely unneeded debug logging is maddening.

直接登录到控制台是可怕的,坦率地说,是没有经验的开发人员的标志。做这种事情的唯一原因是1)他或她不知道其他方法,和/或2)开发人员没有想到将他/她的代码部署到生产站点时会发生什么,以及如何在该点维护应用程序。处理记录1GB /天或更多完全不需要的调试日志记录的应用程序令人抓狂。

The generally accepted best practice is to use a Logging framework that has concepts of:

普遍接受的最佳实践是使用具有以下概念的Logging框架:

  1. Different log objects - Different classes/modules/etc can log to different loggers, so you can choose to apply different log configurations to different portions of the application.
  2. 不同的日志对象 - 不同的类/模块/等可以记录到不同的记录器,因此您可以选择将不同的日志配置应用于应用程序的不同部分。

  3. Different log levels - so you can tweak the logging configuration to only log errors in production, to log all sorts of debug and trace info in a development environment, etc.
  4. 不同的日志级别 - 因此您可以调整日志记录配置以仅记录生产中的错误,在开发环境中记录各种调试和跟踪信息等。

  5. Different log outputs - the framework should allow you to configure where the log output is sent to without requiring any changes in the codebase. Some examples of different places you might want to send log output to are files, files that roll over based on date/size, databases, email, remoting sinks, etc.
  6. 不同的日志输出 - 框架应该允许您配置日志输出发送到的位置,而不需要在代码库中进行任何更改。您可能希望将日志输出发送到的不同位置的一些示例是文件,基于日期/大小滚动的文件,数据库,电子邮件,远程接收器等。

  7. The log framework should never never never throw any Exceptions or errors from the logging code. Your application should not fail to load or fail to start because the log framework cannot create it's log file or obtain a lock on the file (unless this is a critical requirement, maybe for legal reasons, for your app).
  8. 日志框架绝不应该永远不会从日志代码中抛出任何异常或错误。您的应用程序应该无法加载或无法启动,因为日志框架无法创建它的日志文件或获取文件锁定(除非这是一个关键要求,可能出于法律原因,对您的应用程序)。

The eventual log framework you will use will of course depend on your platform. Some common options:

您将使用的最终日志框架当然取决于您的平台。一些常见的选择:

#3


2  

The easiest way to log errors in a consistent format is to use a logging framework such as Log4j (assuming you're using Java). It is useful to include a logging section in your code standards to make sure all developers know what needs to be logged. The nice thing about most logging frameworks is they have different logging levels so you can control how verbose the logging is between development, test, and production.

以一致格式记录错误的最简单方法是使用Log4j等日志框架(假设您使用的是Java)。在代码标准中包含一个日志记录部分非常有用,以确保所有开发人员都知道需要记录的内容。大多数日志记录框架的好处是它们具有不同的日志记录级别,因此您可以控制开发,测试和生产之间日志记录的详细程度。

#4


2  

A best practice is to use the java.util.logging framework

最佳实践是使用java.util.logging框架

Then you can log messages in either of these formats

然后,您可以使用以下任一格式记录消息

log.warning("..");
log.fine("..");
log.finer("..");
log.finest("..");

Or

log.log(Level.WARNING, "blah blah blah", e);

Then you can use a logging.properties (example below) to switch between levels of logging, and do all sorts of clever stuff like logging to files, with rotation etc.

然后你可以使用logging.properties(下面的例子)在日志记录级别之间切换,并做各种聪明的事情,如记录到文件,旋转等。

handlers = java.util.logging.ConsoleHandler

.level = WARNING

java.util.logging.ConsoleHandler.level = ALL

com.example.blah = FINE
com.example.testcomponents = FINEST

Frameworks like log4j and others should be avoided in my opinion, Java has everything you need already.

在我看来,应该避免使用像log4j这样的框架,Java已经拥有了你需要的一切。

EDIT

This can apply as a general practice for any programming language. Being able to control all levels of logging from a single property file is often very important in enterprise applications.

这可以作为任何编程语言的一般实践。能够从单个属性文件控制所有级别的日志记录在企业应用程序中通常非常重要。

#5


2  

Some suggested best-practices

一些建议的最佳做法

  • Use a logging framework. This will allow you to:

    使用日志框架。这将允许您:

    • Easily change the destination of your log messages
    • 轻松更改日志消息的目标

    • Filter log messages based on severity
    • 根据严重性过滤日志消息

    • Support internationalised log messages
    • 支持国际化日志消息

  • If you are using java, then slf4j is now preferred to Jakarta commons logging as the logging facade.

    如果您使用的是java,则slf4j现在更喜欢将Jakarta commons记录为日志记录外观。

  • As stated slf4j is a facade, and you have to then pick an underlying implementation. Either log4j, java.util.logging, or 'simple'.

    如上所述,slf4j是一个外观,然后你必须选择一个底层实现。 log4j,java.util.logging或'simple'。

  • Follow your framework's advice to ensuring expensive logging operations are not needlessly carried out

    按照您的框架建议,确保不必要地执行昂贵的日志记录操作

#6


0  

There really is no best practice for logging an error. It basically just needs to follow a consistent pattern (within the software/company/etc) that provides enough information to track the problem down. For Example, you might want to keep track of the time, the method, parameters, calling method, etc.

记录错误确实没有最佳实践。它基本上只需要遵循一致的模式(在软件/公司/等内),提供足够的信息来跟踪问题。例如,您可能希望跟踪时间,方法,参数,调用方法等。

So long as you dont just print "Error in "

只要你不打印“错误”

#7


0  

The apache common logging API as mentioned above is a great resource. Referring back to java, there is also a standard error output stream (System.err).

如上所述的apache通用日志记录API是一个很好的资源。回头参考java,还有一个标准的错误输出流(System.err)。

Directly from the Java API:

直接来自Java API:

This stream is already open and ready to accept output data.

此流已打开并准备接受输出数据。

Typically this stream corresponds to display output or another output destination specified by the host environment or user. By convention, this output stream is used to display error messages or other information that should come to the immediate attention of a user even if the principal output stream, the value of the variable out, has been redirected to a file or other destination that is typically not continuously monitored.

通常,该流对应于主机环境或用户指定的显示输出或另一输出目的地。按照惯例,此输出流用于显示应立即引起用户注意的错误消息或其他信息,即使主要输出流(变量out的值)已重定向到文件或其他目标,即通常不会持续监控。

#8


0  

Aside from technical considerations from other answers it is advisable to log a meaningful message and perhaps some steps to avoid the error in the future. Depending on the errors, of course.

除了来自其他答案的技术考虑之外,建议记录有意义的消息以及可能的一些步骤以避免将来出现错误。当然,这取决于错误。

You could get more out of a I/O-Error when the message states something like "Could not read from file X, you don't have the appropriate permission."

当消息指出“无法从文件X读取,您没有相应的权限”时,您可以从I / O错误中获得更多。

See more examples on SO or search the web.

在SO上查看更多示例或在网上搜索。

#1


9  

Apache Commons Logging is not intended for applications general logging. It's intended to be used by libraries or APIs that don't want to force a logging implementation on the API's user.

Apache Commons Logging不适用于应用程序常规日志记录。它旨在供不希望强制API用户使用日志记录的库或API使用。

There are also classloading issues with Commons Logging.

Commons Logging也存在类加载问题。

Pick one of the [many] logging api's, the most widely used probably being log4j or the Java Logging API.

选择[很多]日志api中的一个,最广泛使用的可能是log4j或Java Logging API。

If you want implementation independence, you might want to consider SLF4J, by the original author of log4j.

如果您希望实现独立性,您可能需要考虑log4j的原作者SLF4J。

Having picked an implementation, then use the logging levels/severity within that implementation consistently, so that searching/filtering logs is easier.

选择了一个实现,然后一致地使用该实现中的日志记录级别/严重性,以便更容易搜索/过滤日志。

#2


23  

Logging directly to the console is horrendous and frankly, the mark of an inexperienced developer. The only reason to do this sort of thing is 1) he or she is unaware of other approaches, and/or 2) the developer has not thought one bit about what will happen when his/her code is deployed to a production site, and how the application will be maintained at that point. Dealing with an application that is logging 1GB/day or more of completely unneeded debug logging is maddening.

直接登录到控制台是可怕的,坦率地说,是没有经验的开发人员的标志。做这种事情的唯一原因是1)他或她不知道其他方法,和/或2)开发人员没有想到将他/她的代码部署到生产站点时会发生什么,以及如何在该点维护应用程序。处理记录1GB /天或更多完全不需要的调试日志记录的应用程序令人抓狂。

The generally accepted best practice is to use a Logging framework that has concepts of:

普遍接受的最佳实践是使用具有以下概念的Logging框架:

  1. Different log objects - Different classes/modules/etc can log to different loggers, so you can choose to apply different log configurations to different portions of the application.
  2. 不同的日志对象 - 不同的类/模块/等可以记录到不同的记录器,因此您可以选择将不同的日志配置应用于应用程序的不同部分。

  3. Different log levels - so you can tweak the logging configuration to only log errors in production, to log all sorts of debug and trace info in a development environment, etc.
  4. 不同的日志级别 - 因此您可以调整日志记录配置以仅记录生产中的错误,在开发环境中记录各种调试和跟踪信息等。

  5. Different log outputs - the framework should allow you to configure where the log output is sent to without requiring any changes in the codebase. Some examples of different places you might want to send log output to are files, files that roll over based on date/size, databases, email, remoting sinks, etc.
  6. 不同的日志输出 - 框架应该允许您配置日志输出发送到的位置,而不需要在代码库中进行任何更改。您可能希望将日志输出发送到的不同位置的一些示例是文件,基于日期/大小滚动的文件,数据库,电子邮件,远程接收器等。

  7. The log framework should never never never throw any Exceptions or errors from the logging code. Your application should not fail to load or fail to start because the log framework cannot create it's log file or obtain a lock on the file (unless this is a critical requirement, maybe for legal reasons, for your app).
  8. 日志框架绝不应该永远不会从日志代码中抛出任何异常或错误。您的应用程序应该无法加载或无法启动,因为日志框架无法创建它的日志文件或获取文件锁定(除非这是一个关键要求,可能出于法律原因,对您的应用程序)。

The eventual log framework you will use will of course depend on your platform. Some common options:

您将使用的最终日志框架当然取决于您的平台。一些常见的选择:

#3


2  

The easiest way to log errors in a consistent format is to use a logging framework such as Log4j (assuming you're using Java). It is useful to include a logging section in your code standards to make sure all developers know what needs to be logged. The nice thing about most logging frameworks is they have different logging levels so you can control how verbose the logging is between development, test, and production.

以一致格式记录错误的最简单方法是使用Log4j等日志框架(假设您使用的是Java)。在代码标准中包含一个日志记录部分非常有用,以确保所有开发人员都知道需要记录的内容。大多数日志记录框架的好处是它们具有不同的日志记录级别,因此您可以控制开发,测试和生产之间日志记录的详细程度。

#4


2  

A best practice is to use the java.util.logging framework

最佳实践是使用java.util.logging框架

Then you can log messages in either of these formats

然后,您可以使用以下任一格式记录消息

log.warning("..");
log.fine("..");
log.finer("..");
log.finest("..");

Or

log.log(Level.WARNING, "blah blah blah", e);

Then you can use a logging.properties (example below) to switch between levels of logging, and do all sorts of clever stuff like logging to files, with rotation etc.

然后你可以使用logging.properties(下面的例子)在日志记录级别之间切换,并做各种聪明的事情,如记录到文件,旋转等。

handlers = java.util.logging.ConsoleHandler

.level = WARNING

java.util.logging.ConsoleHandler.level = ALL

com.example.blah = FINE
com.example.testcomponents = FINEST

Frameworks like log4j and others should be avoided in my opinion, Java has everything you need already.

在我看来,应该避免使用像log4j这样的框架,Java已经拥有了你需要的一切。

EDIT

This can apply as a general practice for any programming language. Being able to control all levels of logging from a single property file is often very important in enterprise applications.

这可以作为任何编程语言的一般实践。能够从单个属性文件控制所有级别的日志记录在企业应用程序中通常非常重要。

#5


2  

Some suggested best-practices

一些建议的最佳做法

  • Use a logging framework. This will allow you to:

    使用日志框架。这将允许您:

    • Easily change the destination of your log messages
    • 轻松更改日志消息的目标

    • Filter log messages based on severity
    • 根据严重性过滤日志消息

    • Support internationalised log messages
    • 支持国际化日志消息

  • If you are using java, then slf4j is now preferred to Jakarta commons logging as the logging facade.

    如果您使用的是java,则slf4j现在更喜欢将Jakarta commons记录为日志记录外观。

  • As stated slf4j is a facade, and you have to then pick an underlying implementation. Either log4j, java.util.logging, or 'simple'.

    如上所述,slf4j是一个外观,然后你必须选择一个底层实现。 log4j,java.util.logging或'simple'。

  • Follow your framework's advice to ensuring expensive logging operations are not needlessly carried out

    按照您的框架建议,确保不必要地执行昂贵的日志记录操作

#6


0  

There really is no best practice for logging an error. It basically just needs to follow a consistent pattern (within the software/company/etc) that provides enough information to track the problem down. For Example, you might want to keep track of the time, the method, parameters, calling method, etc.

记录错误确实没有最佳实践。它基本上只需要遵循一致的模式(在软件/公司/等内),提供足够的信息来跟踪问题。例如,您可能希望跟踪时间,方法,参数,调用方法等。

So long as you dont just print "Error in "

只要你不打印“错误”

#7


0  

The apache common logging API as mentioned above is a great resource. Referring back to java, there is also a standard error output stream (System.err).

如上所述的apache通用日志记录API是一个很好的资源。回头参考java,还有一个标准的错误输出流(System.err)。

Directly from the Java API:

直接来自Java API:

This stream is already open and ready to accept output data.

此流已打开并准备接受输出数据。

Typically this stream corresponds to display output or another output destination specified by the host environment or user. By convention, this output stream is used to display error messages or other information that should come to the immediate attention of a user even if the principal output stream, the value of the variable out, has been redirected to a file or other destination that is typically not continuously monitored.

通常,该流对应于主机环境或用户指定的显示输出或另一输出目的地。按照惯例,此输出流用于显示应立即引起用户注意的错误消息或其他信息,即使主要输出流(变量out的值)已重定向到文件或其他目标,即通常不会持续监控。

#8


0  

Aside from technical considerations from other answers it is advisable to log a meaningful message and perhaps some steps to avoid the error in the future. Depending on the errors, of course.

除了来自其他答案的技术考虑之外,建议记录有意义的消息以及可能的一些步骤以避免将来出现错误。当然,这取决于错误。

You could get more out of a I/O-Error when the message states something like "Could not read from file X, you don't have the appropriate permission."

当消息指出“无法从文件X读取,您没有相应的权限”时,您可以从I / O错误中获得更多。

See more examples on SO or search the web.

在SO上查看更多示例或在网上搜索。