用 Java 和一般的日志记录:最佳实践? [英] Logging in Java and in general: Best Practices?

查看:24
本文介绍了用 Java 和一般的日志记录:最佳实践?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有时当我看到我的日志代码时,我想知道我是否做得对.对此可能没有明确的答案,但我有以下担忧:

Sometimes when I see my logging code I wonder if I am doing it right. There might be no definitive answer to that, but I have the following concerns:

图书馆类

我有几个库类可能会记录一些 INFO 消息.致命错误报告为异常.目前,我的类中有一个静态记录器实例,类名作为日志记录名称.(Log4j 的:Logger.getLogger(MyClass.class))

I have several library classes which might log some INFO messages. Fatal Errors are reported as exceptions. Currently I have a static logger instance in my classes with the class name as the logging name. (Log4j's: Logger.getLogger(MyClass.class))

这是正确的方法吗?也许这个库类的用户不想要来自我的实现的任何消息,或者想要将它们重定向到特定于应用程序的日志.我应该允许用户从外部世界"设置记录器吗?您如何处理此类情况?

Is this the right way? Maybe the user of this library class doesn't want any messages from my implementation or wants to redirect them to an application specific log. Should I allow the user to set a logger from the "outside world"? How do you handle such cases?

一般日志

在某些应用程序中,我的类可能希望将日志消息写入未由类名称标识的特定日志.(即:HTTP 请求日志)做这样的事情最好的方法是什么?我想到了查找服务...

In some applications my classes might want to write a log message to a specific log not identified by the class' name. (I.e.: HTTP Request log) What is the best way to do such a thing? A lookup service comes to mind...

推荐答案

你的约定很标准也很好(恕我直言).

Your conventions are pretty standard and quite fine (imho).

需要注意的一件事是过多的未处理调试调用导致的内存碎片,因此,使用 Log4J(以及大多数其他 Java 日志记录框架),您最终会得到如下结果:

The one thing to watch is memory fragmentation from excessive unnedded debug calls so, with Log4J (and most other Java logging frameworks), you end up with something like this:

if (log.isDebugEnabled()) {
  log.debug("...");
}

因为构建该日志消息(您可能没有使用)可能很昂贵,尤其是在完成数千或数百万次之后.

because constructing that log message (that you probably aren't using) could be expensive, especially if done thousands or millions of times.

您的 INFO 级别日志记录不应过于健谈"(从您说的情况来看,听起来似乎并非如此).INFO 消息通常应该是有意义和重要的,就像正在启动和停止的应用程序一样.如果遇到问题,您可能想知道的事情.调试/精细级别日志记录更多地用于当您尝试诊断实际遇到的问题时.调试/精细日志记录通常仅在需要时打开.信息通常随时可用.

Your INFO level logging shouldn't be too "chatty" (and from what you say, it sounds like it isn't). INFO messages should be generally meaningful and significant, like the application being started and stopped. Things that you might want to know if you encounter a problem. Debug/fine level logging is more used for when you actually have a problem you're trying to diagnose. Debug/fine logging is typically only turned on when needed. Info is typically on all the time.

如果有人不想从您的课程中获取特定的 INFO 消息,他们当然可以随意更改您的 log4j 配置以不获取它们.Log4j 在这方面非常简单(与 Java 1.4 日志记录相反).

If someone doesn't want specific INFO messages from your classes, they are of course free to change your log4j configuration to not get them. Log4j is beautifully straightforward in this department (as opposed to Java 1.4 logging).

至于您的 HTTP 内容,我通常没有发现这是 Java 日志记录的问题,因为通常一个类负责您感兴趣的内容,因此您只需要将它放在一个地方.在(在我的经验中很少见)当您想要跨看似无关的类的公共日志消息时,只需放入一些可以轻松查找的标记.

As for your HTTP thing, I've generally not found that to be an issue with Java logging because typically a single class is responsible for what you're interested in so you only need to put it in one place. In the (rare in my experience) when you want common log messages across seemingly unrelated classes, just put in some token that can easily be grepped for.

这篇关于用 Java 和一般的日志记录:最佳实践?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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