- spring initializr 自动包含依赖,也可以在 pom.xml 文件中添加 slf4j 的依赖,指定版本
例如我这边默认版本是 1.7.36
通过添加依赖修改版本为 1.7.32
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.32</version> <!-- 版本号可能会有所不同 -->
</dependency>
2. 在类中调用方法
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @Description
* @Author jimaomao
* @DATE 2024/3/14 16:48
*/
@RestController
public class MySlf4j {
private static final Logger logger = LoggerFactory.getLogger(MySlf4j.class);
@GetMapping("/mySlf4j")
public String mySlf4j () {
logger.info("Executing method {} in class {}", new Object(){}.getClass().getEnclosingMethod().getName(), this.getClass().getName());
return "Executing method " + new Object(){}.getClass().getEnclosingMethod().getName() + " in class " + this.getClass().getName();
}
}
- 查看日志