Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Monolog, how to log PHP array into console?

Tags:

php

monolog

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?

like image 308
Ryan Avatar asked Jun 27 '14 10:06

Ryan


3 Answers

Try this:

$log->addWarning('Foo: ' . var_export($data, true));
like image 162
benJ Avatar answered Oct 13 '22 17:10

benJ


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.

like image 23
Colin Smillie Avatar answered Oct 13 '22 18:10

Colin Smillie


Also, you can try this:

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

Or

$log->addWarning('Foo:' , $data);   
like image 25
Félix Castro Avatar answered Oct 13 '22 17:10

Félix Castro