PHP:如何使用独白登录控制台(php://out)? [英] PHP: How to use monolog to log to console (php://out)?

查看:56
本文介绍了PHP:如何使用独白登录控制台(php://out)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚切换到独白,想将我的消息记录到PHP控制台而不是文件中.对于某些人来说,这似乎很明显,但是我花了一些时间才知道该怎么做,而我在SO上找不到类似的问题/答案.

I just switched to monolog and wanted to log my message to the PHP console instead of a file. This might seem obvious for some people, but it took me a little while to figure out how to do that and I couldn't find a similar question/answer on SO.

Monolog的Github自述文件上的示例仅显示了如何使用文件:

The example on Monolog's Github readme only shows how to use a file:

<?php

use Monolog\Logger;
use Monolog\Handler\StreamHandler;

// create a log channel
$log = new Logger('name');
$log->pushHandler(new StreamHandler('path/to/your.log', Logger::WARNING)); // <<< uses a file

// add records to the log
$log->addWarning('Foo');
$log->addError('Bar');

但是它没有任何地方说明如何将消息记录到控制台.在Google上搜索后,我进入了Symfony的帮助页面,或者遇到了一些人正在寻找登录浏览器控制台的方式的问题.

But it doesn't state anywhere how messages can be logged to the console. After searching on Google, I landed either on a help page for Symfony or questions of people looking for a way to log to the browser console.

推荐答案

解决方案非常简单.由于该示例显示了 StreamHandler ,因此可以传入流(而不是文件的路径).默认情况下,在PHP中回显的所有内容都被写入 php://stdout/php://output ,因此我们可以简单地将其中之一用作 StreamHandler 的流:

The solution is rather simple. Since the example shows a StreamHandler it's possible to pass in a stream (instead of the path to a file). By default, everything that is echo'ed in PHP is written to php://stdout / php://output so we can simple use one of those as stream for the StreamHandler:

<?php

use Monolog\Logger;
use Monolog\Handler\StreamHandler;

// create a log channel
$log = new Logger('name');
$log->pushHandler(new StreamHandler('php://stdout', Logger::WARNING)); // <<< uses a stream

// add records to the log
$log->warning('Foo');
$log->error('Bar');

希望这可以节省一些时间:)

Hope this saves somebody some time :)

这篇关于PHP:如何使用独白登录控制台(php://out)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