简体   繁体   English

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

[英]Difference between logger.info and logger.debug

What is the difference between logger.debug and logger.info ?是什么区别logger.debuglogger.info

When will logger.debug be printed? logger.debug什么时候打印?

I suggest you look at the article called "Short Introduction to log4j" .我建议你看看名为“Short Introduction to log4j”的文章。 It contains a short explanation of log levels and demonstrates how they can be used in practice.它包含对日志级别的简短说明,并演示如何在实践中使用它们。 The basic idea of log levels is that you want to be able to configure how much detail the logs contain depending on the situation.日志级别的基本思想是您希望能够根据情况配置日志包含的详细信息。 For example, if you are trying to troubleshoot an issue, you would want the logs to be very verbose.例如,如果您尝试对问题进行故障排除,您会希望日志非常详细。 In production, you might only want to see warnings and errors.在生产中,您可能只想看到警告和错误。

The log level for each component of your system is usually controlled through a parameter in a configuration file, so it's easy to change.系统每个组件的日志级别通常通过配置文件中的参数进行控制,因此很容易更改。 Your code would contain various logging statements with different levels.您的代码将包含不同级别的各种日志语句。 When responding to an Exception , you might call Logger.error .响应Exception ,您可能会调用Logger.error If you want to print the value of a variable at any given point, you might call Logger.debug .如果您想在任何给定点打印变量的值,您可以调用Logger.debug This combination of a configurable logging level and logging statements within your program allow you full control over how your application will log its activity.程序中可配置的日志级别和日志语句的这种组合允许您完全控制应用程序将如何记录其活动。

In the case of log4j at least, the ordering of log levels is:至少在 log4j 的情况下,日志级别的顺序是:

DEBUG < INFO < WARN < ERROR < FATAL

Here is a short example from that article demonstrating how log levels work.这是那篇文章中的一个简短示例,演示了日志级别的工作原理。

   // 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");

This will depend on the logging configuration.这将取决于日志记录配置。 The default value will depend on the framework being used.默认值将取决于所使用的框架。 The idea is that later on by changing a configuration setting from INFO to DEBUG you will see a ton of more (or less if the other way around) lines printed without recompiling the whole application.这个想法是,稍后通过将配置设置从 INFO 更改为 DEBUG,您将看到大量(或更少,如果相反)打印的行,而无需重新编译整个应用程序。

If you think which one to use then it boils down to thinking what you want to see on which level.如果您考虑使用哪个,那么归结为您想在哪个级别上看到什么。 For other levels for example in Log4J look at the API, http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/Level.html对于其他级别,例如在 Log4J 中查看 API, http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/Level.html

只是对所有可能级别的集合的澄清,即:

ALL < TRACE < DEBUG < INFO < WARN < ERROR < FATAL < OFF

Basically it depends on how your loggers are configured.基本上,这取决于您的记录器的配置方式。 Typically you'd have debug output written out during development but turned off in production - or possibly have selected debug categories writing out while debugging a particular area.通常,您会在开发期间写出调试输出但在生产中关闭 - 或者可能在调试特定区域时选择了写出的调试类别。

The point of having different priorities is to allow you to turn up/down the level of detail on a particular component in a reasonably fine-grained way - and only needing to change the logging configuration (rather than code) to see the difference.具有不同优先级的要点是允许您以合理的细粒度方式调高/调低特定组件的细节级别 - 并且只需要更改日志配置(而不是代码)即可查看差异。

  1. INFO is used to log the information your program is working as expected. INFO 用于记录您的程序按预期工作的信息。
  2. DEBUG is used to find the reason in case your program is not working as expected or an exception has occurred. DEBUG 用于查找原因,以防您的程序未按预期工作或发生异常。 it's in the interest of the developer.这符合开发商的利益。

This is a very old question, but i don't see my understanding here so I will add my 2 cents:这是一个非常古老的问题,但我在这里没有看到我的理解,所以我将添加我的 2 美分:

Every level corresponds/maps to a type of user:每个级别对应/映射到一种用户:

  • debug : developer - manual debugging调试:开发人员 - 手动调试
  • trace : automated logging and step tracer - for 3rd level support trace :自动日志记录和步骤跟踪器 - 用于第 3 级支持
  • info : technician / support level 1 /2信息:技术员/支持级别 1 /2
  • warn : technician / user error : automated alert / support level 1警告:技术人员/用户错误:自动警报/支持级别 1
  • critical/fatal : depends on your setup - local IT关键/致命:取决于您的设置 - 本地 IT

It depends on which level you selected in your log4j configuration file.这取决于您在 log4j 配置文件中选择的级别。

<Loggers>
        <Root level="info">
        ...

If your level is "info" (by default), logger.debug(...) will not be printed in your console.如果您的级别是“信息”(默认情况下), logger.debug(...)将不会打印在您的控制台中。 However, if your level is "debug", it will.但是,如果您的级别是“调试”,则可以。

Depending on the criticality level of your code, you should use the most accurate level among the following ones :根据您的代码的严重程度级别,您应该使用以下级别中最准确的级别:

ALL < TRACE < DEBUG < INFO < WARN < ERROR < FATAL < OFF

What is the difference between logger.debug and logger.info? logger.debug 和 logger.info 有什么区别?

These are only some default level already defined.这些只是一些已经定义的默认级别。 You can define your own levels if you like.如果您愿意,您可以定义自己的级别。 The purpose of those levels is to enable/disable one or more of them, without making any change in your code.这些级别的目的是启用/禁用其中一个或多个,而无需对代码进行任何更改。

When logger.debug will be printed ?? logger.debug 什么时候会被打印出来??

When you have enabled the debug or any higher level in your configuration.当您在配置中启用调试或任何更高级别时。

Info Messages are something which we would like to see even if the application is in best of state.即使应用程序处于最佳状态,我们也希望看到信息消息。

Debug messages are usually something that we would like to see while debugging some problem .调试消息通常是我们在调试某些问题时希望看到的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM