Laravel 5.4 Logs And Errors

|
| By Webner

Introduction to Laravel 5.4

Logfiles:
A log file is a file that keeps a registry or records of events, processes, messages that occur in the operating system or any software runs.

-Laravel provides the robust logging services that allow the user to log messages to file.
-Laravel uses the Monolog library, which is an existing standard logging library for PHP. It provides support for a variety of powerful log handlers.
-The App\Exceptions\Handler class is where all the exceptions are triggered by your application when they are logged and then rendered back to the user.

  1. Set the configuration for your logging system housed in config/app.php.
    There is a debug Option in a file which determines how much information for error
    should be displayed to the users with respect to APP_DEBUG which is an environment
    Variable stored in .env file

                'debug' => env('APP_DEBUG', false) 
     

    * For (live/production) their value is set to false due to the risk of exposing sensitive
    information to the end-users and for local development. You should set their value to
    true

  2. Now configure which storage mechanism you want to use. In laravel, we can write log
    information in Single file, Daily file, Syslog and error log.

    Single file: storage\logs\example.log

     'log' => env('APP_LOG', 'single')
    

    Daily file: storage\logs\examplel-2019-06-07.log
    //Errors for each day in a single, dated file

            
       'log' => env('APP_LOG','daily')
    

    We can set the Maximum log files in daily log mode. By default, only 5 files are stored with the sixth (newest) removing the oldest files. It can be changed by setting the log_max_files directive in config/app.php

    'log_max_files' => env('APP_LOG_MAX_FILES', 30)
    

    Syslog: Syslog messages usually include information to help identify basic information
    about when, where, and why the log was sent: IP address, timestamp, and the actual log
    Message.
    //This is for really big apps having a separate log server

    Errorlog: This is the main Apache error log. We can set a different location for error log via directive ‘ErrorLog’.
    E.g. ErrorLog “u:/www/_logs/example.log”, as I on my XAMPP, then look for errors
    there.

  3. After that, we have to set the Log Severity Levels. In laravel, there are different severity or security levels. By default, Laravel writes all the log levels to its storage.

    Here are security levels from least to most severe:
    -debug,
    -info,
    -notice,
    -warning,
    -error,
    -critical,
    -alert,
    -emergency

    All of them will store there own error messages

    'log_level' => env('APP_LOG_LEVEL', 'debug') 
    

    By default, Laravel comes with level ‘debug‘, which stores all messages. It is good for Development and for Live/production and should have this set to ‘error‘.

  4. To write a log file into another location, use the method useDailyFiles or usefiles. After that info to log store to the log file which path would be specified

    Log::useDailyFiles(storage_path().'/logs/name-of-log.log'); 
    Log::info([info to log]);
    

    // There is also a second argument for the useDailyFiles which specifies the number of days log would be stored before erasing the old logs. The default value is unlimited.

Leave a Reply

Your email address will not be published. Required fields are marked *