如何将laravel日志文件数据存储到数据库中(5.5)

如何将laravel日志文件数据存储到数据库中(5.5)

问题描述:

I need to store the Laravel log into my database . Can you please help me?

我需要将Laravel日志存储到我的数据库中。 你能帮我吗? p> div>

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' ];

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.