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

时间:2015-10-09 19:45:42

标签: php laravel artisan

我使用的是Laravel PHP框架。

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

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

由于

3 个答案:

答案 0 :(得分:9)

一种方法(假设您使用的是Laravel 5.0 +):

PrependsOutput.php

@Test
public void testTerribleCase() throws ModuleException {
    class TerribleFilter implements Filter {
        boolean seen;
        @Override
        public boolean isLoggable(LogRecord record) {
            if ("This is terrible!".equals(record.getMessage())) {
                seen = true;
            }
            return true;
        }
    }

    Logger log = Logger.getLogger(MyClass.class.getName());
    TerribleFilter tf = new TerribleFilter();
    log.setFilter(tf);
    try {
        myMethod("terrible");
        assertTrue(tf.seen);
    } finally {
        log.setFilter(null);
    }
}

PrependsTimestamp.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;
    }
}

然后在你的命令中:

<?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]').' ';
    }
}

结果:

enter image description here

答案 1 :(得分:1)

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

答案 2 :(得分:0)

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

结果:

enter image description here