Skip to content

日志收集

简绍

项目中支持以下日志收集方式,可以根据自己的需要进行增删,更多功能见日志扩展模块,主要分为以下几块:

  • 控制台输出
  • log日志文件
  • ELK收集
  • plumelog
  • plumelog(嵌入式Lite模式)

日志配置文件

通过bootx-start模块下logback-spring.xml文件进行项目日志输出的配置,项目中以及预先定义了下列几种常用的日志收集类型,存储在common-log模块下,也可以根据实际需要进行修改 可以直接引用进行使用

  • logback-sensitive 敏感数据脱敏,配置所在位置cn/bootx/platform/common/log/logback-sensitive.xml
  • logback-console 控制台打印输出配置,配置所在位置cn/bootx/platform/common/log/logback-console.xml
  • logback-log-file 传统日志文件格式,配置所在位置cn/bootx/platform/common/log/logback-log-file.xml
  • logback-elk elk配套日志收集,输出json,通过filebeat 写入ES,通过kibana进行管理,配置所在位置cn/bootx/common/log/logback-elk.xml
  • logback-plumelog-embed 配套PlumeLog嵌入式Lite模式 日志收集,PlumeLog 是个轻量级的日志收集和管理组件,配置所在位置cn/bootx/common/log/platform/logback-plumelog.xml
xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <include resource="org/springframework/boot/logging/logback/defaults.xml"/>
    <!--项目名称-->
    <property name="appname" value="bootx-platform"/>
    <!--日志位置-->
    <property name="logdir" value="logs"/>

    <!-- 选择要导入使用的默认日志配置 -->
    <include resource="cn/bootx/platform/common/log/logback-sensitive.xml"/>
    <include resource="cn/bootx/platform/common/log/logback-console.xml"/>
    <include resource="cn/bootx/platform/common/log/logback-log-file.xml"/>
    <include resource="cn/bootx/platform/common/log/logback-elk.xml"/>
    <include resource="cn/bootx/platform/common/log/logback-plumelog.xml"/>
    <!-- 开发输出级别 -->
    <root level="INFO">
        <appender-ref ref="console"/>
        <appender-ref ref="fileInfoLog"/>
<!--        <appender-ref ref="elk"/>-->
<!--        <appender-ref ref="plumelog"/>-->
    </root>
</configuration>

启用外置日志配置文件

Spring Boot项目以Jar方式启动时默认会读取Jar里的logback-spring.xml配置文件,哪怕我们在Jar的外部提供了别的配置文件,也不会进行读取, 所以需要我们在启动Jar的时候,在启动参数中显式指定,来覆盖系统默认的日志配置文件,启动命令如下:

shell
java -Dlogging.config=./logback-spring.xml -jar bootx-starter.jar

ELK日志收集

在项目中_config/elk目录里有对应的配置文件,包括ElasticSearchKibanadocker-compose.yml文件,以及filebeat收集日志相关的配置 filebeat启动脚本,注意:仅供参考

yaml
version: '3'
services:
  # 日志收集
  filebeat:
    image: docker.elastic.co/beats/filebeat:7.13.2
    restart: always
    volumes:
      # 挂载日志目录(需要能读取到系统生成的日志)到容器中
      - /data/logs:/data/logs
    # 启动命令
    command: filebeat /usr/share/filebeat/filebeat.yml

filebeat输出到ES配置

yaml
filebeat.inputs:
- type: log
  enabled: true
  paths:
    - /data/logs/bootx-platform/*.json
# 输出到es
output.elasticsearch:
  hosts: localhost:9200
  # 索引名
  index: "bootx-platform-logs"
# 设置
setup:
# 自定义索引的话必须设置
  template:
    name: "filebeat"
    pattern: "filebeat-*"
  ilm.enabled: false
# 处理配置
processors:
  - decode_json_fields:
      fields: ['message'] #要进行解析的字段
      target: "" #json内容解析到指定的字段,如果为空(“”),则解析到顶级结构下
  - drop_fields:
      fields: ["log","ecs","agent","input","@metadata","message"] #删除无用的字段

ES日志索引格式

json
{
  "mappings": {
      "properties":{
        "@timestamp":{"type":"date"},
        "log_time":{"type":"date"},
        "severity":{"type":"keyword"},
        "service":{"type":"keyword"},
        "trace":{"type":"keyword"},
        "class":{"type":"keyword"},
        "rest":{"type":"text"},
        "stack_trace":{"type":"text"},
        "thread_name":{"type":"keyword"},
        "host":{
          "properties": {
            "name": {"type": "keyword"}
          }
        }
      }
  }
}

plumelog(嵌入式)

提示

不需要额外安装日志中间件,可以满足普通的应用场合,通过在配置文件中添加相关配置即可使用,可以查看plumelog官方文档 说明了解更详细的使用说明

logback-spring.xml配置

xml
<?xml version="1.0" encoding="UTF-8"?>
<included>
    <appender name="plumelog" class="com.plumelog.lite.logback.appender.LiteAppender">
        <!-- 应用名称 -->
        <appName>${appname}</appName>
        <!-- 日志存储位置 -->
        <logPath>${logdir}/plumelog</logPath>
        <!-- 日志保留天数设置为0, 不使用系统的删除逻辑(默认的删除逻辑有问题, 是用 lucene 方式进行清除的, 会遗留非常多的文件夹) -->
        <keepDay>0</keepDay>
    </appender>
</included>

plumelog(lite模式)

提示

推荐普通的项目使用单独部署lite模式的PlumeLog,与应用分离开来,更方便进行维护,Lite模式性能也足够普通的项目使用,可以查看plumelog官方文档 说明了解更详细的使用说明

配置简单说明

shell
#值为4种 redis,kafka,rest,restServer,lite
#lite 简易模式启动不需要配置redis,es等
plumelog.model=redis
#lite模式下日志存储路径
plumelog.lite.log.path=/lucene

logback-spring.xml配置

xml
<!-- plumelog 方式日志收集 -->
<appender name="plumelog" class="com.plumelog.logback.appender.LiteAppender">
    <appName>${appname}</appName>
    <plumelogHost>127.0.0.1</plumelogHost>
</appender>
本文档内容版权属于济南易杯光年软件技术有限公司,保留所有权利