首页
高清壁纸
统计
留言
推荐
Json工具
Search
1
通过iframe嵌套时,被302重定向怎么办?
2,432 阅读
2
关于 MySQL Binlog 权限
1,132 阅读
3
Windows安装PECL
1,046 阅读
4
JS常用监听事件【监听键盘、触控、鼠标、多媒体、打印、剪贴板、表单等事件】
1,027 阅读
5
json在线转换Swagger/OpenAPI文档的注释文本格式
792 阅读
抽奖系统
tony爱编程
收藏分享
经验总结
其它杂类
常用工具
登录
Search
标签搜索
抽奖系统
html+JS
抽奖HTA
js
MySQL
HTA版
源码
hta属性
ecshop
Meta
拖动
javascript
MySQL函数
git提交慢
提交卡
laravel-admin
FSO
redis
文件操作
mysql备份
Tony哥
累计撰写
55
篇文章
累计收到
11
条评论
首页
栏目
抽奖系统
tony爱编程
收藏分享
经验总结
其它杂类
常用工具
页面
高清壁纸
统计
留言
推荐
Json工具
搜索到
1
篇与
的结果
2023-02-24
laravel或lumen将日志上报至kafka
lumen或laravel 将日志上报至kafka大家都清楚 Lumen 算是一个精简版的 Laravel,针对速度优化了框架的加载方式,多余的功能需要自行开启。那么在精简版中日志的上报配置方面lumen也做了精简,本文中将分别介绍这两者日志上报的基本方法,咱们主要还是依赖hhxsv5/laravel-kafka-logger,下面是方法和步骤(Laravel 和 lumen的安装咱们就略过了)咱们先安装php的rdkafka扩展要求依赖要求php-rdkafka>=4.0.0安装方法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安装 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"lavael 上配置修改配置文件 config/logging.phpreturn [ 'channels' => [ //..., 'kafka' => Hhxsv5\LKL\KafkaLogger::getDefinition(['topic' => env('LOG_KAFKA_TOPIC', 'laravel-logs')]), ], ];lumen 上配置在app/Providers目录新增日志扩展类LogServiceProvider.php<?php namespace App\Providers; use Hhxsv5\LKL\KafkaLogHandler; use Illuminate\Support\ServiceProvider; use RdKafka\Conf; use Monolog\Logger; class LogServiceProvider extends ServiceProvider { /** * Configure logging on boot. * * @return void */ public function boot() { $this->createLogger([ 'driver' => 'custom', 'via' => static::class, 'name' => config('app.name'), 'topic' => env('LOG_KAFKA_TOPIC', 'php-logs'), 'fallback' => 'daily', 'flush_timeout_ms' => 5000, 'settings' => [ // see https://github.com/edenhill/librdkafka/blob/master/CONFIGURATION.md 'metadata.broker.list' => env('LOG_KAFKA_BROKER_LIST', '127.0.0.1:9092'), 'queue.buffering.max.ms' => 3000, // < flush_timeout_ms, send messages ASAP 'socket.keepalive.enable' => true, 'log_level' => LOG_WARNING, ], // 'tap' => [App\Library\LogFormatter::class], //--格式化日志 ]); } /** * Register the log service. * * @return void */ public function register() { } private function createLogger($config){ if (!isset($config['fallback'])) { $config['fallback'] = 'daily'; } if (!isset($config['flush_timeout_ms'])) { $config['flush_timeout_ms'] = 60000; } if (!isset($config['level'])) { $config['level'] = Logger::DEBUG; } if (!isset($config['bubble'])) { $config['bubble'] = true; } $producerConf = new Conf(); foreach ($config['settings'] as $key => $value) { $producerConf->set($key, $value); } $producerConf->setLogCb(function ($kafka, $level, $facility, $message) use ($config) { app('log')->channel($config['fallback'])->info(sprintf('KafkaLogger %s: %s (level: %d)', $facility, $message, $level)); }); $producerConf->setErrorCb(function ($kafka, $err, $reason) use ($config) { app('log')->channel($config['fallback'])->error(sprintf('KafkaLogger error: %s (reason: %s)', rd_kafka_err2str($err), $reason)); }); $producerConf->setDrMsgCb(function ($kafka, $message) use ($config) { if ($message->err) { app('log')->channel($config['fallback'])->error(sprintf('KafkaLogger delivery fail: %s', $message->errstr())); } }); $handler = new KafkaLogHandler($config['name'], $producerConf, $config['topic'], null, $config['fallback'], $config['flush_timeout_ms'], $config['level'], $config['bubble']); $this->app['log']->setHandlers([$handler]); return $this->app['log']; } }注册类到bootstrap/app.php$app->register(\App\Providers\LogServiceProvider::class);最后步骤修改或新增.env配置文件如下内容LOG_CHANNEL=kafka # 注意:这一行lumen不要加的,否则白给 LOG_KAFKA_BROKER_LIST=kafka:9092 # 多个用英文逗号间隔 LOG_KAFKA_TOPIC=laravel-logs测试Log::info('测试日志', ['ok-uid-hhxsv5']);{dotted startColor="#ff6c6c" endColor="#1989fa"/}大功告成,祝顺利!
2023年02月24日
262 阅读
0 评论
0 点赞