简体   繁体   中英

Monolog, how to log PHP array into console?

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?

尝试这个:

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

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.

Also, you can try this:

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

Or

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM