一般情况下登录Java:最佳实践? [英] Logging in Java and in general: Best Practices?

查看:73
本文介绍了一般情况下登录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 Request log)做这种事情的最佳方法是什么?想到了查找服务...

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...

推荐答案

您的约定很标准,也很不错(imho).

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天全站免登陆