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('执行业务逻辑...');
    }
}
相关推荐
翼龙云_cloud1 小时前
腾讯云渠道商:腾讯云轻量服务器和CVM有什么差异?
运维·服务器·云计算·php·腾讯云
万岳软件开发小城1 小时前
开发一套私域直播 APP/Web/小程序需要哪些核心模块?完整技术清单来了
小程序·php·直播带货系统源码·直播带货软件开发·私域直播系统源码·私域直播平台搭建·私域直播软件开发
小李独爱秋1 小时前
计算机网络经典问题透视:简述一下TCP拥塞控制算法中的拥塞避免算法
服务器·网络·tcp/ip·计算机网络·php
蒲公英源码2 小时前
基于PHP+Nginx+Redis+MySQL社区生活服务平台
javascript·vue.js·mysql·php
编程小Y14 小时前
php.ini 的核心作用与全面解析
开发语言·php
saber_andlibert18 小时前
【docker】网络基础和容器编排
网络·docker·php
互亿无线明明19 小时前
如何为全球业务构建可扩展的“群发国际短信接口”?
java·c++·python·golang·eclipse·php·erlang
用户6073203694520 小时前
PHP Parse error: syntax error 5分钟带你解决语法错误
php
4311媒体网21 小时前
php和c++哪个更好学?C++难学吗?
java·c++·php