spring-boot的默认日志配置

Apr 24, 2018

今天配置一下 spring-boot 的默认日志,踩过几个坑, 分享一下, 版本是 1.5.9.RELEASE

首先看看 tomcat accesslog 的配置(直接粘贴):

server.tomcat.accesslog.directory=logs # Directory in which log files are created. Can be relative to the tomcat base dir or absolute.
server.tomcat.accesslog.enabled=false # Enable access log.
server.tomcat.accesslog.prefix=access_log # Log file name prefix.
server.tomcat.accesslog.file-date-format=.yyyy-MM-dd # Date format to place in log file name.
server.tomcat.accesslog.suffix=.log # Log file name suffix.

注意:

  1. server.tomcat.accesslog.enabled 首先启用
  2. 日志的文件名格式是: server.tomcat.accesslog. prefix.file-date-format.suffix
  3. server.tomcat.accesslog.directory 这是一个 ** 坑 **,
    • windows下 使用全路径需要设置盘符否则认为是相对路径,相对 server.tomcat.basedir
    • linux 全路径 / 开头即可
  4. server.tomcat.basedir windows下 绝对路径也需要有盘符,否则为相对路径(相对当前的工作目录)
logging.file= # Log file name. For instance `myapp.log`
logging.level.*= # Log levels severity mapping. For instance `logging.level.org.springframework=DEBUG`
logging.path= # Location of the log file. For instance `/var/log`

注意:

  1. logging.path logging.path / logging.file 只能选择一个, logging.file 的优先级更高

我的最终配置 application.yml

spring:
  application:
    name: spring-demo
#
logging:
  path: /var/log/${spring.application.name:sboot}/ # 文件名为 spring.log
#
server:
  port: 8080
  tomcat:
    basedir: /tmp
    accesslog:
      enabled: true
      directory: ${logging.path}

(完)