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('执行业务逻辑...');
    }
}
相关推荐
CRMEB-嘉嘉11 小时前
CRMEB私域会员电商系统pro-用户等级
php
苏琢玉12 小时前
一次受限环境下的 MySQL 数据导出与“可交付化”实践
mysql·php
#Tan-shu#API14 小时前
PHP调用手机归属地查询API的实现方法
智能手机·php·api接口·手机归属地查询·三网手机号码归属地查询
catchadmin14 小时前
PHP True Async 最近进展以及背后的争议
开发语言·php
小韩博14 小时前
小迪笔记45课之-PHP应用&SQL二次注入&堆叠执行&DNS带外&功能点&黑白盒条件
笔记·sql·网络安全·php
wniuniu_16 小时前
ceph心跳机制
开发语言·ceph·php
小韩博18 小时前
PHP-MySQL 数据请求与 SQL 注入多样性(小迪 43 课笔记整理)
sql·mysql·php
中年程序员一枚19 小时前
php实现调用ldap服务器,实现轻量级目录访问协议(Lightweight Directory Access Protocol,LDAP)
服务器·开发语言·php
亚历山大海20 小时前
PHP发送outlook(微软)OAuth 2.0企业版邮箱验证码
开发语言·php·outlook
钟智强20 小时前
红队实战复盘:如何运用【火尖枪】高效突破复杂登录防线
服务器·安全·web安全·http·go·php·bruteforce