15

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

LF00
  • 27,015
  • 29
  • 156
  • 295
Tung Tse
  • 347
  • 1
  • 3
  • 9
  • 1
    Possibly related: [How do I write to the console from a Laravel Controller?](http://stackoverflow.com/q/25148662/608639), [How to echo to console in Laravel and Artisan?](http://stackoverflow.com/q/16733957/608639) – jww Feb 19 '17 at 07:23

5 Answers5

28

It's very simple.

You can call it from anywhere in APP.

$out = new \Symfony\Component\Console\Output\ConsoleOutput();
$out->writeln("Hello from Terminal");
Googlian
  • 6,077
  • 3
  • 38
  • 44
16

Try with

error_log('message here.');

Read More


If You ant add LOG

Log::info('message');

If LOG with an array

Log::info(json_encode($array));

Import Illuminate\Support\Facades\Log;

Abdulla Nilam
  • 36,589
  • 17
  • 64
  • 85
  • 1
    `Log::info()` or `Log::debug()` don't write to the browser console, they write to _storage/logs/laravel-YYYY-MM-DD.log_. Also, you have to import `Illuminate\Support\Facades\Log`. You can then display them on the fly for ex with `tail -f storage/logs/laravel-2022-01-19.log`. – ThCollignon Jan 19 '22 at 07:31
13

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

2upmedia
  • 2,832
  • 1
  • 20
  • 16
  • Wow, this was awesome. Just what I was hoping for. Thanks! – Ryan May 18 '19 at 12:18
  • This is great! How would you show the messages in different colours, like the way Commands work when printing $this->info, $this->warning, $this->error etc? I'd like to use Log::info from a service class, so I don't have $this->info available there. – thirtyish Jun 13 '19 at 11:29
  • 1
    @thirtyish You could probably use the `tap` option and this package to set the formatter https://github.com/bramus/monolog-colored-line-formatter/blob/master/readme.md – 2upmedia Jun 14 '19 at 07:00
  • @2upmedia - So that you're aware: the primary media containing in the information that you linked in "Laravel 5.6 - Write to the Console", is not loading/working/available. That media piece is basically the meat and potatoes of your article, because the line above says: "So this is how it looks like:" with a blank image. Hopefully you can fix that broken link because it looks like a great article that a lot of people can benefit from! – MEE Jan 06 '22 at 15:52
  • 1
    @MEE thanks. I haven't touched that article for years so I'll see what's going on. – 2upmedia Jan 06 '22 at 21:54
  • 1
    @MEE fixed it. Thanks for alerting me. – 2upmedia Jan 06 '22 at 22:00
  • @2upmedia - no worries, glad you got it fixed! – MEE Jan 06 '22 at 22:16
2

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.

LF00
  • 27,015
  • 29
  • 156
  • 295
1

You can call info() method

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

Akash Kumar Verma
  • 3,185
  • 2
  • 16
  • 32