简体   繁体   English

Laravel-将控制台输出写入日志文件

[英]Laravel - Write console output to log files

I have a command that outputs some lines: 我有一个输出一些行的命令:

/**
  * Execute the console command.
  *
  * @return mixed
  */
public function handle()
{
    $this->line('');
    $this->info('--------------------------------------------------------');
    $this->info('----------------- Compress documents -------------------');
    $this->info('--------------------------------------------------------');
    $this->line('');
    $progressBar = $this->output->createProgressBar(3);
    $this->info('Selecting files...');
    // ...
    $progressBar->advance();
    $this->info('Processing...');
    // ...
    $progressBar->advance();
    $this->info('Moving files...');
    // ...
    $progressBar->advance();
    $this->info('--------------------------------------------------------');
    $this->info('------------------------ Result ------------------------');
    $this->info('--------------------------------------------------------');
    // ...
    $this->info('Output quality: '.$resolution.'%');
    $this->info('Processed files: '.sizeof($files));
    $this->info('Original size: '.number_format($originalPathSize, 3).'MB');
    $this->info('Compressed size:'.number_format($compressedPathSize, 3).'MB');
    $this->info('Improvement: '.number_format($compressedPathSizeDiff, 3).'MB ('.number_format($compressedPathSizeDiffPercent, 3).'%)');
    $this->info('Total time: '.$timeFormatted);
    // ...
    $this->table($headers, $rows);
    $this->info('Ready!');
}

It works like a charm. 它像一种魅力。

The problem is that now I need to run this command in a production server (via SSH) and it must take some hours to process all files. 问题是,现在我需要在生产服务器(通过SSH)中运行此命令,并且必须花费几个小时来处理所有文件。 Obviously I don't want to stay logged in during this period to see console output. 显然,我不想在此期间保持登录状态以查看控制台输出。

As scheduling tasks does, there is some way to write "automatically" the console command output to log files? 就像调度任务一样,有什么方法可以“自动”将控制台命令输出写入日志文件?

You can use the screen linux command. 您可以使用screen linux命令。 Screen Example 屏幕示例

and you can save the output like 你可以像这样保存输出

#screen
#php artisan command > /etc/commandResults.log

To write output to your logs from artisan commands you can simply call $this->info() : 要从artisan命令将输出写入日志,您只需调用$this->info()

$this->info('some text');

EDIT: 编辑:

My bad! 我的错! Sorry! 抱歉!

Log::info() is what you want. Log::info()是您想要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM