mtpultz
mtpultz

Reputation: 18248

Laravel 5.2 Monolog to output in Chrome console

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');
});

Upvotes: 4

Views: 2457

Answers (2)

xAoc
xAoc

Reputation: 3588

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

Upvotes: 3

Been Kyung-yoon
Been Kyung-yoon

Reputation: 1754

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

Upvotes: 2

Related Questions