Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Lumen change log file name

Tags:

lumen

Lumen logs are written to /storage/logs and by default given the name lumen.log. How do I change the file name to say xyz.log?

like image 733
HamptonNorth Avatar asked Jun 28 '15 12:06

HamptonNorth


3 Answers

As mentioned in comments the location and the name of the log file is hardcoded.

Now if for some compelling reason you want to change it you can always extend Laravel\Lumen\Application class and override getMonologHandler() method.

Create a new file Application.php in app folder that looks like

namespace App;

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

class Application extends LumenApplication
{
    protected function getMonologHandler()
    {
        return (new StreamHandler(storage_path(env('APP_LOG_PATH', 'logs/xyz.log')), Logger::DEBUG))
            ->setFormatter(new LineFormatter(null, null, true, true));
    }
}

Now change

$app = new Laravel\Lumen\Application(

to

$app = new App\Application(

in bootstrap\app.php file

Voila your log file now is called xyz.log. More over you can change it to whatever you want by defining the environment variable APP_LOG_PATH i.e. via .env file

APP_LOG_PATH=logs/abc.log
like image 188
peterm Avatar answered Nov 01 '22 04:11

peterm


There is a public method available configureMonologUsing seen here and referenced here that you can use to override the default behavior without extending the Application.

Here's how you would use it in your bootstrap/app.php:

$app->configureMonologUsing(function(Monolog\Logger $monolog) {

    $handler = (new \Monolog\Handler\StreamHandler(storage_path('/logs/xyz.log')))
        ->setFormatter(new \Monolog\Formatter\LineFormatter(null, null, true, true));

    return $monolog->pushHandler($handler);
});

Bonus: Also checkout monolog's RotatingFileHandler.

like image 37
prograhammer Avatar answered Nov 01 '22 05:11

prograhammer


In lumen 5.6 and above lumen checks for file config\logging.php. If it is present, lumen will configure logging as indicated in this file.

To get the base template, copy the file from vendor\laravel\lumen-framework\config\logging.php to config\logging.php.

Then edit config\logging.php

    ...

    'daily' => [
        'driver' => 'daily',
        'path' => storage_path('logs/xyz.log'),
        'level' => 'debug',
        'days' => 14,
    ],
    ...
like image 2
8ctopus Avatar answered Nov 01 '22 05:11

8ctopus