spring boot:tomcat的accesslog按日期存放(spring boot v2.5.4)


一,spring boot配置accesslog

application.yml配置
#server
server:
  port: 10800
  error:
    include-stacktrace: always
  #内嵌tomcat日志
  tomcat:
    accesslog:
      buffered: true
      directory: /data/store/back/logs
      enabled: true
      file-date-format: yyyy-MM-dd
      pattern: '%{X-Real-Ip}i %l %T %t %r %s %b %{Referer}i %{User-Agent}i'
      prefix: accesslog
      rename-on-rotate: false
      request-attributes-enabled: false
      rotate: true
      suffix: .log
    connection-timeout: 20000
    max-http-form-post-size: 30MB

说明:刘宏缔的架构森林是一个专注架构的博客,地址:https://www.cnblogs.com/architectforest

         对应的源码可以访问这里获取: https://github.com/liuhongdi/
         或: https://gitee.com/liuhongdi

说明:作者:刘宏缔 邮箱: 371125307@qq.com

二,定时移动昨天的日志到相应的月份目录下:

编写脚本
[lhdop@blog tools]$ more tomcatAccesslogRotate.sh
#!/bin/bash
#Rotate the Nginx logs to prevent a single logfile from consuming too much disk space.
LOGS_PATH=/data/store/back/logs
YESTERDAY=$(date -d "yesterday" +%Y-%m-%d)
CURRENT_LOG=${LOGS_PATH}/accesslog${YESTERDAY}.log
echo "current log path:$CURRENT_LOG";
MONTH=$(date -d "yesterday" +%Y-%m)
DEST_PATH=${LOGS_PATH}/${MONTH}
echo "dest path:$DEST_PATH";
if [ -f ${CURRENT_LOG} ];then
      echo "file exist"
      mv ${CURRENT_LOG} ${DEST_PATH}
else
      echo "file not exist"
fi
定时运行
[lhdop@blog tools]$ crontab -l
1 0 * * * /data/store/back/tools/tomcatAccesslogRotate.sh

三,测试效果:

[lhdop@blog logs]$ ll 2022-03/
total 640
-rw-rw-r-- 1 lhdop lhdop  33535 Mar 10 18:17 accesslog2022-03-10.log
-rw-rw-r-- 1 lhdop lhdop 101630 Mar 10 18:17 back-2022-03-10.log
-rw-rw-r-- 1 lhdop lhdop 509111 Mar 10 17:07 back2022-03-10.log
-rw-rw-r-- 1 lhdop lhdop   3639 Mar  7 15:15 info-2022-03-07-1.log
可以看到移动过来的accesslog日志文件  

四,查看spring boot版本 

  .   ____          _            __ _ _
/\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot ::                (v2.5.4) 

五,查看linux版本 

[lhdop@blog tools]$ cat /etc/os-release
NAME="CentOS Linux"
VERSION="8 (Core)"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="8"
PLATFORM_ID="platform:el8"
PRETTY_NAME="CentOS Linux 8 (Core)"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:centos:centos:8"
HOME_URL="https://www.centos.org/"
BUG_REPORT_URL="https://bugs.centos.org/"
 
CENTOS_MANTISBT_PROJECT="CentOS-8"
CENTOS_MANTISBT_PROJECT_VERSION="8"
REDHAT_SUPPORT_PRODUCT="centos"
REDHAT_SUPPORT_PRODUCT_VERSION=“8"