Mat*_*tej 4 logging elasticsearch fluentd docker kibana
我在docker容器中运行了本地服务器,该容器设置为使用fluentd作为日志驱动程序。我有docker compose文件,该文件在自己的容器中运行fluentd,nginx,elasticsearch和kibana。因此,fluentd从我的服务器中获取日志,并将其传递给elasticsearch并显示在Kibana上。
我的问题是,如何解析流利的日志(elasticsearch或kibana,如果不能流利的话)以创建新标签,以便我可以对它们进行排序并简化导航。
这是在Kibana中显示的当前日志。现在,我希望将此日志字符串“分解”为新标签。在这种情况下:
2017/01/04 13:26:56.574909 UTC (Example deployment.web) [INFO] [GET] /api/device/ 200 10.562379ms
Run Code Online (Sandbox Code Playgroud)
至
date: 2017/01/04
time: 13:26:56.574909 UTC
message: (Example deployment.web)
logType: [INFO]
other: [GET] /api/device/ 200 10.562379ms
Run Code Online (Sandbox Code Playgroud)
我的docker-compose.yml
version: "2"
services:
fluentd:
image: fluent/fluentd:latest
ports:
- "24224:24224"
volumes:
- ./fluentd/etc:/fluentd/etc
command: /fluentd/etc/start.sh
networks:
- lognet
elasticsearch:
image: elasticsearch
ports:
- "9200:9200"
- "9300:9300"
volumes:
- /usr/share/elasticsearch/data:/usr/share/elasticsearch/data
networks:
- lognet
kibana:
image: kibana
restart: always
ports:
- "5601:5601"
environment:
- ELASTICSEARCH_URL=http://localhost:9200
networks:
- lognet
nginx:
image: nginx
ports:
- "8084:80"
logging:
driver: fluentd
networks:
- lognet
networks:
lognet:
driver: bridge
Run Code Online (Sandbox Code Playgroud)
我的fluent.conf文件,不包含解析,只是简单的转发
<source>
type forward
</source>
<match *.*>
type elasticsearch
host elasticsearch
logstash_format true
flush_interval 10s
</match>
Run Code Online (Sandbox Code Playgroud)
我尝试使用regexp,在这里我尝试解析logType
<source>
@type forward
</source>
<match *.*>
type stdout
</match>
<filter docker.**>
@type parser
format /(?<logType>\[([^\)]+)\])/
key_name log
reserve_data false
</filter>
Run Code Online (Sandbox Code Playgroud)
我尝试了其他配置,但均未解析日志。
对于任何有类似问题的人,我都找到了适合我的解决方案。
在fluent.conf文件中,添加了新的过滤器标签。例如,如果我要创建一个称为“ 严重性”的新字段,第一步就是使用正则表达式对其进行记录。
示例是[DEBU]。
<filter *.*>
@type record_transformer
enable_ruby
<record>
severity ${record["log"].scan(/\[([^\)]+)\]/).last}
</record>
</filter>
Run Code Online (Sandbox Code Playgroud)
然后从原始消息中删除:
<filter *.*>
@type record_transformer
enable_ruby
<record>
log ${record["log"].gsub(/\[([^\)]+)\]/, '')}
</record>
</filter>
Run Code Online (Sandbox Code Playgroud)
主要部分是:
severity ${record["log"].scan(/\[([^\)]+)\]/).last}
Run Code Online (Sandbox Code Playgroud)
其中严重性是新字段的名称,record [“ log”]是原始日志字符串,在其中找到通过正则表达式生成的字符串并将其附加到新字段中。
log ${record["log"].gsub(/\[([^\)]+)\]/, '')}
Run Code Online (Sandbox Code Playgroud)
此命令修改字段日志,其中用空字符串替换正则表达式-已删除。
注意:顺序很重要,因为我们首先必须附加到新字段,然后从原始日志消息中删除字符串(如果需要)。
| 归档时间: |
|
| 查看次数: |
2927 次 |
| 最近记录: |