如何从Laravel控制器写入控制台?

Jro*_*rop 41 php console logging laravel

所以我有一个Laravel控制器:

class YeahMyController extends BaseController {
    public function getSomething() {
        Console::info('mymessage'); // <-- what do I put here?
        return 'yeahoutputthistotheresponse';
    }
}
Run Code Online (Sandbox Code Playgroud)

目前,我正在使用artisan运行应用程序(它运行PHP的内置开发Web服务器):

php artisan serve
Run Code Online (Sandbox Code Playgroud)

我想将控制台消息记录到STDOUT工匠流程的管道中.

wir*_*d00 68

这个问题与工匠的服务有关,所以Jrop的答案在这种情况下是理想的.即,error_log登录到apache日志.

但是,如果通过标准Web服务器提供服务,则只需使用Laravel特定的日志记录功能:

\Log::info('This is some useful information.');

\Log::warning('Something could be going wrong.');

\Log::error('Something is really going wrong.');
Run Code Online (Sandbox Code Playgroud)

对于信息的当前版本的laravel如下:

info('This is some useful information.');
Run Code Online (Sandbox Code Playgroud)

这会记录到位于/laravel/storage/logs/laravel-<date>.log(laravel 5.0)的Laravel的日志文件.监控日志 - linux/osx:tail -f /laravel/storage/logs/laravel-<date>.log

  • 如果你使用的是工匠网络服务器,那就没有apache了.并且使用Log :: info也不会输出到STDOUT. (10认同)

Jro*_*rop 63

啊哈!

这可以通过以下PHP函数完成:

error_log('Some message here.');
Run Code Online (Sandbox Code Playgroud)

在这里找到答案:在PHP内置Web服务器中打印一些东西

  • @ wired00这个问题专门询问如何写入'artisan serve`控制台.`error_log()`这样做,而`Log :: info()`没有. (8认同)
  • 更理想的是使用 laravel 内置的日志记录,即 `Log::info('这​​是一些有用的信息。');` 看看我的答案 (3认同)
  • 正是我所需要的。Quick 'n dirty - 非常适合开发工作 (3认同)

Dav*_*sey 22

我自己没有试过这个,但是通过图书馆的快速挖掘表明你可以这样做:

$output = new Symfony\Component\Console\Output\ConsoleOutput();
$output->writeln("<info>my message</info>");
Run Code Online (Sandbox Code Playgroud)

我找不到这个的快捷方式,所以你可能想要创建一个外观来避免重复.


Kyl*_*lfo 13

在 Laravel 6 中有一个名为“stderr”的通道。见config/logging.php

'stderr' => [
    'driver' => 'monolog',
    'handler' => StreamHandler::class,
    'formatter' => env('LOG_STDERR_FORMATTER'),
    'with' => [
        'stream' => 'php://stderr',
    ],
],
Run Code Online (Sandbox Code Playgroud)

在您的控制器中:

use Illuminate\Support\Facades\Log;

Log::channel('stderr')->info('Something happened!');
Run Code Online (Sandbox Code Playgroud)

  • 简单又容易,谢谢 (2认同)

Goo*_*ian 12

非常简单

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

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


Tud*_*dor 9

为了更好地解释Dave Morrissey的答案,我已经在laravel外观中使用Console Output类进行了这些步骤.

1)在您喜欢的文件夹中创建一个Facade(在我的案例中为app\Facades):

class ConsoleOutput extends Facade {

 protected static function getFacadeAccessor() { 
     return 'consoleOutput';
 }

}
Run Code Online (Sandbox Code Playgroud)

2)在app\Providers中注册新的服务提供商,如下所示:

class ConsoleOutputServiceProvider extends ServiceProvider
{

 public function register(){
    App::bind('consoleOutput', function(){
        return new \Symfony\Component\Console\Output\ConsoleOutput();
     });
 }
Run Code Online (Sandbox Code Playgroud)

}

3)在config\app.php文件中添加所有这些东西,注册提供者和别名.

 'providers' => [
   //other providers
    App\Providers\ConsoleOutputServiceProvider::class
 ],
 'aliases' => [
  //other aliases
   'ConsoleOutput' => App\Facades\ConsoleOutput::class,
 ],
Run Code Online (Sandbox Code Playgroud)

就是这样,现在在Laravel应用程序的任何地方,只需以这种方式调用您的方法:

ConsoleOutput::writeln('hello');
Run Code Online (Sandbox Code Playgroud)

希望这对你有所帮助.


Tom*_*meg 8

如果你想要Laravel 的奇特命令 IO(如样式、询问和表格),那么我在下面创建了这个类

指示

我还没有完全验证它是最干净的解决方案等,但它工作得很好(但我只在 Laravel 5.5 下的单元测试用例中测试了它)。

所以很可能你可以随心所欲地使用它:

$cmd = new ConsoleCommand;

$cmd->error("Aw snap!");
$cmd->table($headers, $rows);
$answer = $cmd->ask("Tell me, what do you need?");

//even Symfony's progress bar
$cmd->outputStyle->progressStart(5);  //set n = 100% (here 100% is 5 steps)
$cmd->outputStyle->progressAdvance(); //you can call it n times
$cmd->outputStyle->progressFinish();  //set to 100%
Run Code Online (Sandbox Code Playgroud)

或者当然你也可以包裹在你自己的外观中,或者一些静态单例等,或者你想要的任何方式。

班级本身

class ConsoleCommand extends \Illuminate\Console\Command
{
    protected $name = 'NONEXISTENT';
    protected $hidden = true;

    public $outputSymfony;
    public $outputStyle;

    public function __construct($argInput = null)
    {
        parent::__construct();

        $this->input = new \Symfony\Component\Console\Input\StringInput($argInput);

        $this->outputSymfony = new \Symfony\Component\Console\Output\ConsoleOutput();
        $this->outputStyle = new \Illuminate\Console\OutputStyle($this->input, $this->outputSymfony);

        $this->output = $this->outputStyle;
    }

}
Run Code Online (Sandbox Code Playgroud)

  • 感谢这个好主意。对于当前的 Laravel 版本(使用 7.x),我认为它应该是 `public function __construct($argInput = "")`。 (2认同)

ale*_*xkb 6

我希望将我的日志信息发送到标准输出,因为告诉 Amazon 的容器服务 (ECS) 收集标准输出并将其发送到 CloudWatch Logs 很容易。所以为了让它工作,我在我的config/logging.php文件中添加了一个新的标准输出条目,如下所示:

    'stdout' => [
        'driver' => 'monolog',
        'handler' => StreamHandler::class,
        'with' => [
            'stream' => 'php://stdout',
        ],
        'level' => 'info',
    ],
Run Code Online (Sandbox Code Playgroud)

然后我简单地添加了“stdout”作为堆栈日志通道中的通道之一:

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

    'stack' => [
        'driver' => 'stack',
        'channels' => ['stdout', 'daily'],
    ],
Run Code Online (Sandbox Code Playgroud)

这样,我仍然会在文件中获取日志以进行本地开发(或者甚至在实例上,如果您可以访问它),但更重要的是,它们会被发送到保存在 CloudWatch Logs 中的 stdout。


vbe*_*nce 5

如果您想登录STDOUT,则可以使用Laravel提供的任何一种方式。例如(根据wired00的回答):

Log::info('This is some useful information.');
Run Code Online (Sandbox Code Playgroud)

可以使用以下方法来实现STDOUT的魔力(您正在将文件设置为info消息所在的位置):

Log::useFiles('php://stdout', 'info');
Run Code Online (Sandbox Code Playgroud)

请注意:这仅用于调试。在生产中不要使用任何您不完全了解的东西。