logger.info 和 logger.debug 之间的区别

新手上路,请多包涵

logger.debuglogger.info 有什么区别?

什么时候打印 logger.debug

原文由 Senthilnathan 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 894
2 个回答

这将取决于日志记录配置。默认值将取决于所使用的框架。这个想法是,稍后通过将配置设置从 INFO 更改为 DEBUG,您将看到打印出更多(或更少,如果相反)行,而无需重新编译整个应用程序。

如果您考虑使用哪一个,那么归根结底就是考虑您想在哪个级别上看到什么。对于其他级别,例如 Log4J,请查看 API,http: //logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/Level.html

原文由 toomasr 发布,翻译遵循 CC BY-SA 2.5 许可协议

我建议您看一下名为 “log4j 的简短介绍” 的文章。它包含日志级别的简短说明,并演示了如何在实践中使用它们。日志级别的基本思想是您希望能够根据情况配置日志包含多少详细信息。例如,如果您正在尝试解决问题,您会希望日志非常冗长。在生产中,您可能只想看到警告和错误。

系统每个组件的日志级别通常通过配置文件中的参数控制,因此很容易更改。您的代码将包含各种不同级别的日志记录语句。响应 Exception 时,您可以调用 Logger.error 。如果您想在任何给定点打印变量的值,您可以调用 Logger.debug 。程序中可配置的日志级别和日志语句的这种组合允许您完全控制应用程序记录其活动的方式。

至少在 log4j 的情况下,日志级别的顺序是:

 DEBUG < INFO < WARN < ERROR < FATAL

这是该文章中的一个简短示例,演示了日志级别的工作原理。

    // get a logger instance named "com.foo"
   Logger logger = Logger.getLogger("com.foo");

   // Now set its level. Normally you do not need to set the
   // level of a logger programmatically. This is usually done
   // in configuration files.
   logger.setLevel(Level.INFO);

   Logger barlogger = Logger.getLogger("com.foo.Bar");

   // This request is enabled, because WARN >= INFO.
   logger.warn("Low fuel level.");

   // This request is disabled, because DEBUG < INFO.
   logger.debug("Starting search for nearest gas station.");

   // The logger instance barlogger, named "com.foo.Bar",
   // will inherit its level from the logger named
   // "com.foo" Thus, the following request is enabled
   // because INFO >= INFO.
   barlogger.info("Located nearest gas station.");

   // This request is disabled, because DEBUG < INFO.
   barlogger.debug("Exiting gas station search");

原文由 William Brendel 发布,翻译遵循 CC BY-SA 2.5 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题