How to print messages on console in Laravel?

2019-07-07 01:55发布

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

4条回答
我欲成王,谁敢阻挡
2楼-- · 2019-07-07 02:11

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.

查看更多
Animai°情兽
3楼-- · 2019-07-07 02:18

Try with

error_log('message here.');

Read More

查看更多
三岁会撩人
4楼-- · 2019-07-07 02:20

It's very simple.

You can call it from anywhere in APP.

$out = new \Symfony\Component\Console\Output\ConsoleOutput();
$out->writeln("Hello from Terminal");
查看更多
女痞
5楼-- · 2019-07-07 02:21

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

查看更多
登录 后发表回答