使用 java.util.logging 的好例子

发布于 2024-11-06 01:19:10 字数 1705 浏览 3 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

审判长 2024-11-13 01:19:10

java.util.logging 使您不必在应用程序中携带多个 jar 文件,并且它与良好的 Formatter 配合使用效果很好。

一般来说,在每个类的顶部,您应该:

private static final Logger LOGGER = Logger.getLogger( ClassName.class.getName() );

然后,您可以使用 Logger 类。


对于在执行流顶层进行调试的任何操作,请使用Level.FINE

LOGGER.log( Level.FINE, "processing {0} entries in loop", list.size() );

使用Level.FINER / Level.FINEST 在循环内部以及在调试基本流程问题时可能并不总是需要查看太多细节的地方:

LOGGER.log( Level.FINER, "processing[{0}]: {1}", new Object[]{ i, list.get(i) } );

使用日志记录工具的参数化版本来避免生成大量字符串连接垃圾GC 必须跟上。上面的 Object[] 很便宜,通常在堆栈上分配。


对于异常处理,始终记录完整的异常详细信息:

try {
    ...something that can throw an ignorable exception
} catch( Exception ex ) {
    LOGGER.log( Level.SEVERE, ex.toString(), ex );
}

我总是将 ex.toString() 作为消息传递到此处,因为当我“grep -n” for “< code>Exception”在日志文件中,我也可以看到该消息。否则,它将出现在堆栈转储生成的下一行输出中,并且您还必须有一个更高级的正则表达式来匹配该行,这通常会为您提供比您需要查看的更多的输出。

java.util.logging keeps you from having to tote one more jar file around with your application, and it works well with a good Formatter.

In general, at the top of every class, you should have:

private static final Logger LOGGER = Logger.getLogger( ClassName.class.getName() );

Then, you can just use various facilities of the Logger class.


Use Level.FINE for anything that is debugging at the top level of execution flow:

LOGGER.log( Level.FINE, "processing {0} entries in loop", list.size() );

Use Level.FINER / Level.FINEST inside of loops and in places where you may not always need to see that much detail when debugging basic flow issues:

LOGGER.log( Level.FINER, "processing[{0}]: {1}", new Object[]{ i, list.get(i) } );

Use the parameterized versions of the logging facilities to keep from generating tons of String concatenation garbage that GC will have to keep up with. Object[] as above is cheap, on the stack allocation usually.


With exception handling, always log the complete exception details:

try {
    ...something that can throw an ignorable exception
} catch( Exception ex ) {
    LOGGER.log( Level.SEVERE, ex.toString(), ex );
}

I always pass ex.toString() as the message here, because then when I "grep -n" for "Exception" in log files, I can see the message too. Otherwise, it is going to be on the next line of output generated by the stack dump, and you have to have a more advanced RegEx to match that line too, which often gets you more output than you need to look through.

咿呀咿呀哟 2024-11-13 01:19:10

应该像这样声明记录器:

private final static Logger LOGGER = Logger.getLogger(MyClass.class.getName());

所以如果你重构你的类名,它就会遵循。

我写了一篇关于java记录器的文章 示例

Should declare logger like this:

private final static Logger LOGGER = Logger.getLogger(MyClass.class.getName());

so if you refactor your class name it follows.

I wrote an article about java logger with examples here.

入怼 2024-11-13 01:19:10

有很多示例,也有不同类型的日志记录。查看 java.util。日志记录包。

示例代码:

import java.util.logging.Logger;

public class Main {

  private static Logger LOGGER = Logger.getLogger("InfoLogging");

  public static void main(String[] args) {
    LOGGER.info("Logging an INFO-level message");
  }
}

无需对 类进行硬编码名称

import java.util.logging.Logger;

public class Main {
  private static final Logger LOGGER = Logger.getLogger(
    Thread.currentThread().getStackTrace()[0].getClassName() );

  public static void main(String[] args) {
    LOGGER.info("Logging an INFO-level message");
  }
}

There are many examples and also of different types for logging. Take a look at the java.util.logging package.

Example code:

import java.util.logging.Logger;

public class Main {

  private static Logger LOGGER = Logger.getLogger("InfoLogging");

  public static void main(String[] args) {
    LOGGER.info("Logging an INFO-level message");
  }
}

Without hard-coding the class name:

import java.util.logging.Logger;

public class Main {
  private static final Logger LOGGER = Logger.getLogger(
    Thread.currentThread().getStackTrace()[0].getClassName() );

  public static void main(String[] args) {
    LOGGER.info("Logging an INFO-level message");
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文