tylercd100/lern

Additional fields?

eleftrik opened this issue · 5 comments

Hi, I would like to add some extra data when logging my exceptions. For example: client IP, user agent, etc.
In my case, I would like to log this information in the db table and notify it via e-mail.
What is the best way?
Thank you.

lvian commented

I would second this request, being able to store custom data would be amazing, helping developers to replicate the errors occurred

lvian commented

Ok, so I managed to deal with this with a shameful fix, but that solved my problem until some oficial answer.

Basically, inside Handler.php report function I add my custom data inside the request fields that are going to be collected if you have 'data'=>true inside lern.php

Something like this

public function report(Exception $e)
{
    Request::merge( 'customFieldKey' => 'customFieldValue' );    
    if ($this->shouldReport($e))
    {
        if (app()->bound("lern")) 
        {
        $erro = app()->make("lern")->handle($e); 
    }
    }
    return parent::report($e);
}
gthuo commented

Did it my way...like for example to collect IP address I performed these 4 steps.

  1. Create a migration that edits the schema lern table to reflect the new columns you want to add, and migrate the changes.

  2. On config/lern.php, add ip to the list of optional fields, and set it to true on
    image

  3. On vendor/tylercd100/lern/src/Recorder.php, I add ip to the list of $configDependant variables (line 52)
    image

  4. Still on Recorder.php around line 103 in the collect() method, i add the case for ip in the switch statements, just before the default
    image

  5. I add my function defined in 3 above at the end of the file.
    image

And that was all, has been working well!

@gthuo Yes, but this way you are updating code inside vendor folder. When you update this package, all your modifications will be lost. In my opinion, this is a bad practise.

Ok good news bad news.

Bad news

You're going to need to be on Laravel 5.5

Good news:

Version 4.2 gives you:

  • An IP address option (There is a new migration) Thank you @gthuo
  • Custom Recorder/Notifier classes

In the config/lern.php:

'record'=>[
        /**
         * The Recorder to use
         */
        'class' => \App\LERN\CustomRecorder::class,

        'collect' => [
            'user_agent' => true,
            ...
        ]
        ...
],

app\LERN\CustomRecorder.php

<?php

namespace App\LERN;

use Tylercd100\LERN\Components\Recorder;

class CustomRecorder extends Recorder
{
    protected function collect($key, Exception $e = null) {
        if ($key === "user_agent") {
            return $this->getUserAgent();
        }
        return parent::collect($key, $e);
    }

    protected function getUserAgent() {
        // return user agent string
    }
}

And then finally you will need to add a migration for 'user_agent' or any other custom value that you decide to collect

Please reopen the issue if necessary