6

Laravel Kafka Logger

使用扩展包 laravel-kafka-logger 将 Laravel 的日志推到 Kafka,然后由 ELK 消费、存储、呈现。

要求

依赖要求
php-rdkafka>=4.0.0

依赖安装

1.安装 rdkafka

git clone --depth 1 https://github.com/edenhill/librdkafka.git /tmp/librdkafka && cd /tmp/librdkafka && ./configure && make -j$(nproc) && make install && rm -rf /tmp/librdkafka
pecl install rdkafka

2.安装 laravel-kafka-logger

# Laravel 5.x
composer require "hhxsv5/laravel-kafka-logger:~1.0.0"
# Laravel 6.x & 7.x
composer require "hhxsv5/laravel-kafka-logger:~2.0.0"

开始使用

1.修改配置文件 config/logging.php

return [
    'channels' => [
        // ...
        'kafka' => Hhxsv5\LKL\KafkaLogger::getDefinition(['topic' => env('LOG_KAFKA_TOPIC', 'laravel-logs')]),
    ],
];

2.修改 .env

LOG_CHANNEL=kafka
LOG_KAFKA_BROKER_LIST=kafka:9092
LOG_KAFKA_TOPIC=laravel-logs

3.好了,开始记日志

Log::info('用户登录', ['uid-hhxsv5']);

hhxsv5
709 声望36 粉丝

Think Much, Code Less.