Filebeat采集Tomcat多行日志完整指南

  • A+
所属分类:系统文档

一、Tomcat日志特点分析

Tomcat的catalina.out日志包含以下典型特征:

  • 以日期时间开头(如15-Jul-2023 14:30:45.123
  • 堆栈跟踪信息以空格或at开头
  • 异常信息包含Caused by:前缀
  • 多线程日志交错输出

二、多行日志采集优化方案

1. 增强版Filebeat配置

tomcat_errlog-to-es.yaml

filebeat.inputs:
- type: log
  paths:
    - /oldboyedu/softwares/apache-tomcat-10.1.25/logs/catalina.out
  fields:
    log_type: "tomcat_catalina"
    environment: "production"
  multiline:
    type: pattern
    pattern: '^[0-9]{2}-[A-Za-z]{3}-[0-9]{4}|^[[:space:]]+(at|\.{3})|^Caused by:'
    negate: true
    match: after
    max_lines: 500
    timeout: 5s
  processors:
    - dissect:
        tokenizer: "%{ts} %{level} [%{thread}] %{class}: %{message}"
        field: "message"
        target_prefix: ""
        ignore_failure: true
    - timestamp:
        field: "ts"
        layouts:
          - "02-Jan-2006 15:04:05.000"
          - "02-Jan-2006 15:04:05"
        test:
          - "15-Jul-2023 14:30:45.123"
        ignore_failure: true

output.elasticsearch:
  hosts: 
    - "http://10.0.0.91:9200"
    - "http://10.0.0.92:9200"
    - "http://10.0.0.93:9200"
  index: "tomcat-catalina-%{+yyyy.MM.dd}"
  pipeline: "tomcat_catalina_pipeline"

setup.ilm.enabled: false
setup.template:
  name: "tomcat-catalina"
  pattern: "tomcat-catalina-*"
  overwrite: false
  settings:
    index.number_of_shards: 3
    index.number_of_replicas: 1

2. 关键配置解析

  1. 多行模式优化
    • 同时匹配日期开头和Java异常格式
    • 增加max_linestimeout防止内存溢出
  2. 日志解析增强
    • 使用dissect处理器提取日志组件
    • 精确解析Tomcat时间戳格式
  3. 错误处理
    • 对解析失败的情况进行容错处理

三、Elasticsearch预处理管道

PUT _ingest/pipeline/tomcat_catalina_pipeline
{
  "description": "Process Tomcat Catalina logs",
  "processors": [
    {
      "grok": {
        "field": "message",
        "patterns": [
          "%{TOMCAT_DATESTAMP:timestamp} %{LOGLEVEL:level} %{DATA:thread} %{JAVACLASS:class}]: %{GREEDYDATA:log_message}"
        ],
        "pattern_definitions": {
          "TOMCAT_DATESTAMP": "%{MONTHDAY}-%{MONTH}-%{YEAR} %{TIME}"
        }
      }
    },
    {
      "script": {
        "source": """
          if (ctx.log_message != null && ctx.log_message.contains('Exception')) {
            ctx.is_exception = true;
          }
        """
      }
    }
  ]
}

四、Kibana分析配置

1. 索引模式创建

  1. 创建tomcat-catalina-*索引模式
  2. 设置@timestamp为时间字段
  3. 为关键字段设置适当类型:
    • level - keyword
    • class - keyword
    • is_exception - boolean

2. 可视化建议

  1. 错误仪表板
    • 异常数量趋势图
    • 异常类型词云
    • 最近异常列表
  2. 性能仪表板
    • 启动时间统计
    • 资源警告监控
    • 线程状态分析

3. 告警规则示例

{
  "query": {
    "bool": {
      "must": [
        { "match": { "level": "SEVERE" } },
        { "range": { "@timestamp": { "gte": "now-15m" } } }
      ]
    }
  }
}

五、生产环境最佳实践

1. 日志轮转配置

# /etc/logrotate.d/tomcat
/oldboyedu/softwares/apache-tomcat-10.1.25/logs/catalina.out {
    daily
    rotate 30
    missingok
    compress
    delaycompress
    notifempty
    copytruncate
}

2. 性能优化参数

queue.mem:
  events: 8192
  flush.min_events: 2048
  flush.timeout: "10s"

output.elasticsearch:
  bulk_max_size: 1000
  worker: 6

3. 多实例采集方案

filebeat.inputs:
- type: log
  paths:
    - /app/tomcat-*/logs/catalina.out
  tags: ["tomcat-catalina"]
  fields_under_root: true

六、故障排查指南

1. 多行日志不匹配

检查步骤

# 测试正则表达式
grep -nE '^[0-9]{2}-[A-Za-z]{3}-[0-9]{4}' /oldboyedu/softwares/apache-tomcat-10.1.25/logs/catalina.out

# 查看Filebeat处理后的日志
filebeat export config --path.logs /tmp/filebeat-debug

2. 时间戳解析失败

解决方案

processors:
  - timestamp:
      field: "ts"
      layouts:
        - "02-Jan-2006 15:04:05.000"
        - "2006-01-02 15:04:05"
      ignore_failure: true

3. 内存占用过高

优化方向

  1. 降低queue.mem.events
  2. 增加multiline.timeout
  3. 限制multiline.max_lines

七、高级功能扩展

1. 日志采样分析

processors:
  - sample:
      when:
        contains:
          message: "WARNING"
      rate: 0.5

2. 自定义异常检测

{
  "script": {
    "source": """
      if (ctx.log_message != null) {
        if (ctx.log_message.contains('OutOfMemoryError')) {
          ctx.oom_error = true;
        }
        if (ctx.log_message.contains('Timeout')) {
          ctx.timeout_error = true;
        }
      }
    """
  }
}

总结

通过本方案,您已实现:

  1. 精准的Tomcat多行日志采集
  2. 结构化的日志解析处理
  3. 高效的异常检测机制
  4. 完整的监控分析链路

建议后续:

  1. 建立日志归档策略
  2. 实现异常自动分类
  3. 开发自定义分析插件
  4. 定期优化索引性能

FROM https://www.cnblogs.com/leojazz/p/18796377

  • 我的微信
  • 这是我的微信扫一扫
  • weinxin
  • 我的微信公众号
  • 我的微信公众号扫一扫
  • weinxin

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: