gpt4 book ai didi

mongodb - Fluentd 不将解析后的日志发送到 Mongo

转载 作者:可可西里 更新时间:2023-11-01 10:13:03 25 4
gpt4 key购买 nike

我正在使用 Fluentd 解析日志并将解析后的日志存储在 MongoDB 中。

我的应用程序正在生成以下日志:

[2018-01-25 17:50:22] 192.168.10.1 GET http://localhost.com/mypage html 0 Mozilla/5.0 200 132

Fluentd 正在正确解析日志,但不是时间(我猜)。因为 MongoDB 无法存储解析后的内容。它甚至没有反射(reflect)在解析的日志中。下面是解析结果:

2018-01-25 17:50:22.000000000 +0000 request.main: {"ip-address":"192.168.10.1","request-method":"GET","request-url":"http://localhost.com/mypage","format":"html","request-size":"0","user-agent":"Mozilla/5.0","response-code":"200","response-duration":"132"}

但是,我没有看到这里解析的时间。并怀疑这种行为,fluent-plugin-Mongo 读取:

[warn]: #0 Since v0.8, invalid record detection will be removed because Mongo driver v2.x and the API spec don't provide it. You may lose invalid records, so you should not send such records to Mongo plugin

但是,当使用 fluentular 时, 它解析正确。这是我的尾部配置:

<source>
@type tail
path /home/app-logs/dev/my-app/%Y/%b/dev-main.log
tag request.main
time_format %Y-%m-%d %H:%M:%S
format /^\[(?<time>[^\]]*)\] (?<ip-address>[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*) (?<request-method>\w*) (?<request-url>[^ ]*) (?<format>[^ ]*) (?<request-size>\d*) (?<user-agent>[^ ]*) (?<response-code>\d*) (?<response-duration>\d*)$/
pos_file /tmp/fluentd--1516882649.pos
</source>

mongo插件配置如下:

<match request.*>
@type mongo
host 127.0.0.1
port 27017
user foo
password bar
database my-app
collection requests
capped
capped_size 100m
</match>

感谢任何帮助。谢谢!

最佳答案

我使用 Fluentd 将 Nginx 日志传递给 MongoDB,但我使用 Nginx 的配置文件创建了自定义日志格式。我让 Nginx 把它的日志写成 json 格式,这样我更容易处理。我认为当您使用 Fluentd 时,这是一种更好的方法。如果您可以将日志格式更改为 json,也许您可​​以尝试这些设置:

<source>
@type tail
path /path/json/server_nginx.access.log.json #...or where you placed your Apache access log
pos_file /path2/server_nginx.access.log.json.pos # This is where you record file position
tag nginx.access #fluentd tag!
format json
</source>

<match **>
@type mongo
database logs #(required)
collection foo #(optional; default="untagged")
host ***.***.***.*** #(optional; default="localhost")
port 27017 #(optional; default=27017)
user notmyrealusername
password notmyrealpassword
</match>

我不确定你的应用是否与 nginx 相关,但这些是我的 nginx 日志格式设置:

log_format logstash_json '{ "@timestamp": "$time_iso8601", '
'"@fields": { '
'"remote_addr": "$remote_addr", '
'"request_time": "$request_time", '
'"request": "$request", '
'"http_referrer": "$http_referer", '
'"http_host": "$host", '
'"http_user_agent": "$http_user_agent" } }';

关于mongodb - Fluentd 不将解析后的日志发送到 Mongo,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48443407/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com