本文内容

  • 测试数据
  • 字段属性
  • 按多行解析运行时日志
  • 把多行日志解析到字段
  • 参考资料

在处理日志时,除了访问日志外,还要处理运行时日志,该日志大都用程序写的,比如 log4j。运行时日志跟访问日志最大的不同是,运行时日志是多行,也就是说,连续的多行才能表达一个意思。

本文主要说明,如何用 multiline 出来运行日志。

如果能按多行处理,那么把他们拆分到字段就很容易了。

迁移到:http://www.bdata-cap.com/newsinfo/1712113.html

测试数据


[16-04-12 03:40:01 DEBUG] model.MappingNode:- ['/store/shopclass'] matched over.

[16-04-12 03:40:02 DEBUG] impl.JdbcEntityInserter:- from product_category product_category

where product_category.PARENT_ID is null and product_category.STATUS = ? and product_category.DEALER_ID is null

order by product_category.ORDERS asc

[16-04-12 03:40:03 DEBUG] model.MappingNode:- ['/store/shopclass'] matched over.

[16-04-12 03:40:04 DEBUG] model.MappingNode:- ['/store/shopclass'] matched over.

[16-04-12 03:40:05 DEBUG] impl.JdbcEntityInserter:- from product_category product_category

where product_category.PARENT_ID is null and product_category.STATUS = ? and product_category.DEALER_ID is null

order by product_category.ORDERS desc

[16-04-12 03:40:06 DEBUG] impl.JdbcEntityInserter:- from product_category product_category

where product_category.PARENT_ID is null and product_category.STATUS = ? and product_category.DEALER_ID is null

order by product_category.ORDERS asc

[16-04-12 03:40:07 DEBUG] model.MappingNode:- ['/store/shopclass'] matched over.

测试是在7秒内发生的(当然是假数据)。可以看到,第二、五、六秒的日志是多行的,有条SQL语句。其他是单行的。

字段属性


对 multiline 插件来说,有三个设置比较重要:negate、pattern 和 what。

negate

  • 类型是 boolean

  • 默认为 false

否定正则表达式(如果没有匹配的话)。

pattern

  • 必须设置

  • 类型为 string

  • 没有默认值

要匹配的正则表达式。

what

  • 必须设置

  • 可以为 previous 或 next

  • 没有默认值

如果正则表达式匹配了,那么该事件是属于下一个或是前一个事件?

按多行解析运行时日志


示例1:若配置文件如下所示,

input {

        file{

                path=>"/usr/local/elk/logstash/logs/c.out"

                type=>"runtimelog"

                codec=> multiline {

                        pattern => "^\["

                        negate => true

                        what => "previous"

                }

                start_position=>"beginning"

                sincedb_path=>"/usr/local/elk/logstash/sincedb-access"

                ignore_older=>0

        }

}

output{

        stdout{

                codec=>rubydebug

        }

}

