插件窝 干货文章 Laravel在Linux上如何做错误处理

Laravel在Linux上如何做错误处理

Laravel 错误 配置 php 792    来源:    2025-05-10

Laravel在Linux上的错误处理指南

Laravel提供了强大的错误处理机制,在Linux环境下可以通过多种方式配置和管理错误处理。以下是详细的解决方案:

1. 基本错误处理配置

环境配置

.env文件中设置:

APP_DEBUG=true  # 开发环境设置为true,生产环境必须设为false
APP_ENV=local    # 或 production

日志配置

config/logging.php中配置日志:

'channels' => [
    'stack' => [
        'driver' => 'stack',
        'channels' => ['single', 'slack'], // 可以添加多个日志渠道
    ],

    'single' => [
        'driver' => 'single',
        'path' => storage_path('logs/laravel.log'),
        'level' => 'debug',
    ],

    'slack' => [
        'driver' => 'slack',
        'url' => env('LOG_SLACK_WEBHOOK_URL'),
        'username' => 'Laravel Log',
        'emoji' => ':boom:',
        'level' => 'critical',
    ],
],

2. 自定义异常处理

app/Exceptions/Handler.php中自定义异常处理:

public function register()
{
    $this->reportable(function (Throwable $e) {
        // 自定义异常报告逻辑
    });

    $this->renderable(function (ModelNotFoundException $e, $request) {
        if ($request->expectsJson()) {
            return response()->json(['error' => 'Resource not found'], 404);
        }
    });
}

3. 生产环境错误页面

创建自定义错误页面:

php artisan vendor:publish --tag=laravel-errors

然后在resources/views/errors/目录下创建: - 404.blade.php - 404错误页面 - 500.blade.php - 500错误页面 - 其他HTTP状态码对应的页面

4. 日志管理

查看日志

tail -f storage/logs/laravel.log

日志轮转配置

在Linux上配置日志轮转(创建/etc/logrotate.d/laravel):

/path/to/your/project/storage/logs/*.log {
    daily
    missingok
    rotate 14
    compress
    delaycompress
    notifempty
    create 640 www-data www-data
    sharedscripts
    postrotate
        /usr/bin/find /path/to/your/project/storage/logs/ -type f -mtime +30 -delete
    endscript
}

5. 使用Supervisor管理队列错误

安装Supervisor:

sudo apt-get install supervisor

配置队列工作进程(/etc/supervisor/conf.d/laravel-worker.conf):

[program:laravel-worker]
process_name=%(program_name)s_%(process_num)02d
command=php /path/to/your/project/artisan queue:work --sleep=3 --tries=3
autostart=true
autorestart=true
user=www-data
numprocs=8
redirect_stderr=true
stdout_logfile=/path/to/your/project/storage/logs/worker.log

重新加载Supervisor配置:

sudo supervisorctl reread
sudo supervisorctl update
sudo supervisorctl start laravel-worker:*

6. 监控和告警

使用Laravel Horizon

composer require laravel/horizon
php artisan horizon:install
php artisan horizon

使用Sentry

composer require sentry/sentry-laravel

.env中添加:

SENTRY_LARAVEL_DSN=https://your-sentry-dsn@sentry.io/your-project-id

7. 性能优化和错误预防

OPcache配置

php.ini中:

opcache.enable=1
opcache.memory_consumption=128
opcache.max_accelerated_files=10000
opcache.revalidate_freq=60

定期维护命令

设置Cron任务:

crontab -e

添加:

* * * * * cd /path-to-your-project && php artisan schedule:run >> /dev/null 2>&1

app/Console/Kernel.php中:

protected function schedule(Schedule $schedule)
{
    $schedule->command('queue:restart')->hourly();
    $schedule->command('queue:flush')->daily();
    $schedule->command('backup:clean')->daily();
    $schedule->command('backup:run')->daily();
}

8. 调试工具

Telescope(开发环境)

composer require laravel/telescope
php artisan telescope:install
php artisan migrate

Debugbar

composer require barryvdh/laravel-debugbar --dev

通过以上配置,您可以在Linux服务器上建立完善的Laravel错误处理系统,包括日志记录、异常处理、监控告警等各个方面。