Debugging Laravel with MonoLog and FirePHP


By default, Laravel is configured to create daily log files for your application, and are stored in app/storage/logs. All Laravel logging features are handled by the wonderful MonoLog library. Monolog includes various log handlers you can use – FirePHP, ChromePHP, CouchDB, Stream and many more. One of my favorites is FirePHP while debugging PHP apps.

By default Monolog is enabled in Laravel and we can just log any content easily. The following lines log the data to the logs files in app/storage/logs.

Log::info('General information log');
Log::warning('Warning log');
Log::error('Error log');

This will add the following lines to the log file.

[2013-06-07 06:23:14] log.INFO: General information log [] []
[2013-06-07 06:23:14] log.WARNING: Warning log [] []
[2013-06-07 06:23:14] log.ERROR: Error log [] []

To add FirePHP support for debugging you need to add a handler to Monolog. For that we first need to get a Monolog instance from Laravel and then add a FirePHP handler.

$test_data = array('var1' => 'test data1', 'var2' => 'test data 2');
 
/* Get Monolog instance from Laravel */
$monolog = Log::getMonolog();
 
/* Add the FirePHP handler */
$monolog->pushHandler(new \Monolog\Handler\FirePHPHandler());
 
/* Log information */
$monolog->addInfo('Log Message', $test_data);

You can now view the above logs in your FireBug console. Make sure that FirePHP extension for FireFox is installed.

This site is a digital habitat of Sameer Borate, a freelance web developer working in PHP, MySQL and WordPress. I also provide web scraping services, website design and development and integration of various Open Source API's. Contact me at metapix[at]gmail.com for any new project requirements and price quotes.

2 Responses

1

PHPDeveloper.org: Sameer Borate: Debugging Laravel with MonoLog and FirePHP

June 7th, 2013 at 7:11 am

[...] Sameer Borate has a new post to his site showing you how to debug a Laravel application with Monolog and FirePHP. [...]

2

effexts

October 4th, 2013 at 5:11 am

This also works for Symfony 2.
Great Help.

Your thoughts