thinkphp8 执行 php think 命令的时候指定日志存储路径

一、在命令类中动态设置日志路径

php 复制代码
<?php
namespace app\command;

use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;
use think\facade\Log;

class CustomTask extends Command
{
    protected function configure()
    {
        $this->setName('custom:task')
             ->setDescription('自定义任务命令')
             ->addOption('log-path', 'l', Option::VALUE_OPTIONAL, '指定日志路径')
             ->addOption('log-level', null, Option::VALUE_OPTIONAL, '日志级别', 'info');
    }

    protected function execute(Input $input, Output $output)
    {
        // 设置日志路径
        $logPath = $input->getOption('log-path') 
            ?: app()->getRuntimePath() . 'logs/command/' . date('Y-m-d') . '/';
        
        $this->setupCommandLog($logPath);
        
        $logLevel = $input->getOption('log-level');
        
        Log::info('命令开始执行', [
            'command' => $this->getName(),
            'log_path' => $logPath,
            'log_level' => $logLevel
        ]);
        
        $output->writeln("日志路径: {$logPath}");
        
        try {
            // 业务逻辑
            $this->handleTask($output);
            
            Log::info('命令执行成功');
            $output->writeln('<info>任务完成</info>');
            
        } catch (\Exception $e) {
            Log::error('命令执行失败: ' . $e->getMessage());
            $output->writeln('<error>执行失败: ' . $e->getMessage() . '</error>');
            return 1;
        }
        
        return 0;
    }
    
    protected function handleTask(Output $output)
    {
        for ($i = 1; $i <= 3; $i++) {
            $message = "处理第 {$i} 个任务";
            Log::info($message);
            $output->writeln($message);
            sleep(1);
        }
    }
    
    protected function setupCommandLog($path)
    {
        // 确保目录存在
        if (!is_dir($path)) {
            mkdir($path, 0755, true);
        }
        
        // 动态配置日志
        config([
            'log.channels.command' => [
                'type' => 'File',
                'path' => $path,
                'level' => ['info', 'error', 'debug'],
                'single' => false,
                'max_files' => 10,
            ]
        ]);
        
        // 设置为默认日志通道
        config(['log.default' => 'command']);
    }
}

二、使用环境变量指定日志路径

php 复制代码
<?php
namespace app\command;

use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\facade\Log;
use think\facade\Env;

class EnvTask extends Command
{
    protected function configure()
    {
        $this->setName('env:task')
             ->setDescription('使用环境变量配置日志');
    }

    protected function execute(Input $input, Output $output)
    {
        // 从环境变量获取日志路径
        $logPath = Env::get('command_log_path', app()->getRuntimePath() . 'logs/command/');
        $logLevel = Env::get('command_log_level', 'info');
        
        $this->setupLogging($logPath, $logLevel);
        
        Log::info('环境变量命令开始执行');
        $output->writeln("使用日志路径: {$logPath}");
        
        // 业务逻辑
        $this->runBusinessLogic($output);
        
        Log::info('环境变量命令执行完成');
        $output->writeln('<info>完成</info>');
        
        return 0;
    }
    
    protected function setupLogging($path, $level)
    {
        config([
            'log' => [
                'default' => 'file',
                'channels' => [
                    'file' => [
                        'type' => 'File',
                        'path' => $path,
                        'level' => $level,
                        'single' => false,
                        'max_files' => 30,
                    ],
                ],
            ]
        ]);
    }
    
    protected function runBusinessLogic(Output $output)
    {
        // 业务代码
        $output->writeln('执行业务逻辑...');
    }
}
相关推荐
运筹vivo@9 分钟前
攻防世界: unseping
web安全·php
JaguarJack16 分钟前
PHP 8.5 闭包和一等可调用对象进入常量表达式
后端·php·服务端
科技块儿11 小时前
使用强大的离线IP地址定位库IP数据云获取数据信息
网络·tcp/ip·php
做萤石二次开发的哈哈16 小时前
萤石开放平台 萤石可编程设备 | 设备 Python SDK 使用说明
开发语言·网络·python·php·萤石云·萤石
steem_ding18 小时前
net.core 调优指南
开发语言·php
hteng18 小时前
逮住那个幽灵:Laravel+Supervisor后台任务高并发下 PDO Error 2014 的排查实录
php·laravel
zcfeng53020 小时前
PHP升级
开发语言·php
无情的8861 天前
S11参数与反射系数的关系
开发语言·php·硬件工程
CRMEB1 天前
高品质开源电商系统的技术内核:架构设计与技术优势
ai·开源·php·免费源码·源代码管理·商城源码
BingoGo1 天前
别再手写 URL 解析器了:PHP 8.5 URI 扩展让 URL 处理更安全、更干净
后端·php