实现 think/queue 日志分离

当我们使用think/queue包含了比较多的不同队列,日志会写到runtime/log目录下,合并写入的,不好排查问题,我们遇到一个比较严重的就是用了不同用户来执行,权限冲突了,导致部分队列执行不了.

为了解决以上问题,本来希望通过Log::init设置不同日志路径的,但是本地测试没生效,于是用了一下方案:

  1. event中AppInit添加QueueLog
php 复制代码
<?php
// 事件定义文件

use app\common\event\QueueLog;

return [
    'bind'      => [
    ],

    'listen'    => [
        'AppInit'  => [QueueLog::class],
        'HttpRun'  => [],
        'HttpEnd'  => [],
        'LogLevel' => [],
        'LogWrite' => [],
    ],

    'subscribe' => [
    ],
];
  1. 创建QueueLog事件
php 复制代码
<?php
namespace app\common\event;

use think\App;
use think\facade\Request;

class QueueLog
{
    public function handle(App $app): bool
    {
        if (!Request::isCli()) {
            return true;
        }
        $args = (array) $_SERVER['argv'];
        foreach ($args as $arg) {
            if (str_starts_with($arg, '--queue=')) {
                $queueName = substr($arg, 8);
                $app->setRuntimePath($app->getRuntimePath() . '/' . 'queue' . '-' . $queueName . '/');
                break;
            }
        }
        return true;
    }
}

这样的话,不同队列的日志就写到不同的路径下了

相关推荐
漏刻有时8 小时前
PHP GeoJSON转PNG地图渲染程序开发笔记、源码解读、问题复盘与整改方案
android·笔记·php
always_TT10 小时前
【Python 日志记录:logging 模块入门】
开发语言·python·php
Immortal__y13 小时前
Upload-Labs 关卡防御方式
php
JonLee202015 小时前
适配 Yii 3.0,php-casbin/yii-permission 3.0 正式发布
安全·php·rbac·yii·权限·casbin
QH1392923188017 小时前
# R&S ZNB43 ZNA43 ZNA67矢量网络分析仪
开发语言·网络·php
名字还没想好☜19 小时前
Python concurrent.futures 实战:用 ThreadPoolExecutor 并发处理 + as_completed 收结果
数据库·python·php·并发
风样滴男人哟20 小时前
PHP特性之反射类ReflectionClass机制
android·开发语言·php
nvd1120 小时前
GCP L4 Passthrough 负载均衡器“假死超时”深度排查复盘
运维·php·负载均衡
2601_9657984721 小时前
ForumLab Review: Fast PHP Script for SEO Forum Sites
android·adb·php
姜太小白1 天前
【Linux】df -h 卡住问题的通用排查与解决方案总结
linux·运维·php