引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
其次,创建一个自定义的Controller类,该类处理/actuator/logfile路由的请求。可以参考以下示例代码:
import org.springframework.core.io.Resource;
import org.springframework.http.MediaType;
import org.springframework.util.StreamUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
@RestController
public class LogFileController {
@GetMapping(value = "/actuator/logfile", produces = MediaType.TEXT_PLAIN_VALUE)
public String getLogFile() throws Exception {
Resource resource = new ClassPathResource("logs/application.log"); // 指定日志文件路径
byte[] bytes = StreamUtils.copyToByteArray(resource.getInputStream());
return new String(bytes, StandardCharsets.UTF_8);
}
}
上述代码中,我们假设日志文件位于classpath根目录下的"logs/"子目录中,名称为"application.log"。你可以根据实际情况修改路径和文件名。
最后,重新编译、构建和部署应用程序,然后就可以通过访问http://localhost:8080/actuator/logfile来查看历史日志文件的内容了。
配置文件添加
management.endpoints.web.exposure.include= loggers
之后访问http://localhost:8080/xxx/actuator/loggers
即可看到每个文件的日志级别
查看某个目录的日志级别
http://localhost:8080/xxx/actuator/loggers](http://localhost:8080/rose-server/actuator/loggers/{目录名}
可以配置指定目录的日志级别
post请求修改指定目录日志级别:
http://localhost:8080/xxx/actuator/loggers/{目录名}
请求提
{
"configuredLevel": "debug"
}
再次查看此目录的日志级别发现已被修改
http://localhost:8080/xxx/actuator/loggers](http://localhost:8080/rose-server/actuator/loggers/{目录名}
本文介绍了如何在SpringBoot项目中使用ActuatorAPI来访问和管理日志文件,包括自定义Controller处理logfile请求,配置文件设置web接口暴露日志,以及调整特定目录的日志级别。
513





