0 follower

Final Class Yiisoft\Log\Logger

InheritanceYiisoft\Log\Logger
ImplementsPsr\Log\LoggerInterface
Uses TraitsPsr\Log\LoggerTrait

Logger records logged messages in memory and sends them to different targets according to {@see Logger::$targets}.

You can call the method {@see \Yiisoft\Log\Logger::log()} to record a single log message.

For more details and usage information on Logger, see PSR-3 specification.

When the application ends or {@see \Yiisoft\Log\Logger::$flushInterval} is reached, Logger will call {@see \Yiisoft\Log\Logger::flush()} to send logged messages to different log targets, such as file or email according to the {@see \Yiisoft\Log\Logger::$targets}.

Public Methods

Hide inherited methods

Method Description Defined By
__construct() Initializes the logger by registering {@see Logger::flush()} as a shutdown function. Yiisoft\Log\Logger
assertLevelIsString() Asserts that the log message level is a string. Yiisoft\Log\Logger
assertLevelIsSupported() Asserts that the log message level is supported. Yiisoft\Log\Logger
assertLevelIsValid() Asserts that the log message level is valid. Yiisoft\Log\Logger
flush() Flushes log messages from memory to targets. Yiisoft\Log\Logger
getTargets() Yiisoft\Log\Logger
log() Yiisoft\Log\Logger
setExcludedTracePaths() Sets an array of paths to exclude from tracing when tracing is enabled with {@see Logger::setTraceLevel()}. Yiisoft\Log\Logger
setFlushInterval() Sets how many log messages should be logged before they are flushed from memory and sent to targets. Yiisoft\Log\Logger
setTraceLevel() Sets how much call stack information (file name and line number) should be logged for each log message. Yiisoft\Log\Logger
validateLevel() Returns the text display of the specified level. Yiisoft\Log\Logger

Constants

Hide inherited constants

Constant Value Description Defined By
LEVELS [ \Psr\Log\LogLevel::EMERGENCY, \Psr\Log\LogLevel::ALERT, \Psr\Log\LogLevel::CRITICAL, \Psr\Log\LogLevel::ERROR, \Psr\Log\LogLevel::WARNING, \Psr\Log\LogLevel::NOTICE, \Psr\Log\LogLevel::INFO, \Psr\Log\LogLevel::DEBUG, ] The list of log message levels. See {@see LogLevel} constants for valid level names. Yiisoft\Log\Logger

Method Details

Hide inherited methods

__construct() public method

Initializes the logger by registering {@see Logger::flush()} as a shutdown function.

public mixed __construct ( Yiisoft\Log\Target[] $targets = [], Yiisoft\Log\ContextProvider\ContextProviderInterface|null $contextProvider null )
$targets Yiisoft\Log\Target[]

The log targets.

$contextProvider Yiisoft\Log\ContextProvider\ContextProviderInterface|null

The context provider. If null, {@see \Yiisoft\Log\ContextProvider\SystemContextProvider} with default parameters will be used.

                public function __construct(
    array $targets = [],
    ?ContextProviderInterface $contextProvider = null,
) {
    $this->setTargets($targets);
    $this->contextProvider = $contextProvider ?? new SystemContextProvider();
    register_shutdown_function(function () {
        // make regular flush before other shutdown functions, which allows session data collection and so on
        $this->flush();
        // make sure log entries written by shutdown functions are also flushed
        // ensure "flush()" is called last when there are multiple shutdown functions
        register_shutdown_function([$this, 'flush'], true);
    });
}

            
assertLevelIsString() public static method

Asserts that the log message level is a string.

public static void assertLevelIsString ( mixed $level )
$level mixed

The message level.

throws \Psr\Log\InvalidArgumentException

When the log message level is not a string.

                public static function assertLevelIsString(mixed $level): void
{
    if (is_string($level)) {
        return;
    }
    throw new \Psr\Log\InvalidArgumentException(
        sprintf('The log message level must be a string, %s provided.', get_debug_type($level))
    );
}

            
assertLevelIsSupported() public static method

Asserts that the log message level is supported.

public static void assertLevelIsSupported ( string $level )
$level string

The message level.

throws \Psr\Log\InvalidArgumentException

When the log message level is not supported.

                public static function assertLevelIsSupported(string $level): void
{
    if (in_array($level, self::LEVELS, true)) {
        return;
    }
    throw new \Psr\Log\InvalidArgumentException(
        sprintf(
            'Invalid log message level "%s" provided. The following values are supported: "%s".',
            $level,
            implode('", "', self::LEVELS)
        )
    );
}

            
assertLevelIsValid() public static method

Asserts that the log message level is valid.

public static void assertLevelIsValid ( mixed $level )
$level mixed

