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

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

相关推荐
小虎哥-技术博客1 小时前
ThinkPHP 5.x 到 8.x 行为扩展迁移指南
php
小虎哥-技术博客6 小时前
ThinkPHP 5 到 ThinkPHP 8 路由迁移完整指南
php
yangSnowy8 小时前
PHP的运行模式
开发语言·php
浅川.2510 小时前
STL专项:queue 队列
数据结构·stl·queue
YJlio11 小时前
网络与通信具总览(14.0):从 PsPing 到 TCPView / Whois 的联合作战
开发语言·网络·php
iCheney!11 小时前
php生成赛博像素风头像
开发语言·php
小虎哥-技术博客13 小时前
ThinkPHP 5.0.24 到 ThinkPHP 8.x 迁移学习指南
php
m0_7381207214 小时前
渗透测试——靶机DC-6详细横向过程(Wordpress渗透)
服务器·网络·python·web安全·ssh·php
傻啦嘿哟15 小时前
实战:用GraphQL接口高效采集数据
开发语言·驱动开发·php
BingoGo15 小时前
CatchAdmin 2025 年终总结 模块化架构的进化之路
后端·开源·php