It is recommended to use SLF4J (Simple Logging Facade for Java) as a logging API, SLF4J is a simple Facade for logging systems that allows end users to use their desired logging system when deploying their applications.
1. Advantages of Slf4j
Compared to using apache commons-logging or directly using log4j, SLF4J provides an advanced feature called parameterized logs that can significantly improve log statement performance when configured to shut down logs,
log.debug("Found {} records matching filter: '{}'", records, filter);//slf4j
log.debug("Found " + records + " records matching filter: '" + filter + "'");//log4j
It can be seen that the advantages of SLF4J are: simpler and easier to read; when the log level is not enough, the overhead of string splicing is missing, and the toString method of the object (records/filter) will not be called.
After 1.6.0, Slf4j supports printing of exception stacks, and can be passed in as the last parameter, which basically meets common log printing scenarios.
log.error("Failed to format {}", s, e);
2. Pay attention to the inheritance relationship between Loggers
The inheritance of logger is achieved through naming.
The child logger will inherit the appender of the parent logger by default and add them to its own Appender; unless addition="false" is added, the appender of the parent logger will no longer be inherited.
The child logger will inherit the output level of the parent logger only if it does not define its output level.
The above comprehensive understanding of slf4j and log4j is all the content I share with you. I hope you can give you a reference and I hope you can support Wulin.com more.