5

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?

11
  • 2
    Probably not, that I can think of, but you can add your own Log::info() lines to your code here Commented Jun 28, 2019 at 13:06
  • I'm also using lines like $this->line('...'); which ARE being written to the laravel log files. Trying to figure out how this works. Commented Jun 28, 2019 at 13:24
  • @BramVerstraten storage/logs/laravel.log? Commented Jun 28, 2019 at 13:27
  • @AlexandreThebaldi That's correct Commented Jun 28, 2019 at 13:29
  • @BramVerstraten Strange! my laravel.log stay intact after running the command many times... Commented Jun 28, 2019 at 13:30

3 Answers 3

4

You can use the screen linux command. Screen Example

and you can save the output like

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

Sign up to request clarification or add additional context in comments.

1 Comment

Yep, the idea is that you want a command, and you should be able to execute it on the shell and get a result. If you change it to write always to a file it lost their purpose of having a command, using screen and redirecting you should be able to execute it without the connection issues. I was going to recommend a cron job but I was thinking it was not a recurrence need.
0

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.

Comments

-1

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.

2 Comments

The OP already is. That's most of the commands in their function. That command is only for writing output to the console
False. I ran the command with $this->info('...') and it doesn't appears on log files

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.