souvikbhattacharyas's avatar

Display class and method name in log (Log formatter)

Hi, Currently as per my monolog implementation in laravel I can see the printed message in the file which I have written with Log::debug/info. But for better control and debugging I would like to include the class name and method name on each message.

For java it can be done with formatted but how we can do it in laravel?? Any help would be appreciated.

0 likes
11 replies
souvikbhattacharyas's avatar

In this solution I have to write the class and function name in every log message I write. But what I want that I will put it in some centralize file and this the rest process will execute automatically. Just like in java you mention the 'Log Format' in log4j.props and everything else taken care.

souvikbhattacharyas's avatar

I have already gone though the tutorial you shared earlier but not been able to figure out the way I can make this happen. May be because I am new in php related technologies. Can you provide me a sample configuration.

My Requirement - Daily rotating log files stored in specified location. Class and method name should be included in the log file .

My current configuration is -

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

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

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

.............

Any help would be appreciable.

souvikbhattacharyas's avatar

Thanks for the reply. I will also look for the solution by that time it will be great if you can help me out with an example depending on your availability.

D9705996's avatar
D9705996
Best Answer
Level 51

This is something I had been trying to do for a while so your question intrigued me and I think I have found a solution.

If you create a new file app\Logging\CustomizeFormatter with the following content

<?php

namespace App\Logging;

use Monolog\Logger;
use Monolog\Processor\IntrospectionProcessor;

class CustomizeFormatter
{
    public function __invoke($logger)
    {
        foreach ($logger->getHandlers() as $handler) {
            $handler->pushProcessor(new IntrospectionProcessor(Logger::DEBUG, ['Illuminate\']));
        }
    }
}

If you then change your active logging configuration (I'm using daily channel) to add a tap key with your custom formatter

 'tap'    => [App\Logging\CustomizeFormatter::class],

Then when you use the logger you will get something like the following added to your log entries

{
  "file": "/.../app/Http/Controllers/MyController.php",
  "line": 25,
  "class": "App\Http\Controllers\MyController",
  "function": "index"
}

Think that covers what you asked for, feel free to change the names/file locations to suit and the array being passed to the IntrospectionProcessor as the 2nd argument is classes to ignore (If you omit this you just get the logger class in the logs which isn't useful).

2 likes
souvikbhattacharyas's avatar

Hi, Thanks for the help. Actually it worked for me also. But I have below questions for better understanding.

a) Will it effect the performance in production as it needs to check the process every time? b) In the IntrospectionProcessor, do we have option to only display class name and method name instead of all. I have checked the code of the processor but didn't find any.

D9705996's avatar

@SOUVIKBHATTACHARYAS - To be honest I don't have any hard facts for you on performance. I would suck it and see! If you need to only apply this for dev then create a separate logging channel that you only use in dev (via .env).

D9705996's avatar

Also the code that provides the extra data is here

You might be able to play about with the formatting options to only show extra.class and extra.method. TBH for me if im debugging I want all the data so would be tempted to just leave as is

Please or to participate in this conversation.