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.
I would second this request, being able to store custom data would be amazing, helping developers to replicate the errors occurred
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);
}
Did it my way...like for example to collect IP address I performed these 4 steps.
-
Create a migration that edits the schema lern table to reflect the new columns you want to add, and migrate the changes.
-
On config/lern.php, add ip to the list of optional fields, and set it to true on
-
On vendor/tylercd100/lern/src/Recorder.php, I add ip to the list of $configDependant variables (line 52)
-
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
-
I add my function defined in 3 above at the end of the file.
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