99

学习 Fluentd(二):收集 Spring Boot 日志

 4 years ago
source link: http://muziyuchen.com/fluentd-2/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

更新至 Fluentd 1.0 版本

更新至 Spring Boot 2.2.2.RELEASE 版本

Logback

Spring Boot 默认使用 Logback 记录日志。

Logback 日志文件配置:

<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">  
    <File>/var/logs/app/server.log</File>
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
        <FileNamePattern>/var/logs/app/history/server.%d{yyyy-MM-dd}.log.gz</FileNamePattern>
        <maxHistory>30</maxHistory>
    </rollingPolicy>
    <layout class="ch.qos.logback.classic.PatternLayout">
        <pattern>%date [%thread] %-5level %logger{50}:%L - %msg%n</pattern>
    </layout>
</appender>

日志文件路径为 /var/logs/app/server.log,日志文件格式为 %date [%thread] %-5level %logger{50}:%L - %msg%n

in_tail 插件 和 multiline 插件

fluentd 配置:

<source>  
  @type tail
  path /var/logs/app/server.log
  pos_file /var/logs/app/.server.log.pos
  tag server.log
  <parse>
    @type multiline
    format_firstline /\d{4}-\d{1,2}-\d{1,2}/
    format1 /^(?<time>\d{4}-\d{1,2}-\d{1,2} \d{1,2}:\d{1,2}:\d{1,2},\d{3}) \[(?<thread>.*)\] (?<level>[^\s]+) (?<message>.*)/
  </parse>
</source>

配置项 tail.path 指定日志路径;

配置项 tail.pos_file 指定日志文件记录最后读取位置的文件路径;

配置项 multiline.format_firstline 指定第一行的正则表达式;

配置项 multiline.formatN 指定地 N 行正则表达式。

参考


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK