Monolog, how to log PHP array into console?

丶灬走出姿态 提交于 2019-12-03 14:14:43

问题


I am using the browser handler to log message into JS console

require_once 'vendor/autoload.php';

use Monolog\Logger;
use Monolog\Handler\BrowserConsoleHandler;

$log = new Logger('name');
$log->pushHandler(new BrowserConsoleHandler);

$data = array(1,2,3,4);

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

I am wondering, is it possible to log array such as $data into the console which reassemble the array content?


回答1:


Try this:

$log->addWarning('Foo: ' . var_export($data, true));



回答2:


The best approach ( from the 2nd half of Felix's answer ) for an array is:

$log->addWarning('Foo:' , $data); 

The AddWarning will accept an array as the 2nd parameter and format it properly in the browser.

Using var_export will convert to a string and not format the array properly in the browser console.




回答3:


Also, you can try this:

$log->addWarning('Foo: ' . print_r($data, true));  

Or

$log->addWarning('Foo:' , $data);   


来源:https://stackoverflow.com/questions/24450180/monolog-how-to-log-php-array-into-console

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!