Skip to content

Laravel Error Logging System

This error logging system allows you to capture and store critical error information in the database, helping with tracking, debugging, and monitoring the overall health of your Laravel application. By logging essential error details, it provides valuable insights into application issues and facilitates easier troubleshooting.

Features

  • Comprehensive Error Logging: Logs include error type, message, stack trace, file, line, request URL, HTTP method, request data, IP address, and user agent.
  • Automatic Logging: Errors are automatically logged into the error_logs table through seamless integration with Laravel’s exception handler.
  • Centralized Error Tracking: Helps track and monitor errors across your application in a centralized location.
  • Easy Setup: Simple installation and configuration steps to integrate into your existing Laravel project.

Installation and Setup

Step 1: Create the error_logs Table Migration

To create the error_logs table, generate a migration file:

bash
php artisan make:migration create_error_logs_table
php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateErrorLogsTable extends Migration
{
    public function up()
    {
        Schema::create('error_logs', function (Blueprint $table) {
            $table->id();
            $table->string('error_type')->nullable();
            $table->text('error_message');
            $table->text('stack_trace')->nullable();
            $table->string('file')->nullable();
            $table->integer('line')->nullable();
            $table->string('url')->nullable();
            $table->string('method', 10)->nullable();
            $table->json('request_data')->nullable();
            $table->string('ip_address')->nullable();
            $table->text('user_agent')->nullable();
            $table->timestamps();
        });
    }

    public function down()
    {
        Schema::dropIfExists('error_logs');
    }
}

This will create the error_logs table in your database.

Step 2: Create the ErrorLog Model

bash
php artisan make:model ErrorLog
php
namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class ErrorLog extends Model
{
    protected $fillable = ['error_type', 'error_message', 'stack_trace', 'file', 'line', 'url', 'method', 'request_data', 'ip_address', 'user_agent'];
}

Step 3: Create the ErrorLog Service

bash
php artisan make:service ErrorLogService
php
namespace App\Services;

use App\Models\ErrorLog;

class ErrorLogService
{
    public static function logError(\Throwable $exception)
    {
        ErrorLog::create([
            'error_type' => get_class($exception),
            'error_message' => $exception->getMessage(),
            'stack_trace' => $exception->getTraceAsString(),
            'file' => $exception->getFile(),
            'line' => $exception->getLine(),
            'url' => request()->fullUrl(),
            'method' => request()->method(),
            'request_data' => request()->all(),
            'ip_address' => request()->ip(),
            'user_agent' => request()->userAgent(),
        ]);
    }
}

Step 4: Register the ErrorLog Service in the Exception Handler

php
namespace App\Exceptions;

use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use App\Services\ErrorLogService;

class Handler extends ExceptionHandler
{
    public function render($request, \Throwable $exception)
    {
        ErrorLogService::logError($exception);
        return parent::render($request, $exception);
    }
}

This integration ensures that every exception is logged to the error_logs table, making it easier to trace and resolve issues.