处理程序
ElasticsearchLogstashHandler
此处理程序直接处理 Elasticsearch 的 HTTP 接口。这意味着如果 Elasticsearch 需要时间响应,它会减慢您的应用程序的速度。即使所有 HTTP 调用都是异步完成的。
要使用它,请将其声明为服务
1 2 3 4 5 6 7 8 9 10 11 12 13
# config/services.yaml
services:
Symfony\Bridge\Monolog\Handler\ElasticsearchLogstashHandler: ~
# optionally, configure the handler using the constructor arguments (shown values are default)
Symfony\Bridge\Monolog\Handler\ElasticsearchLogstashHandler:
arguments:
$endpoint: "http://127.0.0.1:9200"
$index: "monolog"
$client: null
$level: !php/enum Monolog\Level::Debug
$bubble: true
$elasticsearchVersion: '1.0.0'
然后在 Monolog 配置中引用它。
在开发环境中,保持默认配置即可:对于每个日志,都会发出一个 HTTP 请求,将日志推送到 Elasticsearch
1 2 3 4 5 6
# config/packages/prod/monolog.yaml
monolog:
handlers:
es:
type: service
id: Symfony\Bridge\Monolog\Handler\ElasticsearchLogstashHandler
在生产环境中,强烈建议将此处理程序包装在具有缓冲功能的处理程序中(例如 FingersCrossedHandler 或 BufferHandler),以便仅使用批量推送调用 Elasticsearch 一次。为了获得更好的性能和容错能力,建议使用合适的 ELK 栈。
1 2 3 4 5 6 7 8 9 10
# config/packages/prod/monolog.yaml
monolog:
handlers:
main:
type: fingers_crossed
handler: es
es:
type: service
id: Symfony\Bridge\Monolog\Handler\ElasticsearchLogstashHandler
这项工作,包括代码示例,根据 Creative Commons BY-SA 3.0 许可证获得许可。