如何在java中使用日志级别 [英] How to use log levels in java

查看:418
本文介绍了如何在java中使用日志级别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个需要使用记录器功能的应用程序。我已经阅读了不同级别的记录器:

I am developing an application where i need to use the logger functionality. I have read about different levels of logger which are:


  • 严重(最高)

  • 警告

  • INFO

  • CONFIG

  • FINE

  • FINER

  • FINEST

  • SEVERE (highest)
  • WARNING
  • INFO
  • CONFIG
  • FINE
  • FINER
  • FINEST

我无法理解每个日志记录级别的使用情况。

I am not able to understand the usage of each logging level.

有人能举例说明所有日志记录级别及其用法吗?

Can someone give me a good example showing all the logging levels and their usage?

推荐答案

通常,您不需要所有这些级别,严重,警告,信息,精细可能就足够了。我们正在使用Log4J(不是直接java.util.logging)和以下级别(名称可能与其他日志框架不同):

Generally, you don't need all those levels, SEVERE, WARNING, INFO, FINE might be enough. We're using Log4J (not java.util.logging directly) and the following levels (which might differ in name from other logging frameworks):


  • 错误:任何或可能是关键的错误/异常。我们的Logger会自动在我们的服务器上发送每封邮件的电子邮件(用法: logger.error(message);

警告:任何可能警告我们潜在问题的消息,例如当用户尝试使用错误的凭据登录时 - 如果经常或在短时间内发生这种情况可能表示发生攻击(使用情况: logger.warn(message);

WARN: Any message that might warn us of potential problems, e.g. when a user tried to log in with wrong credentials - which might indicate an attack if that happens often or in short periods of time (usage: logger.warn("message"); )

信息:查看日志文件时我们想知道的任何事情,例如当预定作业开始/结束时(用法: logger.info(message);

INFO: Anything that we want to know when looking at the log files, e.g. when a scheduled job started/ended (usage: logger.info("message"); )

DEBUG:顾名思义,调试我们很少打开的消息。 (用法: logger.debug(message);

DEBUG: As the name says, debug messages that we only rarely turn on. (usage: logger.debug("message"); )

这样做的好处是,如果您将日志级别设置为WARN,那么信息和调试消息几乎不会对性能产生影响。如果您需要从生产系统获取其他信息,您可以在短时间内将级别降低到INFO或DEBUG(因为您将获得更多日志条目,这会使您的日志文件变得更大,更难阅读)。调整日志级别等通常可以在运行时完成(我们的JBoss实例每分钟检查一次配置中的更改)。

The beauty of this is that if you set the log level to WARN, info and debug messages have next to no performance impact. If you need to get additional information from a production system you just can lower the level to INFO or DEBUG for a short period of time (since you'd get much more log entries which make your log files bigger and harder to read). Adjusting log levels etc. can normally be done at runtime (our JBoss instance checks for changes in that config every minute or so).

这篇关于如何在java中使用日志级别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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