Laravel PHP -在Artisan Console输出中添加时间戳

06odsfpq  于 4个月前  发布在  PHP
关注(0)|答案(3)|浏览(61)

使用Laravel PHP框架。
对于App\Console\Command类,在Artisan控制台输出(即$this->info$this->error)前添加时间戳的最佳方法是什么?
我不想在每一行都重复一个时间戳方法,我宁愿它是自动的。
谢谢

3xiyfsfu

3xiyfsfu1#

一种方法来做到这一点(假设你在Laravel 5.0+上):
PrependsOutput.php

<?php 

namespace App\Console\Commands;

trait PrependsOutput
{
    public function line($string)
    {
        parent::line($this->prepend($string));
    }

    public function comment($string)
    {
        parent::comment($this->prepend($string));
    }

    public function error($string)
    {
        parent::error($this->prepend($string));
    }

    public function info($string)
    {
        parent::info($this->prepend($string));
    }

    public function warn($string)
    {
        parent::warn($this->prepend($string));
    }

    protected function prepend($string)
    {
        if (method_exists($this, 'getPrependString')) {
            return $this->getPrependString($string).$string;
        }

        return $string;
    }
}

字符串
PrependsTimestamp.php

<?php

namespace App\Console\Commands;

trait PrependsTimestamp
{
    protected function getPrependString($string)
    {
        return date(property_exists($this, 'outputTimestampFormat') ?
            $this->outputTimestampFormat : '[Y-m-d H:i:s]').' ';
    }
}


在你的命令下:

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

class MyCommand extends Command
{
    use PrependsOutput, PrependsTimestamp;

    protected $signature = 'mycommand';
    protected $description = '';

    // you can override the default format
    // protected $outputTimestampFormat = '(m/d/Y H:i:s)';

    public function handle()
    {
        $this->comment('comment');
        $this->info('info');
        $this->warn('warn');
        $this->error('error');
        $this->line('line');
    }
}


结果:


的数据

6ss1mwsb

6ss1mwsb2#

从laravel 5.5开始,你可以添加这个特性。
并在控制台命令中使用它

<?php

namespace App\Console;

trait PrependsTimestamp
{
    public function line($string, $style = null, $verbosity = null)
    {
        $timestamped = date('[Y-m-d H:i:s] ') . ucfirst($style) . ': ' . $string;

        $styled = $style ? "<$style>$timestamped</$style>" : $timestamped;

        $this->output->writeln($styled, $this->parseVerbosity($verbosity));
    }
}

字符串

相同输出:


的数据

4bbkushb

4bbkushb3#

@peterm的答案针对Laravel 5.8(可能更早)进行了调整:

  • 需要遵循line()的父方法签名。
  • 重写line()足以为所有输出类型前置文本

PrependsOutput.php

<?php 

namespace App\Console\Commands;

trait PrependsOutput
{
    public function line($string, $style = null, $verbosity = null)
    {
        parent::line($this->prepend($string), $style, $verbosity);
    }

    protected function prepend($string)
    {
        if (method_exists($this, 'getPrependString')) {
            return $this->getPrependString($string) . $string;
        }

        return $string;
    }
}

字符串
PrependsTimestamp.php

<?php

namespace App\Console\Commands;

trait PrependsTimestamp
{
    protected function getPrependString($string)
    {
        return date(property_exists($this, 'outputTimestampFormat') ?
            $this->outputTimestampFormat : '[Y-m-d H:i:s]').' ';
    }
}


在你的命令下:

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

class MyCommand extends Command
{
    use PrependsOutput, PrependsTimestamp;

    protected $signature = 'mycommand';
    protected $description = '';

    // you can override the default format
    // protected $outputTimestampFormat = '(m/d/Y H:i:s)';

    public function handle()
    {
        $this->comment('comment');
        $this->info('info');
        $this->warn('warn');
        $this->error('error');
        $this->line('line');
    }
}


结果:
x1c 0d1x的数据

相关问题