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

10次阅读

共计 809 个字符,预计需要花费 3 分钟才能阅读完成。

简介

在理论开发中,咱们发现在 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'        => MonologHandlerElasticsearchHandler::class,
    'formatter'      => MonologFormatterElasticsearchFormatter::class,
    'formatter_with' => ['index' => env('ELASTIC_LOGS_INDEX'),
        'type'  => '_doc',
    ],
    'handler_with'   => ['client' => ElasticsearchClientBuilder::create()->setHosts([env('ELASTIC_HOST')])->build(),],
],

正文完
 0