博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ELK学习笔记-LogStash读取json日志分类型建立索引
阅读量:5315 次
发布时间:2019-06-14

本文共 2195 字,大约阅读时间需要 7 分钟。

测试目的是,用ELK处理在业务中用户定义的json log日志,则试PHP脚本如下:

 

'login', 'user_id' => rand(1000,3000), 'user_name' => "name_".rand(1,3000 ), 'level' => 1, 'register_time' => time(), ); $str = json_encode( $reg ); file_put_contents( "testlog" , $str."\n" , FILE_APPEND ); $reg = array( 'method' => 'register', 'user_id' => rand(1000,3000), 'user_name' => "name_".rand(1,3000 ), 'level' => rand(1,30), 'login_time' => time(), ); $str = json_encode( $reg ); file_put_contents( "testlog" , $str."\n" , FILE_APPEND );}

循环生成注册log和登录log保存到testlog文件中,结果如下:

{"method":"register","user_id":2933,"user_name":"name_91","level":27,"login_time":1470179550}

{"method":"login","user_id":1247,"user_name":"name_979","level":1,"register_time":1470179550}
{"method":"register","user_id":2896,"user_name":"name_1972","level":17,"login_time":1470179550}
{"method":"login","user_id":2411,"user_name":"name_2719","level":1,"register_time":1470179550}
{"method":"register","user_id":1588,"user_name":"name_1484","level":4,"login_time":1470179550}
{"method":"login","user_id":2507,"user_name":"name_1190","level":1,"register_time":1470179550}
{"method":"register","user_id":2382,"user_name":"name_234","level":21,"login_time":1470179550}
{"method":"login","user_id":1208,"user_name":"name_443","level":1,"register_time":1470179550}
{"method":"register","user_id":1331,"user_name":"name_1297","level":3,"login_time":1470179550}
{"method":"login","user_id":2809,"user_name":"name_743","level":1,"register_time":1470179550}

 

logstash目录下建立配置文件

vim config/json.conf

input {    file {        path => "/home/bona/logstash-2.3.4/testlog"        start_position => "beginning"        codec => "json"    }}output {   elasticsearch {        hosts => ["192.168.68.135:9200"]        index => "data_%{method}"   }}

重点是index中,%{method} 来匹配log中的method字段.

以上log就会分别建立data_login   data_register两个索引, 要注意的是索引名称必须全部小写

ES中已经成功以method建立了索引

 

 

参考资料:

http://udn.yyuap.com/doc/logstash-best-practice-cn/output/elasticsearch.html

转载于:https://www.cnblogs.com/lchb/articles/5731838.html

你可能感兴趣的文章
MySQL知识总结(二)基本语句总结
查看>>
SSM框架整合
查看>>
AMD and CMD are dead之KMD.js依赖可视化工具发布
查看>>
第三课 Makefile文件的制作(上)
查看>>
SQL Azure Reporting CTP
查看>>
Leetcode400Nth Digit第N个数字
查看>>
JavaScript数组迭代方法(图解)
查看>>
ycsb-命令及参数-与生成的负载类型相关
查看>>
扒开系统调用的三层皮(下)
查看>>
子类访问父类和方法覆写
查看>>
在Activity不可见时暂停WebView的语音播放,可见时继续播放之前的语音
查看>>
Dubbo的使用及原理浅析
查看>>
【POJ 2240】Arbitrage
查看>>
C#薪水和前途
查看>>
使用 Apache Pig 处理数据5
查看>>
Python中函数的参数传递与可变长参数
查看>>
HSV色彩空间
查看>>
[转] ArcEngine 产生专题图
查看>>
大数相乘
查看>>
16进制可逆加密算法
查看>>