Reputation: 5
Any one know how to send error messages to database in laravel which generate from app/exceptions/handler.php ?
I need to send what error massages generated in report() method to database.
Upvotes: 0
Views: 3148
Reputation: 725
If you are interested doing this manually, you can do something as following.
Step 1 - Create a model to store errors that has a DB structure as following.
class Error extends Model
{
protected $fillable = ['user_id' , 'code' , 'file' , 'line' , 'message' , 'trace' ];
}
Step 2 Locate the App/Exceptions/Handler.php file, include Auth, and the Error model you created. and replace the report function with the following code.
public function report(Exception $exception) { // Checks if a user has logged in to the system, so the error will be recorded with the user id $userId = 0; if (Auth::user()) { $userId = Auth::user()->id; } $data = array( 'user_id' => $userId, 'code' => $exception->getCode(), 'file' => $exception->getFile(), 'line' => $exception->getLine(), 'message' => $exception->getMessage(), 'trace' => $exception->getTraceAsString(), ); Error::create($data); parent::report($exception); }
(I am demonstrating this using laravel 5.6)
Upvotes: 1
Reputation: 8017
Because Laravel uses Monolog for handling logging it seems that writing Monolog Handler would be the cleanest way.
I was able to find something that exists already, please have a look at monolog-mysql package. I did not use it, so I don't know whether it works and if it works well, but it's definitely good starting point.
Upvotes: 1