The message level.

throws \Psr\Log\InvalidArgumentException

When the log message level is not a string or is not supported.

                public static function assertLevelIsValid(mixed $level): void
{
    self::assertLevelIsString($level);
    self::assertLevelIsSupported($level);
}

            
flush() public method

Flushes log messages from memory to targets.

public void flush ( boolean $final false )
$final boolean

Whether this is a final call during a request.

                public function flush(bool $final = false): void
{
    $messages = $this->messages;
    // https://github.com/yiisoft/yii2/issues/5619
    // new messages could be logged while the existing ones are being handled by targets
    $this->messages = [];
    $this->dispatch($messages, $final);
}

            
getTargets() public method

public Yiisoft\Log\Target[] getTargets ( )
return Yiisoft\Log\Target[]

The log targets. Each array element represents a single {@see \Yiisoft\Log\Target} instance.

                public function getTargets(): array
{
    return $this->targets;
}

            
log() public method

public void log ( mixed $level, string|\Stringable $message, array $context = [] )
$level mixed
$message string|\Stringable
$context array

                public function log(mixed $level, string|Stringable $message, array $context = []): void
{
    self::assertLevelIsString($level);
    $this->messages[] = new Message(
        $level,
        $message,
        array_merge($this->contextProvider->getContext(), $context),
    );
    if ($this->flushInterval > 0 && count($this->messages) >= $this->flushInterval) {
        $this->flush();
    }
}

            
setExcludedTracePaths() public method
Deprecated since 2.1, to be removed in 3.0 version. Use {@see \Yiisoft\Log\self::$contextProvider} and {@see \Yiisoft\Log\ContextProvider\SystemContextProvider::setExcludedTracePaths()} instead.

Sets an array of paths to exclude from tracing when tracing is enabled with {@see Logger::setTraceLevel()}.

public self setExcludedTracePaths ( string[] $excludedTracePaths )
$excludedTracePaths string[]

The paths to exclude from tracing.

throws InvalidArgumentException

for non-string values.

                public function setExcludedTracePaths(array $excludedTracePaths): self
{
    if (!$this->contextProvider instanceof SystemContextProvider) {
        throw new RuntimeException(
            '"Logger::setExcludedTracePaths()" is unavailable when using a custom context provider.'
        );
    }
    /** @psalm-suppress DeprecatedMethod */
    $this->contextProvider->setExcludedTracePaths($excludedTracePaths);
    return $this;
}

            
setFlushInterval() public method

Sets how many log messages should be logged before they are flushed from memory and sent to targets.

See also \Yiisoft\Log\Logger::$flushInterval.

public self setFlushInterval ( integer $flushInterval )
$flushInterval integer

The number of messages to accumulate before flushing.

                public function setFlushInterval(int $flushInterval): self
{
    $this->flushInterval = $flushInterval;
    return $this;
}

            
setTraceLevel() public method
Deprecated since 2.1, to be removed in 3.0 version. Use {@see \Yiisoft\Log\self::$contextProvider} and {@see \Yiisoft\Log\ContextProvider\SystemContextProvider::setTraceLevel()} instead.

Sets how much call stack information (file name and line number) should be logged for each log message.

public self setTraceLevel ( integer $traceLevel )
$traceLevel integer

The number of call stack information.

                public function setTraceLevel(int $traceLevel): self
{
    if (!$this->contextProvider instanceof SystemContextProvider) {
        throw new RuntimeException(
            '"Logger::setTraceLevel()" is unavailable when using a custom context provider.'
        );
    }
    /** @psalm-suppress DeprecatedMethod */
    $this->contextProvider->setTraceLevel($traceLevel);
    return $this;
}

            
validateLevel() public static method
Deprecated since 2.1, to be removed in 3.0. Use {@see \Psr\Log\LogLevel::assertLevelIsValid()} instead.

Returns the text display of the specified level.

public static string validateLevel ( mixed $level )
$level mixed

The message level, e.g. {@see \Psr\Log\LogLevel::ERROR}, {@see \Psr\Log\LogLevel::WARNING}.

return string

The text display of the level.

throws \Psr\Log\InvalidArgumentException

for invalid log message level.

                public static function validateLevel(mixed $level): string
{
    if (!is_string($level)) {
        throw new \Psr\Log\InvalidArgumentException(sprintf(
            'The log message level must be a string, %s provided.',
            get_debug_type($level)
        ));
    }
    if (!in_array($level, self::LEVELS, true)) {
        throw new \Psr\Log\InvalidArgumentException(sprintf(
            'Invalid log message level "%s" provided. The following values are supported: "%s".',
            $level,
            implode('", "', self::LEVELS)
        ));
    }
    return $level;
}