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

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

相关推荐
DFT计算杂谈18 小时前
SCIENTIFIC REPORTS 非 van der Waals可调二维磁性材料设计平台
开发语言·php
凡尘——雨落凡尘18 小时前
PHP实战复盘:高并发限流、防刷、验证码、会话安全、CSRF防护导致站点稳定性与安全加固实战
php·csrf 防护·csrf 攻击
2601_9657984719 小时前
Bostami Portfolio Template Review: Speed, Code & SEO Test
php
AC赳赳老秦1 天前
开源组件版本数据监控:OpenClaw 抓取公开版本信息,自动提醒更新与安全风险
前端·python·安全·开源·github·php·openclaw
漏刻有时1 天前
PHP GeoJSON转PNG地图渲染程序开发笔记、源码解读、问题复盘与整改方案
android·笔记·php
always_TT1 天前
【Python 日志记录:logging 模块入门】
开发语言·python·php
Immortal__y2 天前
Upload-Labs 关卡防御方式
php
JonLee20202 天前
适配 Yii 3.0,php-casbin/yii-permission 3.0 正式发布
安全·php·rbac·yii·权限·casbin
QH139292318802 天前
# R&S ZNB43 ZNA43 ZNA67矢量网络分析仪
开发语言·网络·php
名字还没想好☜2 天前
Python concurrent.futures 实战:用 ThreadPoolExecutor 并发处理 + as_completed 收结果
数据库·python·php·并发