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

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

相关推荐
AI 智能服务5 小时前
第6课__本地工具调用(文件操作)
服务器·人工智能·windows·php
松涛和鸣7 小时前
49、智能电源箱项目技术栈解析
服务器·c语言·开发语言·http·html·php
晚枫歌F8 小时前
io_uring的介绍和实现
开发语言·php
Tisfy11 小时前
网站访问耗时优化 - 从数十秒到几百毫秒的“零成本”优化过程
服务器·开发语言·性能优化·php·网站·建站
xifangge202512 小时前
PHP 错误日志在哪里看?Apache / Nginx / PHP-FPM 一次讲清
nginx·php·apache
ShoreKiten14 小时前
ctfshow-web257【保姆级wp】
php·web
chilavert31816 小时前
技术演进中的开发沉思-303计算机原理:加密基础
开发语言·算法·php·计算机原理
AC赳赳老秦1 天前
DeepSeek 私有化部署避坑指南:敏感数据本地化处理与合规性检测详解
大数据·开发语言·数据库·人工智能·自动化·php·deepseek
桃花键神1 天前
Undetectable接入亮数据代理IP深度测评:高效、稳定、适配性极强的海外多账号运营利器
网络协议·tcp/ip·php
catchadmin1 天前
PHP 8.5 #[\NoDiscard] 揪出“忽略返回值“的 Bug
php