Lumen-framework: Lumen daily log

Created on 31 Mar 2017  路  15Comments  路  Source: laravel/lumen-framework

Documentation 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 a daily log.

My version: Laravel Framework version Lumen (5.4) (Laravel Components 5.4.*) I came from 5.4 installations.

How can generate logs with daily files?

Most helpful comment

@formigone For Lumen 5.6, I got a simple solution to log daily: modify LOG_CHANNEL=stack to LOG_CHANNEL=daily in .env file.

All 15 comments

You can create a ProviderService, like LogServiceProvider in Laravel

Same issue here

use this:
https://stackoverflow.com/questions/37342418/lumen-5-1-6-daily-log/37343425#37343425

And change App\Application.php for this:

namespace App;

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

class Application extends LumenApplication {

    /**
     * {@inheritdoc}
     */
    protected function getMonologHandler() {
        $filename = storage_path('logs/lumen-' . php_sapi_name() . '.log');
        $handler = new RotatingFileHandler($filename);
        return $handler;
    }

}

To create a new LogServiceProvider is a bad choice, because Lumen has override the log abstract in Application. So it always use Laravel LogServiceProvider when you use Log facade.

A good idea is to use Application::configureMonologUsing(function ($logger) { return $logger; }) to configure the Logger. You can push daily handler to the logger.

@anilskalyane in bootstrap/app.php and before return $app add the following:

/*
|--------------------------------------------------------------------------
| Custom Configure Monolog
|--------------------------------------------------------------------------
|
*/

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

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

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

     return $monolog;
 });

Environment

Lumen (5.5.2) (Laravel Components 5.5.*)

Solution

bootstrap/app.php

/*
|--------------------------------------------------------------------------
| Custom The Application Monolog
|--------------------------------------------------------------------------
|
| Configuration daily monolog.
|
*/

$app->configureMonologUsing(function (\Monolog\Logger $logger) {
    $maxFiles = env('APP_MAX_LOG_FILE');
    $filename = storage_path('logs/lumen.log');
    $handler = new \Monolog\Handler\RotatingFileHandler($filename, $maxFiles);
    $handler->setFilenameFormat('{date}-{filename}', 'Y-m-d');
    $formatter = new \Monolog\Formatter\LineFormatter(null, null, true, true);
    $handler->setFormatter($formatter);
    $logger->pushHandler($handler);

    return $logger;
});

Good luck.

I'm using Lumen 5.6. The configuration specified by @imajinyun results in the following error:

Call to undefined method Laravel\Lumen\Application::configureMonologUsing()

Ideas?

@formigone Have you fix it ?

Call to undefined method Laravel\Lumen\Application::configureMonologUsing()

I solved it by adding a file ~/my-app/config/logging.php with the following:

<?php

use App\Logging\LoggerFactory;

return [
    'default' => env('LOG_CHANNEL', 'custom'),

    'channels' => [
        'custom' => [
            'driver' => 'custom',
            'via' => LoggerFactory::class,
            'graylogHost' => env('LOG_GRAYLOG_HOST'),
            'graylogPort' => env('LOG_GRAYLOG_PORT'),
            'name' => env('LOG_NAME'),
        ],
    ],
];

As well as a factory at ~/my-app/app/Logging/LoggerFactory.php:

<?php

namespace App\Logging;

use Gelf\Publisher;
use Gelf\Transport\UdpTransport;
use Monolog\ErrorHandler;
use Monolog\Handler\ErrorLogHandler;
use Monolog\Handler\GelfHandler;
use Monolog\Logger;
use Monolog\Processor\MemoryPeakUsageProcessor;
use Monolog\Processor\WebProcessor;

class LoggerFactory
{
    public function __invoke(array $config): Logger
    {
        $name = $config['name'];
        $graylogHost = $config['graylogHost'];
        $graylogPort = $config['graylogPort'];

        $log = new Logger($name);

        ErrorHandler::register($log);

         try {
             $transport = new UdpTransport($graylogHost, $graylogPort);
             $handler = new GelfHandler(new Publisher($transport));
             $log->pushHandler($handler);
         } catch (\Exception $e) {
             error_log('Could not add Graylog handler');
         }

         $log->pushHandler(new ErrorLogHandler());

        if (php_sapi_name() !== 'cli') {
            $log->pushProcessor(new WebProcessor());
        }

        $log->pushProcessor(new MemoryPeakUsageProcessor());

        return $log;
    }
}

Looks like since Lumen automatically registers the "logging" construct, we don't need to explicitly call $app->register() from bootstrap/app.php.

@formigone For Lumen 5.6, I got a simple solution to log daily: modify LOG_CHANNEL=stack to LOG_CHANNEL=daily in .env file.

@erDaren thank you for the simple solution.
I am facing this weird issue, where if I update the .env, then that day's log gets created and works smoothly on the first day.
However, for the next day, the new log file gets created but the user-group of the new log file is different than the log created on the first day. Due to this, the application is unable to write into the log and the services crashed. Any ideas what is it?

@erDaren thank you for the simple solution.
I am facing this weird issue, where if I update the .env, then that day's log gets created and works smoothly on the first day.
However, for the next day, the new log file gets created but the user-group of the new log file is different than the log created on the first day. Due to this, the application is unable to write into the log and the services crashed. Any ideas what is it?

We are having the same issue with Lumen 5.6

@lanex-victor are you having this issue with 5.7 as well?

Just tested this in 5.7 and this seems to work properly. Please provide more info to replicate this.

Having a similar issue, Lumen 5.6, I have set the logging to be daily by setting LOG_CHANNEL=daily, however, the log files are all gzipped.

Is this able to be turned off? I haven't found anywhere where it says there will be gzipped, let alone where it can be enabled/disabled...

Was this page helpful?
0 / 5 - 0 ratings