【问题标题】:Laravel PHP - Prepend Timestamp to Artisan Console OutputLaravel PHP - 在 Artisan 控制台输出前添加时间戳
【发布时间】:2016-01-07 20:38:30
【问题描述】:

我正在使用 Laravel PHP 框架。

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

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

谢谢

【问题讨论】:

    标签: php laravel laravel-artisan


    【解决方案1】:

    从 laravel 5.5 开始,你可以添加这个 trait。

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

    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】:

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

      结果:

      【讨论】:

        【解决方案3】:

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

        结果:

        【讨论】:

        • 不确定它是否是 Laravel 5.5 的东西,但现在你只需要重写 line() 方法,它会处理所有的输出方法。
        • 还应该通过$style$verbosity 参数,例如public function line($string, $style = NULL, $verbosity = NULL)
        • Laravel 5.8 现在就像@aaron 已经指出的那样简单地覆盖line($string, $style = NULL, $verbosity = NULL)
        猜你喜欢
        • 2021-01-20
        • 1970-01-01
        • 2018-07-31
        • 2018-08-27
        • 1970-01-01
        • 2022-06-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多