编码插件(Codec)

Codec 是 logstash 从 1.3.0 版开始新引入的概念(Codec 来自 Coder/decoder 两个单词的首字母缩写)。

在此之前,logstash 只支持纯文本形式输入,然后以过滤器处理它。但现在,我们可以在输入 期处理不同类型的数据,这全是因为有了 codec 设置。

所以,这里需要纠正之前的一个概念。Logstash 不只是一个input | filter | output 的数据流,而是一个 input | decode | filter | encode | output 的数据流!codec 就是用来 decode、encode 事件的。

codec 的引入,使得 logstash 可以更好更方便的与其他有自定义数据格式的运维产品共存,比如 graphite、fluent、netflow、collectd,以及使用 msgpack、json、edn 等通用数据格式的其他产品等。

事实上,我们在第一个 “hello world” 用例中就已经用过 codec 了 —— rubydebug 就是一种 codec!虽然它一般只会用在 stdout 插件中,作为配置测试或者调试的工具。

1,采用json编码

这里通过nginx日志作为案例测试学习使用。修改nginx配置文件,将日志以json格式存储:

  1. log_format json '{"@timestamp":"$time_iso8601",'
  2. '"@version":"1",'
  3. '"host":"$server_addr",'
  4. '"client":"$remote_addr",'
  5. '"size":$body_bytes_sent,'
  6. '"responsetime":$request_time,'
  7. '"domain":"$host",'
  8. '"url":"$uri",'
  9. '"status":"$status"}';
  10. access_log /logs/nginx/access.log json;

重启nginx使其生效。

设置测试使用的logstash配置文件webnginx.conf:

  1. input {
  2. file {
  3. path => "/logs/nginx/access.log"
  4. type => "nginx"
  5. start_position => "beginning"
  6. add_field => { "key"=>"value"}
  7. codec => "json"
  8. }
  9. }
  10. output {
  11. stdout{
  12. codec => rubydebug{ }
  13. }
  14. }

logstash加载启动测试:

  1. logstash -f webnginx.conf

访问测试地址,显示如下(这里我将www.elk.com定向到了nginx所在的虚拟机)

  1. {
  2. "type" => "nginx",
  3. "url" => "/app/panels/timepicker/module.html",
  4. "tags" => [],
  5. "path" => "/logs/nginx/access.log",
  6. "@timestamp" => 2017-02-13T17:02:47.000Z,
  7. "size" => 2397,
  8. "domain" => "www.elk.com",
  9. "@version" => "1",
  10. "host" => "192.168.1.104",
  11. "client" => "192.168.2.16",
  12. "responsetime" => 0.0,
  13. "key" => "value",
  14. "status" => "200"
  15. }

补充说明:
日志格式中统一记录为字符串格式(即都带上双引号 “),然后再在 logstash 中用 filter/mutate 插件来变更应该是数值类型的字符字段的值类型。

2,合并多行数据(Multiline)

有些时候,应用程序调试日志会包含非常丰富的内容,为一个事件打印出很多行内容。这种日志通常都很难通过命令行解析的方式做分析。 logstash 正为此准备好了 codec/multiline 插件。测试:

  1. input {
  2. stdin {
  3. codec => multiline {
  4. pattern => "^\["
  5. negate => true
  6. what => "previous"
  7. }
  8. }
  9. }
  10. output {
  11. stdout {
  12. codec => rubydebug{ }
  13. }
  14. }

加载:logstash -f multiline.conf
效果如下:

  1. hello world
  2. hello logstash
  3. hello multiline
  4. [
  5. {
  6. "@timestamp" => 2017-02-13T17:29:47.658Z,
  7. "@version" => "1",
  8. "host" => "0.0.0.0",
  9. "message" => "[\nhello world\nhello logstash\nhello multiline",
  10. "tags" => [
  11. [0] "multiline"
  12. ]
  13. }

这个插件的原理很简单,就是把当前行的数据添加到前面一行后面,直到新进的当前行匹配 ^\[ 正则为止。这个正则还可以用 grok 表达式。

参考文章:
http://kibana.logstash.es/content/logstash/plugins/codec/multiline.html

最新文章

  1. java中的数据结构(集合|容器)
  2. MSBUID相关(笔记)
  3. 从exchange2010上面删除特定主题或特定时间的邮件
  4. Android应用程序进程启动过程的源代码分析
  5. jsp中获取json字符串,并解析
  6. Spring MVC ajax:post/get 的具体实现
  7. javaweb中重定向和请求转发(response.sendRedirect()和request.getRequestDispatcher(rul).forward(request,response)))的区别
  8. 谈谈PBOC3.0中使用的国密SM2算法
  9. dmraid 用法
  10. Python-数据类型之字典
  11. SEED实验——Environment Variable and Set-UID Program实验报告
  12. python collection模块
  13. java项目(学习和研究)
  14. PHP(表单元素)
  15. PAT 1081 Rational Sum
  16. 菜鸟教程--AJAX
  17. tp3 链接生成二维码
  18. transformClassesWithJarMergingForDebug
  19. 【EF框架】EF DBFirst 快速生成数据库实体类 Database1.tt
  20. gh-ost安装

热门文章

  1. 查看显卡报错:NVIDIA-SMI has failed because it couldn't communicate with the NVIDIA driver. Make sure that the latest NVIDIA driver is installed and running.
  2. linux 测试2
  3. C++核心编程 2 引用
  4. AT3950 [AGC022E] Median Replace
  5. CF123E Maze(期望dp,树形dp,式子)
  6. Shell 编程 基础用法
  7. 初学python-day5 流程控制
  8. PHP伪协议与文件包含漏洞1
  9. OO第四单元及学期总结
  10. Noip模拟42 2021.8.17