I need to store the Laravel log into my database . Can you please help me?
Asked
Active
Viewed 9,136 times
2 Answers
3
Go to App/Exceptions/Handler.php and then write below code in report() function and define your model as an ErrorLog
$data = [
'id' => $this->createUniversalUniqueIdentifier(),
'file' => $exception->getFile(),
'line' => $exception->getLine(),
'message' => $exception->getMessage(),
'trace' => $exception->getTraceAsString(),
];
$dataArr =['id' => $data['id'],
'file' => $data['file'],
'error_summary' => 'Line '.$data['line'].' '.$data['message'],
'log_trace' => $data['trace']
];
ErrorLog::create($dataArr);
Your Model file should be like that ErrorLog.php protected $table = 'logs'; protected $fillable = ['id', 'file', 'error_summary', 'log_trace' ];

Shailesh
- 71
- 1
- 6
1
Laravel supports Monolog for handling logs. Monolog supports many different handlers, including database handlers like the MongoDB handler.
You can use the MongoDB handler by adding a new channel to the channels
array in your config/logging.php
file, e.g.:
'channels' => [
'mongolog' => [
'driver' => 'monolog',
'handler' => Monolog\Handler\MongoDBHandler::class,
'with' => [
'database' => 'mongo-database-name',
'collection' => 'log-collection-name',
],
],
Then you can set your default log channel to mongolog
in your .env
file, e.g. LOG_CHANNEL=mongolog
.

D Malan
- 10,272
- 3
- 25
- 50
-
1But this is only supported in laravel 5.6+ – Azima Mar 24 '20 at 13:20