Laravel PHP - 将时间戳添加到 Artisan 控制台输出

Laravel PHP - Prepend Timestamp to Artisan Console Output

我正在使用 Laravel PHP 框架。

App\Console\Command class 的 Artisan 控制台输出(即 $this->info$this->error)添加时间戳的最佳方法是什么?

我不想在每一行中都重复时间戳方法。我宁愿它是自动的。

谢谢

一种方法(假设您使用 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');
    }
}

结果:

@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');
    }
}

结果:

从 laravel 5.5 开始,您可以添加此特征。

并在您的控制台命令中使用它

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));
    }
}

相同的输出: