Reputation: 4700
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?
Upvotes: 5
Views: 4786
Reputation: 111
You could extend the line()
method and implement this feature.
Add the option to write to log instead of console using an extra option in your command:
protected $signature = 'app:your-custom-command {--log}';
/**
* Write a string as standard output or logs in laravel.log
*/
public function line($string, $style = null, $verbosity = null): void
{
if ($this->option('log')) {
logger()->log($style, $string);
} else {
parent::line($string, $style, $verbosity);
}
}
Then make sure to define the --log
in your Kernel.php
.
$schedule->command(YourCustomCommand::class, ['--log' => true])->everyTenMinutes();
Just a headsup, this was a perfect solution for my usecase, but this solution would not display your table layout as it has its own rendering of writeln
.
Remark based on your initial thoughtprocess!
You might want to dig into the posibilities to write output of scheduler job to email or file; see https://laravel.com/docs/10.x/scheduling#task-output.
This might even solve your actual question of writing it to log instead of my suggestion.
Upvotes: 0
Reputation: 2606
You can use the screen
linux command.
Screen Example
and you can save the output like
#screen
#php artisan command > /etc/commandResults.log
Upvotes: 4
Reputation: 473
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.
Upvotes: -1