2

I am trying to override where lumen writes logs, from 'storage/logs/lumen.log' to 'php://stderr'. The following code is what I am currently trying, and it does not work as expected.

No errors are thrown, and my logs are still written to the default location (in the storage/logs folder).

And when I do:

dd(app('Psr\Log\LoggerInterface'));

I get the default implementation.

Did I misinterpret the documentation, or am I approaching this the wrong way?

<?php namespace App\Providers;

use Monolog\Logger;
use Monolog\Handler\StreamHandler;
use Monolog\Formatter\LineFormatter;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        app()->instance('Psr\Log\LoggerInterface', new Logger('lumen', [$this->getMonologHandler()]));
    }

    public function getMonologHandler() {
        return (new StreamHandler('php://stderr', Logger::DEBUG))->setFormatter(new LineFormatter(null, null, true, true));
    }
}
Robin
  • 1,395
  • 1
  • 9
  • 9

2 Answers2

3

You'll need to extend \Laravel\Lumen\Application and override the registerLogBindings() and/or getMonologHandler() method to set up your own logging config.

Robin
  • 1,395
  • 1
  • 9
  • 9
Jack McDade
  • 689
  • 7
  • 14
  • 1
    This is a much better idea, thanks - although does not actually answer the question specifics, it does solve the problem the question is aimed at – Robin May 15 '15 at 20:17
0

Here's a clean solution that doesn't require you to extend the application:

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\RotatingFileHandler;

class LogServiceProvider extends ServiceProvider
{
    /**
     * Configure logging on boot.
     *
     * @return void
     */
    public function boot()
    {
        $maxFiles = 5;

        $handlers[] = (new RotatingFileHandler(storage_path('logs/lumen.log'), $maxFiles))
            ->setFormatter(new LineFormatter(null, null, true, true));

        $this->app['log']->setHandlers($handlers);
    }

    /**
     * Register the log service.
     *
     * @return void
     */
    public function register()
    {
        // Log binding already registered in vendor/laravel/lumen-framework/src/Application.php.
    }
}

Then don't forget to add the service provider to your Lumen bootstrap/app.php:

$app->register(\App\Providers\LogServiceProvider::class);
prograhammer
  • 20,132
  • 13
  • 91
  • 118