简体   繁体   中英

SLF4J logging, different Levels

In SLF4J (Logging) how levels are different in characteristic. ie How ERROR message is different than DEBUG message.

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. 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. 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. TRACE level is even more granular. 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. You can specify at what level the application can debug. If everything goes well in application, I would leave it at INFO level. 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.

Also, understand that when you run the debugging at DEBUG level, even the INFO level logs will be printed. If you turn on the debugged at TRACE level, even the DEBUG and INFO level logs will be printed. If you turn on debugging at INFO level, only INFO level logs will be printed.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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