实现 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;
    }
}

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

相关推荐
狗凯之家源码网2 小时前
域名防红系统源码效果实测与能力边界解析
php
2601_965798472 小时前
Why Most WordPress Themes Break Elementor and How I Fixed It
数据库·人工智能·php
郑州光合科技余经理8 小时前
代驾系统架构拆解:订单链路、权限组织与私有化源码交付
开发语言·后端·算法·架构·系统架构·uni-app·php
Ivanqhz17 小时前
php8 use-def链
算法·php
DFT计算杂谈1 天前
ZrBr单层掺杂诱导的自旋/反常霍尔响应切换
linux·开发语言·机器学习·php·量子计算
BingoGo1 天前
PHP 8.6 新特性一览
后端·php
90后小陈老师1 天前
从“打开就卡“到“秒开“:一次 PHP GD 动态海报生成的性能优化实战
开发语言·性能优化·php
Sagittarius_A*1 天前
Burst Lab|PHP 审计 10:PHP 网络请求与 SSRF 漏洞审计思路
网络·安全·web安全·php·代码审计
JaguarJack1 天前
对标 npx 的 CPX PHP 的 Composer 包执行器
后端·php·服务端
BingoGo1 天前
对标 npx 的 CPX PHP 的 Composer 包执行器
后端·php