【解决】YAML配置log日志输出格式‘%’报错,character ‘%‘ that cannot start any token,Caused by: org.yaml.snakeyaml.sca

YAML配置log日志输出格式‘%’报错,Caused by: org.yaml.snakeyaml.scanner.ScannerException: while scanning for the next token

  • 问题
  • 原因
  • 解决

found character ‘%’ that cannot start any token. (Do not use % for indentation))

问题

YAML配置log日志输出格式‘%’报错:
Caused by: org.yaml.snakeyaml.scanner.ScannerException: while scanning for the next token
found character ‘%’ that cannot start any token. (Do not use % for indentation)
【解决】YAML配置log日志输出格式‘%’报错,character ‘%‘ that cannot start any token,Caused by: org.yaml.snakeyaml.sca_第1张图片
YAML:

logging:
  level:
    com:
      xc: trace
  path: ./spring/log

  # 在控制台输出的日志的格式
  pattern:
    console: %d{yyyy‐MM‐dd} [%thread] %‐5level %logger{50} ‐ %msg%n
    file: %d{yyyy‐MM‐dd} === [%thread] === %‐5level === %logger{50} ==== %msg%n

原因

分析为YAML不能直接转译%类符号

解决

修改YAML中日志输出格式,把console,file值,前后都加上单引号’, 如下:

console修改前:%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n
console修改后:‘%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n’

file修改前:%d{yyyy‐MM‐dd HH:mm:ss.SSS} === [%thread] === %‐5level === %logger{50} ==== %msg%n
file修改后:‘%d{yyyy-MM-dd HH:mm:ss.SSS} === [%thread] === %-5level === %logger{50} ==== %msg%n’

logging:
  level:
    com:
      xc: trace
  path: ./spring/log

  # 在控制台输出的日志的格式
  pattern:
    console: '%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n'
    file: '%d{yyyy-MM-dd HH:mm:ss.SSS} === [%thread] === %-5level === %logger{50} ==== %msg%n'

再次执行,一切正常,完美!

补充小知识,日志输出格式解析:

日志输出格式:
%d表示日期时间,
%thread表示线程名,
%‐5level:级别从左显示5个字符宽度
%logger{50} 表示logger名字最长50个字符,否则按照句点分割。
%msg:日志消息,
%n是换行符

你可能感兴趣的:(intellij,idea,开发环境,日志,java,spring,boot,log4j)