'How to print messages on console in Laravel?

How do laravel print out some string on console when running php artisan serve? I tried Log::info but it isn't working.



Solution 1:[1]

It's very simple.

You can call it from anywhere in APP.

$out = new \Symfony\Component\Console\Output\ConsoleOutput();
$out->writeln("Hello from Terminal");

Solution 2:[2]

Laravel 5.6 simplified this because you now have a logging.php config file you could leverage.

The key thing to know is that you want to output to stdout and php has a stream wrapper built-in called php://stdout. Given that, you could add channel for that wrapper. You would add the stdout "channel" to your channels that you will be logging to.

Here's how the config will basically look:

<?php    

return [
  'default' => env('LOG_CHANNEL', 'stack'),

  'channels' => [
    'stack' => [
        'driver' => 'stack',
        'channels' => ['single','stdout'],
    ],

    'single' => [
        'driver' => 'single',
        'path' => storage_path('logs/laravel.log'),
        'level' => 'debug',
    ],

    'stdout' => [
        'driver' => 'monolog',
        'handler' => StreamHandler::class,
        'with' => [
            'stream' => 'php://stdout',
        ],
    ],
];

I have more information here - Laravel 5.6 - Write to the Console

Solution 3:[3]

You've to config where laravel to store the logs. Default Log::info() put the log in the log file not the console. you can use tail -f logpath to see the log.

Solution 4:[4]

You can call info() method

   $this->info("Your Message");

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Googlian
Solution 2 2upmedia
Solution 3 LF00
Solution 4 Akash Kumar Verma