关于laravel:Laravel-使用-Elasticsearch-作为日志存储

简介

在理论开发中,咱们发现在 Debug 的时候常常须要查问日志。而传统的形式是须要 SSH 到生产环境,而后应用 cat,tail 和 grep 等命令查问日志,且无奈进行日志的统计和剖析,深度开掘这些日志的价值。

本片文章的侧重点在于优雅的让 Laravel 间接将日志写入 Elasticsearch,当然你也能够抉择应用Filebeat 采集 Laravel 的本地日志。

装置Elasticsearch依赖包

composer require elasticsearch/elasticsearch

.env环境变量配置

# 批改日志存在通道
LOG_CHANNEL=elasticsearch

# 增加 elasticsearch 日志存储配置
ELASTIC_HOST=http://elasticsearch:9200    # elasticsearch 服务地址
ELASTIC_LOGS_INDEX=shopem-store-es-logs    # elasticsearch 日志存储索引名

增加日志通道

config/logging.php 文件里的 channels 里增加如下配置

'elasticsearch' => [
    'driver'         => 'monolog',
    'level'          => 'debug',
    'handler'        => \Monolog\Handler\ElasticsearchHandler::class,
    'formatter'      => \Monolog\Formatter\ElasticsearchFormatter::class,
    'formatter_with' => [
        'index' => env('ELASTIC_LOGS_INDEX'),
        'type'  => '_doc',
    ],
    'handler_with'   => [
        'client' => \Elasticsearch\ClientBuilder::create()->setHosts([env('ELASTIC_HOST')])->build(),
    ],
],

在Kibana中查看日志

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理