نسخه:

خطاها و ثبت نام

پیکربندی

کنترل کننده گزارش برای برنامه شما در app/start/global.php فایل شروع ثبت شده است . به طور پیش فرض، لاگر به گونه ای پیکربندی شده است که از یک فایل گزارش استفاده کند. با این حال، ممکن است در صورت نیاز این رفتار را سفارشی کنید. از آنجایی که لاراول از کتابخانه پرطرفدار logging Monolog استفاده می کند ، می توانید از انواع کنترل کننده هایی که Monolog ارائه می دهد استفاده کنید.

برای مثال، اگر می‌خواهید از فایل‌های گزارش روزانه به جای یک فایل بزرگ استفاده کنید، می‌توانید تغییر زیر را در فایل شروع خود اعمال کنید:

$logFile = 'laravel.log';
 
Log::useDailyFiles(storage_path().'/logs/'.$logFile);

جزئیات خطا

به طور پیش فرض، جزئیات خطا برای برنامه شما فعال است. این به این معنی است که وقتی خطایی رخ می دهد، یک صفحه خطا با یک ردیابی پشته و پیام خطا به شما نشان داده می شود. شما می توانید جزئیات خطا را با تنظیم debug گزینه موجود در فایل خود app/config/app.php روی false .

توجه: اکیداً توصیه می شود که جزئیات خطا را در محیط تولید خاموش کنید.

رسیدگی به خطاها

به طور پیش فرض، app/start/global.php فایل حاوی یک کنترل کننده خطا برای همه استثناها است:

App::error(function(Exception $exception)
{
Log::error($exception);
});

این اصلی ترین کنترل کننده خطا است. با این حال، در صورت نیاز، می توانید کنترل کننده های بیشتری را مشخص کنید. هندلرها بر اساس نوع اشاره استثنایی که آنها را مدیریت می کنند فراخوانی می شوند. برای مثال، می‌توانید یک کنترلر ایجاد کنید که فقط RuntimeException نمونه‌ها را کنترل کند:

App::error(function(RuntimeException $exception)
{
// Handle the exception...
});

اگر یک کنترل کننده استثنا پاسخی را برگرداند، آن پاسخ به مرورگر ارسال می شود و هیچ کنترل کننده خطا دیگری فراخوانی نمی شود:

App::error(function(InvalidUserException $exception)
{
Log::error($exception);
 
return 'Sorry! Something is wrong with this account!';
});

برای گوش دادن به خطاهای مهلک PHP، می توانید از App::fatal روش زیر استفاده کنید:

App::fatal(function($exception)
{
//
});

اگر چندین کنترل کننده استثنا دارید، آنها باید از عمومی ترین تا خاص ترین تعریف شوند. بنابراین، برای مثال، یک کنترل کننده که تمام استثناهای نوع را کنترل می کند Exception باید قبل از یک نوع استثنای سفارشی مانند تعریف شود Illuminate\Encryption\DecryptException .

محل قرار دادن کنترل کننده های خطا

There is no default "home" for error handler registrations. Laravel offers you freedom in this area. One option is to define the handlers in your start/global.php file. In general, this is a convenient location to place any "bootstrapping" code. If that file is getting crowded, you could create an app/errors.php file, and require that file from your start/global.php script. A third option is to create a service provider that registers the handlers. Again, there is no single "correct" answer. Choose a location that you are comfortable with.

HTTP Exceptions

Some exceptions describe HTTP error codes from the server. For example, this may be a "page not found" error (404), an "unauthorized error" (401) or even a developer generated 500 error. In order to return such a response, use the following:

App::abort(404);

Optionally, you may provide a response:

App::abort(403, 'Unauthorized action.');

This method may be used at any time during the request's lifecycle.

Handling 404 Errors

You may register an error handler that handles all "404 Not Found" errors in your application, allowing you to easily return custom 404 error pages:

App::missing(function($exception)
{
return Response::view('errors.missing', array(), 404);
});

Logging

The Laravel logging facilities provide a simple layer on top of the powerful Monolog library. By default, Laravel is configured to create a single log file for your application, and this file is stored in app/storage/logs/laravel.log. You may write information to the log like so:

Log::info('This is some useful information.');
 
Log::warning('Something could be going wrong.');
 
Log::error('Something is really going wrong.');

The logger provides the seven logging levels defined in RFC 5424: debug, info, notice, warning, error, critical, and alert.

An array of contextual data may also be passed to the log methods:

Log::info('Log message', array('context' => 'Other helpful information'));

Monolog has a variety of additional handlers you may use for logging. If needed, you may access the underlying Monolog instance being used by Laravel:

$monolog = Log::getMonolog();

You may also register an event to catch all messages passed to the log:

Registering A Log Listener

Log::listen(function($level, $message, $context)
{
//
});