laravellogginglevels

Laravel logging levels numeric values


I have seen that Laravel logging levels have specific numerical values in addition to the name, as follow:

'emergency' => 100
'alert'     => 200
'critical'  => 300
'error'     => 400
'warning'   => 500
'notice'    => 600
'info'      => 700
'debug'     => 800

Solution

  • Laravel`s Logger Interface follows PSR-3 Compliment and are based on RFC 5424 Log Levels.

    The LoggerInterface exposes eight methods to write logs to the eight RFC 5424 levels (debug, info, notice, warning, error, critical, alert, emergency).

    from laravel source code:

    <?php
    
    namespace Psr\Log;
    
    /**
     * Describes log levels.
     */
    class LogLevel
    {
        const EMERGENCY = 'emergency';
        const ALERT     = 'alert';
        const CRITICAL  = 'critical';
        const ERROR     = 'error';
        const WARNING   = 'warning';
        const NOTICE    = 'notice';
        const INFO      = 'info';
        const DEBUG     = 'debug';
    }
    

    RFC5424 is for system programs, not php . and PSR (PHP Standard Recommendation) article #3 suppose to use just Levels . not priorities (numbers).

    and Laravel has this interface implementing and promoise to work with every library needs to log with PSR-3 compliment Log Interface.

    Note that : PSR-3 only promises Log Names (8 levels) and not priority numbers

    Also Laravel Uses Underlying Drivers and packages for logging.

    MonoLog is the package laravel is using to log.

    it has its own api definition like priorities and codes as below:

    /**
     * Detailed debug information
     */
    const DEBUG = 100;
    
    /**
     * Interesting events
     *
     * Examples: User logs in, SQL logs.
     */
    const INFO = 200;
    
    /**
     * Uncommon events
     */
    const NOTICE = 250;
    
    /**
     * Exceptional occurrences that are not errors
     *
     * Examples: Use of deprecated APIs, poor use of an API,
     * undesirable things that are not necessarily wrong.
     */
    const WARNING = 300;
    
    /**
     * Runtime errors
     */
    const ERROR = 400;
    
    /**
     * Critical conditions
     *
     * Example: Application component unavailable, unexpected exception.
     */
    const CRITICAL = 500;
    
    /**
     * Action must be taken immediately
     *
     * Example: Entire website down, database unavailable, etc.
     * This should trigger the SMS alerts and wake you up.
     */
    const ALERT = 550;
    
    /**
     * Urgent alert.
     */
    const EMERGENCY = 600;
    

    and laravel Converts PSR-3 Log Levels (names) to Monolog Code levels

    /**
         * Converts PSR-3 levels to Monolog ones if necessary
         *
         * @param string|int Level number (monolog) or name (PSR-3)
         * @return int
         */
        public static function toMonologLevel($level)
        {
            if (is_string($level)) {
                // Contains chars of all log levels and avoids using strtoupper() which may have
                // strange results depending on locale (for example, "i" will become "İ")
                $upper = strtr($level, 'abcdefgilmnortuwy', 'ABCDEFGILMNORTUWY');
                if (defined(__CLASS__.'::'.$upper)) {
                    return constant(__CLASS__ . '::' . $upper);
                }
             }
          return $level;
      }
    

    and this is MONOLOG API 2:

    https://github.com/Seldaek/monolog/blob/master/src/Monolog/Logger.php

    RFC 5424, Numbers are only for SysLog