说明:匹配以“[”开头的行,如果不是,那肯定是属于前一行的。

解析结果如下所示,能解析出6个JSON:

{

    "@timestamp" => "2016-06-01T04:37:43.147Z",

       "message" => "[16-04-12 03:40:01 DEBUG] model.MappingNode:- ['/store/shopclass'] matched over.",

      "@version" => "1",

          "path" => "/usr/local/elk/logstash/logs/c.out",

          "host" => "vcyber",

          "type" => "runtimelog"

}

{

    "@timestamp" => "2016-06-01T04:37:43.152Z",

       "message" => "[16-04-12 03:40:02 DEBUG] impl.JdbcEntityInserter:- from product_category product_category\nwhere product_category.PARENT_ID is null and product_category.STATUS = ? and product_category.DEALER_ID is null\norder by product_category.ORDERS asc",

      "@version" => "1",

          "tags" => [

        [0] "multiline"

    ],

          "path" => "/usr/local/elk/logstash/logs/c.out",

          "host" => "vcyber",

          "type" => "runtimelog"

}

{

    "@timestamp" => "2016-06-01T04:37:43.152Z",

       "message" => "[16-04-12 03:40:03 DEBUG] model.MappingNode:- ['/store/shopclass'] matched over.",

      "@version" => "1",

          "path" => "/usr/local/elk/logstash/logs/c.out",

          "host" => "vcyber",

          "type" => "runtimelog"

}

{

    "@timestamp" => "2016-06-01T04:37:43.155Z",

       "message" => "[16-04-12 03:40:04 DEBUG] model.MappingNode:- ['/store/shopclass'] matched over.",

      "@version" => "1",

          "path" => "/usr/local/elk/logstash/logs/c.out",

          "host" => "vcyber",

          "type" => "runtimelog"

}

{

    "@timestamp" => "2016-06-01T04:37:43.157Z",

       "message" => "[16-04-12 03:40:05 DEBUG] impl.JdbcEntityInserter:- from product_category product_category\nwhere product_category.PARENT_ID is null and product_category.STATUS = ? and product_category.DEALER_ID is null\norder by product_category.ORDERS desc",

      "@version" => "1",

          "tags" => [

        [0] "multiline"

    ],

          "path" => "/usr/local/elk/logstash/logs/c.out",

          "host" => "vcyber",

          "type" => "runtimelog"

}

{

    "@timestamp" => "2016-06-01T04:37:43.159Z",

       "message" => "[16-04-12 03:40:06 DEBUG] impl.JdbcEntityInserter:- from product_category product_category\nwhere product_category.PARENT_ID is null and product_category.STATUS = ? and product_category.DEALER_ID is null\norder by product_category.ORDERS asc",

      "@version" => "1",

          "tags" => [

        [0] "multiline"

    ],

          "path" => "/usr/local/elk/logstash/logs/c.out",

          "host" => "vcyber",

          "type" => "runtimelog"

}

解析时,最后一行日志,不会解析。只有当再追加一条日志时,才会解析最后一条日志。

示例2:若将配置文件修改为,

input {

        file{

                path=>"/usr/local/elk/logstash/logs/c.out"

                type=>"runtimelog"

                codec=>multiline  {

                        pattern => "^\["

                        negate => true

                        what => "next"

                }

                start_position=>"beginning"

                sincedb_path=>"/usr/local/elk/logstash/sincedb-access"

                ignore_older=>0

        }

}

output{

        stdout{

                codec=>rubydebug

        }

}

解析结果为,能解析出7个JSON:

{

    "@timestamp" => "2016-06-01T04:40:43.232Z",

       "message" => "[16-04-12 03:40:01 DEBUG] model.MappingNode:- ['/store/shopclass'] matched over.",

      "@version" => "1",

          "path" => "/usr/local/elk/logstash/logs/c.out",

          "host" => "vcyber",

          "type" => "runtimelog"

}

{

    "@timestamp" => "2016-06-01T04:40:43.237Z",

       "message" => "[16-04-12 03:40:02 DEBUG] impl.JdbcEntityInserter:- from product_category product_category",

      "@version" => "1",

          "path" => "/usr/local/elk/logstash/logs/c.out",

          "host" => "vcyber",

          "type" => "runtimelog"

}

{

    "@timestamp" => "2016-06-01T04:40:43.238Z",

       "message" => "where product_category.PARENT_ID is null and product_category.STATUS = ? and product_category.DEALER_ID is null\norder by product_category.ORDERS asc\n[16-04-12 03:40:03 DEBUG] model.MappingNode:- ['/store/shopclass'] matched over.",

      "@version" => "1",

          "tags" => [

        [0] "multiline"

    ],

          "path" => "/usr/local/elk/logstash/logs/c.out",

          "host" => "vcyber",

          "type" => "runtimelog"

}

{

    "@timestamp" => "2016-06-01T04:40:43.239Z",

       "message" => "[16-04-12 03:40:04 DEBUG] model.MappingNode:- ['/store/shopclass'] matched over.",

      "@version" => "1",

          "path" => "/usr/local/elk/logstash/logs/c.out",

          "host" => "vcyber",

          "type" => "runtimelog"

}

{

    "@timestamp" => "2016-06-01T04:40:43.244Z",

       "message" => "[16-04-12 03:40:05 DEBUG] impl.JdbcEntityInserter:- from product_category product_category",

      "@version" => "1",

          "path" => "/usr/local/elk/logstash/logs/c.out",

          "host" => "vcyber",

          "type" => "runtimelog"

}

{

    "@timestamp" => "2016-06-01T04:40:43.245Z",

       "message" => "where product_category.PARENT_ID is null and product_category.STATUS = ? and product_category.DEALER_ID is null\norder by product_category.ORDERS desc\n[16-04-12 03:40:06 DEBUG] impl.JdbcEntityInserter:- from product_category product_category",

      "@version" => "1",

          "tags" => [

        [0] "multiline"

    ],

          "path" => "/usr/local/elk/logstash/logs/c.out",

          "host" => "vcyber",

          "type" => "runtimelog"

}

{

    "@timestamp" => "2016-06-01T04:40:43.249Z",

       "message" => "where product_category.PARENT_ID is null and product_category.STATUS = ? and product_category.DEALER_ID is null\norder by product_category.ORDERS asc\n[16-04-12 03:40:07 DEBUG] model.MappingNode:- ['/store/shopclass'] matched over.",

      "@version" => "1",

          "tags" => [

        [0] "multiline"

    ],

          "path" => "/usr/local/elk/logstash/logs/c.out",

          "host" => "vcyber",

          "type" => "runtimelog"

}

示例3:若将配置文件修改为,

codec=>multiline  {

        pattern => "^\["

        negate => false

        what => "previous"

}

则解析结果为:

{

    "@timestamp" => "2016-06-01T05:38:50.853Z",

       "message" => "[16-04-12 03:40:01 DEBUG] model.MappingNode:- ['/store/shopclass'] matched over.\n[16-04-12 03:40:02 DEBUG] impl.JdbcEntityInserter:- from product_category product_category",

      "@version" => "1",

          "tags" => [

        [0] "multiline"

    ],

          "path" => "/usr/local/elk/logstash/logs/c.out",

          "host" => "vcyber",

          "type" => "runtimelog"

}

{

    "@timestamp" => "2016-06-01T05:38:50.856Z",

       "message" => "where product_category.PARENT_ID is null and product_category.STATUS = ? and product_category.DEALER_ID is null",

      "@version" => "1",

          "path" => "/usr/local/elk/logstash/logs/c.out",

          "host" => "vcyber",

          "type" => "runtimelog"

}

{

    "@timestamp" => "2016-06-01T05:38:50.858Z",

       "message" => "order by product_category.ORDERS asc\n[16-04-12 03:40:03 DEBUG] model.MappingNode:- ['/store/shopclass'] matched over.\n[16-04-12 03:40:04 DEBUG] model.MappingNode:- ['/store/shopclass'] matched over.\n[16-04-12 03:40:05 DEBUG] impl.JdbcEntityInserter:- from product_category product_category",

      "@version" => "1",

          "tags" => [

        [0] "multiline"

    ],

          "path" => "/usr/local/elk/logstash/logs/c.out",

          "host" => "vcyber",

          "type" => "runtimelog"

}

{

    "@timestamp" => "2016-06-01T05:38:50.860Z",

       "message" => "where product_category.PARENT_ID is null and product_category.STATUS = ? and product_category.DEALER_ID is null",

      "@version" => "1",

          "path" => "/usr/local/elk/logstash/logs/c.out",

          "host" => "vcyber",

          "type" => "runtimelog"

}

{

    "@timestamp" => "2016-06-01T05:38:50.861Z",

       "message" => "order by product_category.ORDERS desc\n[16-04-12 03:40:06 DEBUG] impl.JdbcEntityInserter:- from product_category product_category",

      "@version" => "1",

          "tags" => [

        [0] "multiline"

    ],

          "path" => "/usr/local/elk/logstash/logs/c.out",

          "host" => "vcyber",

          "type" => "runtimelog"

}

{

    "@timestamp" => "2016-06-01T05:38:50.863Z",

       "message" => "where product_category.PARENT_ID is null and product_category.STATUS = ? and product_category.DEALER_ID is null",

      "@version" => "1",

          "path" => "/usr/local/elk/logstash/logs/c.out",

          "host" => "vcyber",

          "type" => "runtimelog"

}

把多行日志解析到字段


配置文件如下所示:

input {

        file{

                path=>"/usr/local/elk/logstash/logs/c.out"

                type=>"runtimelog"

                codec=>multiline  {

                        pattern => "^\["

                        negate => true

                        what => "previous"

                }

                start_position=>"beginning"

                sincedb_path=>"/usr/local/elk/logstash/sincedb-access"

                ignore_older=>0

        }

}

filter {

        grok {

                match=>["message","\[%{TIMESTAMP_ISO8601:timestamp} %{LOGLEVEL:level}\] %{GREEDYDATA:msg}"]

        }

}

output{

        stdout{

                codec=>rubydebug

        }

}

解析后结果:

{

    "@timestamp" => "2016-06-01T06:33:26.426Z",

       "message" => "[16-04-12 03:40:01 DEBUG] model.MappingNode:- ['/store/shopclass'] matched over.",

      "@version" => "1",

          "path" => "/usr/local/elk/logstash/logs/c.out",

          "host" => "vcyber",

          "type" => "runtimelog",

     "timestamp" => "16-04-12 03:40:01",

         "level" => "DEBUG",

           "msg" => "model.MappingNode:- ['/store/shopclass'] matched over."

}

{

    "@timestamp" => "2016-06-01T06:33:26.485Z",

       "message" => "[16-04-12 03:40:02 DEBUG] impl.JdbcEntityInserter:- from product_category product_category\nwhere product_category.PARENT_ID is null and product_category.STATUS = ? and product_category.DEALER_ID is null\norder by product_category.ORDERS asc",

      "@version" => "1",

          "tags" => [

        [0] "multiline"

    ],

          "path" => "/usr/local/elk/logstash/logs/c.out",

          "host" => "vcyber",

          "type" => "runtimelog",

     "timestamp" => "16-04-12 03:40:02",

         "level" => "DEBUG",

           "msg" => "impl.JdbcEntityInserter:- from product_category product_category\nwhere product_category.PARENT_ID is null and product_category.STATUS = ? and product_category.DEALER_ID is null\norder by product_category.ORDERS asc"

}

{

    "@timestamp" => "2016-06-01T06:33:26.491Z",

       "message" => "[16-04-12 03:40:03 DEBUG] model.MappingNode:- ['/store/shopclass'] matched over.",

      "@version" => "1",

          "path" => "/usr/local/elk/logstash/logs/c.out",

          "host" => "vcyber",

          "type" => "runtimelog",

     "timestamp" => "16-04-12 03:40:03",

         "level" => "DEBUG",

           "msg" => "model.MappingNode:- ['/store/shopclass'] matched over."

}

{

    "@timestamp" => "2016-06-01T06:33:26.492Z",

       "message" => "[16-04-12 03:40:04 DEBUG] model.MappingNode:- ['/store/shopclass'] matched over.",

      "@version" => "1",

          "path" => "/usr/local/elk/logstash/logs/c.out",

          "host" => "vcyber",

          "type" => "runtimelog",

     "timestamp" => "16-04-12 03:40:04",

         "level" => "DEBUG",

           "msg" => "model.MappingNode:- ['/store/shopclass'] matched over."

}

{

    "@timestamp" => "2016-06-01T06:33:26.494Z",

       "message" => "[16-04-12 03:40:05 DEBUG] impl.JdbcEntityInserter:- from product_category product_category\nwhere product_category.PARENT_ID is null and product_category.STATUS = ? and product_category.DEALER_ID is null\norder by product_category.ORDERS desc",

      "@version" => "1",

          "tags" => [

        [0] "multiline"

    ],

          "path" => "/usr/local/elk/logstash/logs/c.out",

          "host" => "vcyber",

          "type" => "runtimelog",

     "timestamp" => "16-04-12 03:40:05",

         "level" => "DEBUG",

           "msg" => "impl.JdbcEntityInserter:- from product_category product_category\nwhere product_category.PARENT_ID is null and product_category.STATUS = ? and product_category.DEALER_ID is null\norder by product_category.ORDERS desc"

}

{

    "@timestamp" => "2016-06-01T06:33:26.495Z",

       "message" => "[16-04-12 03:40:06 DEBUG] impl.JdbcEntityInserter:- from product_category product_category\nwhere product_category.PARENT_ID is null and product_category.STATUS = ? and product_category.DEALER_ID is null\norder by product_category.ORDERS asc",

      "@version" => "1",

          "tags" => [

        [0] "multiline"

    ],

          "path" => "/usr/local/elk/logstash/logs/c.out",

          "host" => "vcyber",

          "type" => "runtimelog",

     "timestamp" => "16-04-12 03:40:06",

         "level" => "DEBUG",

           "msg" => "impl.JdbcEntityInserter:- from product_category product_category\nwhere product_category.PARENT_ID is null and product_category.STATUS = ? and product_category.DEALER_ID is null\norder by product_category.ORDERS asc"

}

参考资料


Logstash——multiline 插件,匹配多行日志的更多相关文章

  1. logstash之multiline插件,匹配多行日志

    在外理日志时,除了访问日志外,还要处理运行时日志,该日志大都用程序写的,比如log4j.运行时日志跟访问日志最大的不同是,运行时日志是多行,也就是说,连续的多行才能表达一个意思. 在filter中,加 ...

  2. Logstash——multiline 插件,匹配多行日志

    本文内容 测试数据 字段属性 按多行解析运行时日志 把多行日志解析到字段 参考资料 在处理日志时,除了访问日志外,还要处理运行时日志,该日志大都用程序写的,比如 log4j.运行时日志跟访问日志最大的 ...

  3. Logstash-安装logstash-filter-multiline插件(解决logstash匹配多行日志)

    ELK-logstash在搬运日志的时候会出现多行日志,普通的搬运会造成保存到ES中日志一条一条的保存,很丑,而且不方便读取,logstash-filter-multiline可以解决该问题. 接下来 ...

  4. logstash匹配多行日志

    在工作中,遇到一个问题就是日志的处理,首选的方案就是ELFK(filebeat+logstash+es+kibana) 因为之前使用过logstash采集日志的时候,非常的消耗系统的资源,所以这里我选 ...

  5. Python正则处理多行日志一例

    正则表达式基础知识请参阅<正则表达式基础知识>,本文使用正则表达式来匹配多行日志并从中解析出相应的信息. 假设现在有这样的SQL日志: SELECT * FROM open_app WHE ...

  6. Python正则处理多行日志一例(可配置化)

    正则表达式基础知识请参阅<正则表达式基础知识>,本文使用正则表达式来匹配多行日志并从中解析出相应的信息. 假设现在有这样的SQL日志: SELECT * FROM open_app WHE ...

  7. 写给大忙人的ELK最新版6.2.4学习笔记-Logstash和Filebeat解析(java异常堆栈下多行日志配置支持)

    接前一篇CentOS 7下最新版(6.2.4)ELK+Filebeat+Log4j日志集成环境搭建完整指南,继续对ELK. logstash官方最新文档https://www.elastic.co/g ...

  8. ELK学习笔记之Logstash和Filebeat解析对java异常堆栈下多行日志配置支持

    0x00 概述 logstash官方最新文档.假设有几十台服务器,每台服务器要监控系统日志syslog.tomcat日志.nginx日志.mysql日志等等,监控OOM.内存低下进程被kill.ngi ...

  9. logstash 安装插件multiline

    一.安装multiline 在使用elk 传输记录 java 日志时,如下 一个java的报错 在elk中会按每一行 产生多条记录,不方便查阅 这里修改配置文件 使用  multiline   插件 ...

随机推荐

  1. 阅读高翔的RGBD-SLAM博文笔记

    目录 高翔的RGBD-SLAM笔记 前端VO: 后端优化 高翔的RGBD-SLAM笔记 RGBD相机的特点: 使用RGBD相机中的深度这一维信息,以及相机的针孔成像模型,相机的内参,可以将二维点恢复成 ...

  2. FCOS及其和Faster R-CNN的区别

    RetinaNet,SSD,YOLOv3,Faster R-CNN等都是Anchor-based的检测器,即需要预定义的Anchor boxes来进行训练.FCOS是一种Anchor-free和Pro ...

  3. 错误: 找不到或无法加载主类 com.leyou.LeyouItemApplication Process finished with exit code 1

    在IDEA的使用过程中,经常断掉服务或者重启服务,最近断掉服务重启时突然遇到了一个启动报错: 错误:找不到或无法加载主类 猜测:1,未能成功编译: 尝试:菜单--->Build--->Re ...

  4. nginx geoip_module 地域信息读取

    1.安装geoip yum -y install nginx-module-geoip 2.安装完成后在cd /etc/nginx/modules/ 能看到安装的模块 # cd /etc/nginx/ ...

  5. AutoResetEvent介绍及使用场景

    AutoResetEvent 允许线程通过发信号互相通信.通常,此通信涉及线程需要独占访问的资源. 线程通过调用 AutoResetEvent 上的 WaitOne 来等待信号.如果 AutoRese ...

  6. requests--文件上传,文件下载

    文件上传 在做接口自动化的时候,有时需要上传文件,比如更改头像等等,在request里,通过files参数来上传 import requests base_url = 'http://httpbin. ...

  7. map的基本操作

    向map添加元素: 因为map是不允许出现重复关键字的,所以如果重复插入键相同的元素后面的元素是不会插入成功的,下面是一个验证程序: #include<iostream> #include ...

  8. 【CFGym102059G】Fascination Street(思维DP)

    点此看题面 大致题意: 有\(n\)个路灯,每个路灯有一定的建造费用,且建成后可照亮自身及周围距离为\(1\)的两个格子.你可以交换\(k\)次两个路灯的建造费用,求照亮所有格子的最小费用. 题意转换 ...

  9. [LeetCode] 419. Battleships in a Board 平板上的战船

    Given an 2D board, count how many battleships are in it. The battleships are represented with 'X's, ...

  10. 基于Django的Rest Framework框架的序列化组件

    本文目录 一 Django自带序列化组件 二 rest-framework序列化之Serializer 三 rest-framework序列化之ModelSerializer 四 生成hypermed ...