在springboot访问日志

1.废话不多说,直接上工程结构。

2.启动类随便加一个方法。

@GetMapping("/hello")
    public String hello() {
        return "hello";
    }

3.主要是yml文件的配置参数。

server:
  tomcat:
    basedir: my-tomcat
    accesslog:
      pattern: 'ip: %A, response code: %s, time: %t'
      enabled: true
      directory: crazyit-logs
      buffered: false

注:basedir指的是我们定义的根目录,而directory指的是生成的log存放目录,我们可根据需要修改目录路径。

4.pom文件。

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>org.crazyit.boot.c3</groupId>
    <artifactId>log-test</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.1.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    
</project>

5.访问localhost:8080/hello后,刷新工程后,可在指定目录看到生成的log文件。

原文地址:https://www.cnblogs.com/malun/p/13921652.html