如何在 Laravel 的控制台上打印消息?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42324438/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 15:21:31  来源:igfitidea点击:

How to print messages on console in Laravel?

phplaravellogging

提问by Tung Tse

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

运行 php artisan serve 时,laravel 如何在控制台上打印出一些字符串?我试过了,Log::info但它不起作用。

回答by 2upmedia

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

Laravel 5.6 简化了这一点,因为您现在有了一个logging.php可以利用的配置文件。

The key thing to know is that you want to output to stdoutand 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.

要知道的关键是你想输出到stdoutphp 有一个内置的流包装器,称为php://stdout. 鉴于此,您可以为该包装器添加通道。您可以将标准输出“频道”添加到您将要登录的频道。

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

我在这里有更多信息 - Laravel 5.6 - 写入控制台

回答by Abdulla Nilam

Try with

试试

error_log('message here.');

Read More

阅读更多

回答by Googlian

It's very simple.

这很简单。

You can call it from anywhere in APP.

您可以在 APP 的任何位置调用它。

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

回答by Kris Roofe

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 logpathto see the log.

您必须配置 laravel 存储日志的位置。默认Log::info()将日志放在日志文件中而不是控制台中。您可以使用tail -f logpath查看日志。