http://www.slf4j.org/
官网:
The Simple Logging Facade for Java (SLF4J) serves as a simple facade or abstraction for various logging frameworks (e.g. java.util.logging, logback, log4j) allowing the end user to plug in the desired logging framework at deployment time.
由于,SLF4J是一个日志门面框架并非具体日志实现,所以需要和其他框架搭配使用。这里使用 logback 作为例子
<!-- 日志:slf4j + logback --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.31</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-core</artifactId> <version>1.2.3</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.2.3</version> <scope>compile</scope> </dependency>
<?xml version="1.0" encoding="UTF-8"?> <configuration scan="true" > <contextName>logback</contextName> <property name="log.path" value="./logs" /> <property name="CONSOLE_LOG_PATTERN" value="%d{yyyy-MM-dd HH:mm:ss.SSS} ${LOG_LEVEL_PATTERN:-%5p} ${PID:- } --- [%15.15t] %-40.40logger{39} : %m%n}"/> <!--1. 输出到控制台--> <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender"> <!--此日志appender是为开发使用,只配置最底级别,控制台输出的日志级别是大于或等于此级别的日志信息--> <filter class="ch.qos.logback.classic.filter.ThresholdFilter"> <level>INFO</level> </filter> <encoder> <pattern>${CONSOLE_LOG_PATTERN}</pattern> <!-- 设置字符集 --> <charset>UTF-8</charset> </encoder> </appender> <!--root 标签和其他logger标签起相同的作用,只是其他logger 可以继承此标签的属性--> <root> <appender-ref ref="CONSOLE" /> </root> </configuration>
package com.black.nginx.app; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class NginxApp { private static Logger logger = LoggerFactory.getLogger(NginxApp.class); public static void main(String[] args) { logger.info("app start ..."); logger.info("app shutdown ."); } }
2021-07-13 23:19:41.591 INFO --- [ main] com.black.nginx.app.NginxApp : app start ... 2021-07-13 23:19:41.596 INFO --- [ main] com.black.nginx.app.NginxApp : app shutdown .