4

I'm trying to implement monolog so it outputs to chrome console. So far I have this, but when I log a message it doesn't output anything.

Is there anything else that needs to be done to make this work that I've missed?

In the past in CakePHP or CodeIgniter I pulled in ChromePHP and output to the console by typing ChromePhp::log();, but it seems like Laravel has a much cleaner way of doing this using Monolog.

AppServiceProvider.php

<?php

namespace TNC\Providers;

use Log;
use Monolog\Handler\ChromePHPHandler;
use Monolog\Formatter\ChromePHPFormatter;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        Log::listen(function () {

            $monolog = Log::getMonolog();

            if (env('APP_ENV') === 'local') {
                $monolog->pushHandler($chromeHandler = new ChromePHPHandler());
                $chromeHandler->setFormatter(new ChromePHPFormatter());
            }
        });
    }

    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }
}

Route.php

Route::get('/', function () {

    Log::info('General information log');

    return view('foundation.score');
});
mtpultz
  • 17,267
  • 22
  • 122
  • 201
  • Did you install chrome extension https://chrome.google.com/webstore/detail/chrome-logger/noaneddfkdjfnfdakjjmocngnfkfehhd ? – xAoc Jan 06 '16 at 10:51
  • Hi @xAoc, thanks for the reply. I do have it installed. – mtpultz Jan 06 '16 at 18:46
  • 2
    very strange, because I've tested in laravel 5.1 and 5.2 and all works fine. Did you try to remove env == local? Maybe your environment isn't local? – xAoc Jan 06 '16 at 21:00
  • Hi @xAoc, thanks that was it. I removed the if-statement and it worked. Checking my .env I had it set to production. Thanks for the help, stupid mistake, but totally worth the points. Post your solution and I'll mark it as the correct answer. – mtpultz Jan 08 '16 at 02:20

2 Answers2

3

The problem in the environment variable because it isn't a local. Just auto mistake or lack of attention :)

xAoc
  • 3,448
  • 3
  • 23
  • 36
2

bootstrap/app.php

/*
|--------------------------------------------------------------------------
| Custom Monolog Configuration
|--------------------------------------------------------------------------
|
| https://laravel.com/docs/5.2/errors#configuration
|
*/

$app->configureMonologUsing(function($monolog) {
    if (app()->environment('local')) {
        $monolog->pushHandler($chromeHandler = new Monolog\Handler\ChromePHPHandler());
        $chromeHandler->setFormatter(new Monolog\Formatter\ChromePHPFormatter);
    }
});

// ...

return $app;

app/Http/routes.php

Route::get('/', function () {
    Log::info('General information log');

    return view('welcome');
});

Screenshot

Chrome console

Been Kyung-yoon
  • 1,644
  • 12
  • 13