zoukankan      html  css  js  c++  java
  • Spring Boot 2 配置服务器访问日志

    Tomcat控制台中看到的日志是服务器的日志,而服务器访问日志则是记录服务处理的请求信息。

    开发环境:IntelliJ IDEA 2019.2.2
    Spring Boot版本:2.1.8

    1、新建一个名称为demo的Spring Boot项目。

    2、application.yml 添加配置

    server:
      tomcat:
        basedir: logs
        accesslog:
          pattern: '%t %m %s %v %U'
          enabled: true
          directory: access-logs
          buffered: false

    上面表示将日志存放于项目的logsaccess-logs目录下;
    buffered: false表示不缓冲,直接将日志记录到文件中;
    pattern的标识符一些常用取值如下:

    %a - 远端IP地址
    %A - 本地IP地址
    %b - 发送的字节数,不包括HTTP头,如果为0,使用"-"
    %B - 发送的字节数,不包括HTTP头
    %h - 远端主机名(如果resolveHost=false,远端的IP地址)
    %H - 请求协议
    %l - 从identd返回的远端逻辑用户名(总是返回 '-')
    %m - 请求的方法(GET,POST,等)
    %p - 收到请求的本地端口号
    %q - 查询字符串(如果存在,以 '?'开始)
    %r - 请求的第一行,包含了请求的方法和URI
    %s - 响应的状态码
    %S - 用户的session ID
    %t - 日志和时间,使用通常的Log格式
    %u - 认证以后的远端用户(如果存在的话,否则为'-')
    %U - 请求的URI路径
    %v - 本地服务器的名称
    %D - 处理请求的时间,以毫秒为单位
    %T - 处理请求的时间,以秒为单位

    3、修改启动类代码 DemoApplication.java

    package com.example.demo;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @SpringBootApplication
    @RestController
    public class DemoApplication  {
    
        public static void main(String[] args) {
            SpringApplication.run(DemoApplication.class, args);
        }
    
        @RequestMapping("/{name}")
        public String test(@PathVariable String name){
            return name;
        }
    }

    Run项目后,自动在项目目录中生成logsaccess-logsaccess_log.2019-09-25.log
    先后访问

    http://localhost:8080/111
    http://localhost:8080/aaa

    打开access_log.2019-09-25.log,可看到生成了2条记录:

    [25/Sep/2019:22:01:58 +0800] GET 200 localhost /111
    [25/Sep/2019:22:02:01 +0800] GET 200 localhost /aaa

    备注:
    本人版本是apache-tomcat-9.0.0.M11,默认是开启访问日志,打开tomcat/conf/server.xml文件,下面是开启访问日志。

     记录的文件放在/tomcat/logs目录下,文件命名为localhost_access_log.2019-09-25.txt这种形式,一天一个文件。

  • 相关阅读:
    topcoder srm 445 div1
    topcoder srm 440 div1
    topcoder srm 435 div1
    topcoder srm 430 div1
    topcoder srm 400 div1
    topcoder srm 380 div1
    topcoder srm 370 div1
    topcoder srm 425 div1
    WKWebView强大的新特性
    Runtime那些事
  • 原文地址:https://www.cnblogs.com/gdjlc/p/11588043.html
Copyright © 2011-2022 走看看