Laravel 5: Errors & Logging

Laravel 5: Errors & Logging


Even best coded app will get in trouble and generate errors.
Your app will have to be able to recover from errors and be able to tell you about them, so you can (possibly) fix them.

Configuration
Error Detail

On error page show just message (se to false), or message and all that crap that makes you scratch your head ho to read it, but which sometimes can be helpful (set to true).

Controlled by APP_DEBUG
Defined in file: config/app.php
Setup in file: .env

Values:

  • true (development)
  • false (live/production)
Log Storage

Tells Laravel how and where to store your error logs.

Controlled by APP_LOG
Defined in file: config/app.php
Setup in file: .env
values: “single“, “daily“, “syslog“, “errorlog

single:
All errors single file: storage\logs\laravel.log

daily:
Errors for each day in a single, dated file: storage\logs\laravel-2017-02-06.log (example).
By default, only 5 files are stored with sixth (newest) removing oldest.
It can be changed by setting log_max_files directive in config/app.php.
It is best to define constant in app.php:

… and set it in .env file

syslog:
This is for really big apps having a separate log server called syslog – read more here.

errorlog:
This is main Apache error log.
Look for it here: \apache\logs
If in file: apache\conf\extra\httpd-vhosts.conf you set different location for error log via directive ErrorLog, e.g.
ErrorLog “u:/www/_logs/la5-error.log”, as I on my XAMPP, then look for errors there.

Log Severity Levels

Here are security levels from least to most severe:

  • debug,
  • info,
  • notice,
  • warning,
  • error,
  • critical,
  • alert,
  • emergency

If you select any of these levels, it will store its own error messages and all more severe ones.
Example: ‘alert‘ will store alert level messages and emergency level ones.

By default, Laravel comes with level ‘debug‘, which stores all messages, which is good for development.

Live/production, should have this set to ‘error‘.

The Exception Handler – Custom Error Pages
Custom, ‘place anywhere’ exception

The HttpException instance raised by the abort function will be passed to the view as an $exception variable.

Custom HTTP Error Pages

To make a custom exception page (error page), follow this procedure.
e.g. you want to make exception for 404 HTTP status code ‘not found‘, you create a blade template in:

Now just make all needed error page using HTTP status codes.
You can find them here.

How to Log Message Using Laravel
Logging info

You may write information to log file (as configured above) using the Log facade, e.g.:

Logging other (all) messages

Log level methods are as follows:

  • Log::emergency($message);
  • Log::alert($message);
  • Log::critical($message);
  • Log::error($message);
  • Log::warning($message);
  • Log::notice($message);
  • Log::info($message);
  • Log::debug($message);
Contextual Log Information

Second argument in form of an array will be added to log message: