pankaj
pankaj

Reputation: 1906

how to customize file name of log in Laravel 8.0?

i was searching solution for custom Log file name. i got a solution that was working in 5.6 version project. but after that means more than 5.6 it is not working. i try to used many solution but not working. every time file name generating laravel.log. but i want to get it like laravel-{vendor_code}-{date}.log

i used solution that code is below.

CustomLogFile.php

<?php

    namespace App\Logging;
    
    use Illuminate\Http\Request;
    use Monolog\Handler\RotatingFileHandler;
    
    class CustomLogFile
    {
        /**
         * Customize the given logger instance.
         *
         * @param  \Illuminate\Log\Logger  $logger
         * @return void
         */
        public function __invoke($logger )
        {
            $code = 'NA';
            $headers = apache_request_headers();
            if( isset( $headers['DBAuth'] ))
            {
                $code =  dnc($headers['DBAuth']) ;
            }
            elseif ( isset($_REQUEST['code']) )
            {
                $code = $_REQUEST['code'];
            }
    
            foreach ($logger->getHandlers() as $handler) {
                if ($handler instanceof RotatingFileHandler) {
                    $sapi = php_sapi_name();
                    $handler->setFilenameFormat("{filename}-$code-{date}", 'Y-m-d');
                }
            }
        }
    }

Config/ logging.php

'default' => env('LOG_CHANNEL', 'stack'),
   ......

    'channels' => [
        'stack' => [
            'driver' => 'stack',
            'channels' => ['daily'],
            'ignore_exceptions' => false,
        ],

        'single' => [
            'driver' => 'single',         
            'path' => storage_path('logs/laravel.log'),
            'level' => 'debug',
        ],

        'daily' => [
            'driver' => 'daily',
            'tap' => [App\Logging\CustomLogFile::class],
            'path' => storage_path('logs/laravel.log'),
            'level' => 'debug',
            'days' => 14,
        ],

here this code is working as my condition in 5.6 v of Laravel but not more than 5.6 v.

i try to inspect the $logger->getHandlers() . then i am getting stream array is null. so can it be reason of this.

Upvotes: 0

Views: 3037

Answers (2)

Den
Den

Reputation: 534

Here is working solution for Laravel 8:

CustomLogFile.php


class CustomLogFile
{

    public function __invoke(array $config): Logger
    {
        $log = new Logger($config['logname']);
        $level = $log::toMonologLevel($config['level'] ?: 'debug');

        $vendor_code = $_REQUEST['code'] ?? 'NA';
        $date = Carbon::now()->toDateString();

        $logPath = storage_path("logs/$vendor_code-$date.log");
        $log->pushHandler(new StreamHandler($logPath, $level, false));

        return $log;
    }
}

Config/logging.php

'channels' => [
//...
        'by_vendor_code' => [
            'driver' => 'custom',
            'via' => UserLogger::class,
            'logname' => 'by_vendor_code',
            'level' => 'debug',
        ],
//...
    ],

Usage example from anywhere:

logs('by_vendor_code')->info('this vendor want to: ', ['context' => 'some context data']);

Upvotes: 2

pankaj
pankaj

Reputation: 1906

may be its answer will be like this in env file

LOG_CHANNEL=daily
LOG_LEVEL=debug

Also Change day setting in Config/logging.php

'daily' => [
            'driver' => 'daily',
            'tap' => [App\Logging\CustomLogFile::class],
            'path' => storage_path('logs/laravel.log'),
            'level' => 'debug',
            'days' => 1,  // 14----> 1

because in 6.2 version i was getting that result. so answer here i will confirm soon with 8.* version.

Upvotes: 0

Related Questions