master

laravel/framework

Last updated at: 29/12/2023 09:23

ParsesLogConfiguration.php

TLDR

This file is part of the Illuminate\Log namespace and contains a PHP trait called ParsesLogConfiguration. This trait provides methods for parsing and processing log configuration.

Methods

level

This method takes an array of log configuration and returns the corresponding Monolog constant based on the log level provided in the configuration. If the log level is not valid, it throws an InvalidArgumentException.

actionLevel

This method is similar to the level method but specifically used for processing the action level in the log configuration. It returns the Monolog constant based on the action level provided in the configuration.

parseChannel

This method extracts the log channel name from the given configuration array. If the channel name is not provided, it returns the fallback channel name obtained from the abstract method getFallbackChannelName().

Classes

None

<?php

namespace Illuminate\Log;

use InvalidArgumentException;
use Monolog\Level;

trait ParsesLogConfiguration
{
    /**
     * The Log levels.
     *
     * @var array
     */
    protected $levels = [
        'debug' => Level::Debug,
        'info' => Level::Info,
        'notice' => Level::Notice,
        'warning' => Level::Warning,
        'error' => Level::Error,
        'critical' => Level::Critical,
        'alert' => Level::Alert,
        'emergency' => Level::Emergency,
    ];

    /**
     * Get fallback log channel name.
     *
     * @return string
     */
    abstract protected function getFallbackChannelName();

    /**
     * Parse the string level into a Monolog constant.
     *
     * @param  array  $config
     * @return int
     *
     * @throws \InvalidArgumentException
     */
    protected function level(array $config)
    {
        $level = $config['level'] ?? 'debug';

        if (isset($this->levels[$level])) {
            return $this->levels[$level];
        }

        throw new InvalidArgumentException('Invalid log level.');
    }

    /**
     * Parse the action level from the given configuration.
     *
     * @param  array  $config
     * @return int
     *
     * @throws \InvalidArgumentException
     */
    protected function actionLevel(array $config)
    {
        $level = $config['action_level'] ?? 'debug';

        if (isset($this->levels[$level])) {
            return $this->levels[$level];
        }

        throw new InvalidArgumentException('Invalid log action level.');
    }

    /**
     * Extract the log channel from the given configuration.
     *
     * @param  array  $config
     * @return string
     */
    protected function parseChannel(array $config)
    {
        return $config['name'] ?? $this->getFallbackChannelName();
    }
}