简体   繁体   English

SLF4J日志记录,不同级别

[英]SLF4J logging, different Levels

In SLF4J (Logging) how levels are different in characteristic. 在SLF4J(日志记录)中,级别的特性如何不同。 ie How ERROR message is different than DEBUG message. 即ERROR消息与DEBUG消息有何不同。

import org.apache.log4j.Logger;

public class LogClass {

    private static org.apache.log4j.Logger log = Logger.getLogger(LogClass.class);   
    public static void main(String[] args) {

        log.trace("Trace Message!")
        log.debug("Debug Message!");
        log.info("Info Message!");
        log.warn("Warn Message!");
        log.error("Error Message!");
        log.fatal("Fatal Message!");
    }
}

The Output is same regardless of Level, is there any difference in implementation: 不论级别如何,输出都是相同的,实现上是否有所不同:

Debug Message!
Info Message!
Warn Message!
Error Message!
Fatal Message!

If these levels are producing the same kind of messages then why the implementation didn't have only one method with parameter as level. 如果这些级别正在产生相同类型的消息,那么为什么实现中没有仅一个参数作为级别的方法。 Something like: 就像是:

log("Level","msg");

Starting from the bottom, there's no real benefit to have a log(level, msg) method if you already have all the different methods for all the possible levels. 从底部开始,如果您已经拥有所有可能级别的所有不同方法,那么使用log(level, msg)方法并没有真正的好处。 Only if you'd need to log the same message in different levels, but that's a bad practice, since that message should clearly fall into one specific category. 仅当您需要以不同级别记录同一条消息时,但这是一个不好的做法,因为该消息显然应该属于一个特定类别。 And you can always choose how much logging you get out by specifying the level globally or at the package/class. 而且,您始终可以通过全局或在包/类中指定级别来选择获取多少日志。 The message are exactly the same on each level, the only difference is if that message is gonna make to the logging output or not, based on your configuration, and what purpose do you give to each level. 该消息在每个级别上都是完全相同的,唯一的区别是,该消息是否要根据您的配置生成到日志输出中,以及您对每个级别给出什么目的。

The key purpose to name them levels is to enable you to debug at various levels. 给它们命名级别的主要目的是使您能够在各个级别上进行调试。 Say for example, INFO level can used to log high level information on the progress of the application during execution. 例如,INFO级别可用于在执行期间记录有关应用程序进度的高级信息。 DEBUG level logged is meant to be even deeper than just high level information. 记录的调试级别不仅限于高级信息,更深层的。 At DEBUG level, you can have more information logged that can include information of what is happening at a module level or component level. 在DEBUG级别,您可以记录更多信息,其中包括有关模块级别或组件级别发生的情况的信息。 TRACE level is even more granular. TRACE级别更加精细。 You can log message like entering and exiting a method and what information is being returned by each method. 您可以记录诸如进入和退出方法之类的消息,以及每种方法所返回的信息。 ERROR level is to purely meant to log only errors and exception 错误级别仅用于记录错误和异常

You need to be mindful of what kind of message can be logged into their respective level. 您需要注意可以将哪种消息登录到各自的级别。

To answer your question, these levels can be controlled in log4j.properties or log4j.xml. 为了回答您的问题,可以在log4j.properties或log4j.xml中控制这些级别。 You can specify at what level the application can debug. 您可以指定应用程序可以调试的级别。 If everything goes well in application, I would leave it at INFO level. 如果在应用程序中一切顺利,我将其保留在INFO级别。 If something goes wrong and I wanted to dig in deepeur in terms of debugging, I would try to turn on at DEBUG level or TRACE level. 如果出现问题,并且我想在调试方面进行更深入的研究,我将尝试在DEBUG级别或TRACE级别打开。

Also, understand that when you run the debugging at DEBUG level, even the INFO level logs will be printed. 另外,请理解,当您在DEBUG级别运行调试时,甚至会打印INFO级别的日志。 If you turn on the debugged at TRACE level, even the DEBUG and INFO level logs will be printed. 如果在TRACE级别打开调试,则甚至会打印DEBUG和INFO级别的日志。 If you turn on debugging at INFO level, only INFO level logs will be printed. 如果在INFO级别打开调试,则仅会打印INFO级别的日志。

I hope you got some clarify. 希望您能澄清一下。

Because it is easier to use for you as a user. 因为它更易于您使用。 As the implementation, it might have that very code. 作为实现,它可能具有该代码。

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

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