Laravel - Write console output to log files

Viewed 2389

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. 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?

2 Answers

You can use the screen linux command. 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():

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

EDIT:

My bad! Sorry!

Log::info() is what you want.

Related