Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lumen (5.1.6) daily log

Documentantion says:

By default, Lumen is configured to create daily log files for your application which are stored in the storage/logs directory.

But my application still generating a lumen.log without daily log.

My version: Laravel Framework version Lumen (5.1.6) (Laravel Components 5.1.*) I came from 5.1 installation.

How can generate logs with daily files?

like image 991
Sangar82 Avatar asked May 20 '16 09:05

Sangar82


1 Answers

Since this commit there's a configureMonologUsing method. You should place a call to this method in your bootstrap/app.php file

use Monolog\Formatter\LineFormatter;
use Monolog\Handler\RotatingFileHandler;

$app->configureMonologUsing(function ($monolog) {
    $maxFiles = 7;

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

    $monolog->setHandlers([$rotatingLogHandler]);

    return $monolog;
});

You can create a service provider which creates a new rotating log handler and then replaces the Monolog handlers.

<?php

namespace App\Providers;

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

class LogServiceProvider extends ServiceProvider
{
    public function boot()
    {
        app('Psr\Log\LoggerInterface')->setHandlers([$this->getRotatingLogHandler()]);
    }

    public function getRotatingLogHandler($maxFiles = 7)
    {
        return (new RotatingFileHandler(storage_path('logs/lumen.log'), $maxFiles))
            ->setFormatter(new LineFormatter(null, null, true, true));
    }

    public function register()
    {
    }
}

You could also extend Application and replace the getMonologHandler or registerLogBindings methods. Below is an example replacing the former.

Replace in bootstrap/start.php

// This
$app = new Laravel\Lumen\Application(
    realpath(__DIR__.'/../')
);

// With this
$app = new App\Application(
    realpath(__DIR__.'/../')
);

And create App\Application.php

<?php

namespace App;

use Monolog\Formatter\LineFormatter;
use Monolog\Handler\RotatingFileHandler;
use Laravel\Lumen\Application as LumenApplication;

class Application extends LumenApplication
{
    /**
     * {@inheritdoc}
     */
    protected function getMonologHandler()
    {
        $maxRotatedFiles = 3

        return (new RotatingFileHandler(storage_path('logs/lumen.log'), $maxRotatedFiles))
            ->setFormatter(new LineFormatter(null, null, true, true));
    }
}
like image 178
Ben Swinburne Avatar answered Oct 11 '22 09:10

Ben Swinburne