Final Class Yiisoft\Log\Logger
| Inheritance | Yiisoft\Log\Logger |
|---|---|
| Implements | Psr\Log\LoggerInterface |
| Uses Traits | Psr\Log\LoggerTrait |
Logger records logged messages in memory and sends them to different targets according to Logger::$targets.
You can call the method 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 \Yiisoft\Log\Logger::$flushInterval is reached, Logger will call Yiisoft\Log\Logger::flush() to send logged messages to different log targets, such as file or email according to the \Yiisoft\Log\Logger::$targets.
Public Methods
| Method | Description | Defined By |
|---|---|---|
| __construct() | Initializes the logger by registering Yiisoft\Log\Logger::flush() as a shutdown function. | Yiisoft\Log\Logger |
| __destruct() | 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 Yiisoft\Log\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
| 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 LogLevel constants for valid level names. | Yiisoft\Log\Logger |
Method Details
Initializes the logger by registering Yiisoft\Log\Logger::flush() as a shutdown function.
| public __construct( Yiisoft\Log\Target[] $targets = [], Yiisoft\Log\ContextProvider\ContextProviderInterface|null $contextProvider = null ): mixed | ||
| $targets | Yiisoft\Log\Target[] |
The log targets. |
| $contextProvider | Yiisoft\Log\ContextProvider\ContextProviderInterface|null |
The context provider. If null, 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();
}
Asserts that the log message level is a string.
| public static assertLevelIsString( mixed $level ): void | ||
| $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)),
);
}
Asserts that the log message level is supported.
| public static assertLevelIsSupported( string $level ): void | ||
| $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),
),
);
}
Asserts that the log message level is valid.
| public static assertLevelIsValid( mixed $level ): void | ||
| $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);
}
Flushes log messages from memory to targets.
| public flush( boolean $final = false ): void | ||
| $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);
}
| public getTargets( ): Yiisoft\Log\Target[] | ||
| return | Yiisoft\Log\Target[] |
The log targets. Each array element represents a single Yiisoft\Log\Target instance. |
|---|---|---|
public function getTargets(): array
{
return $this->targets;
}
| public log( mixed $level, string|\Stringable $message, array $context = [] ): void | ||
| $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();
}
}
Sets an array of paths to exclude from tracing when tracing is enabled with Yiisoft\Log\Logger::setTraceLevel().
| public setExcludedTracePaths( string[] $excludedTracePaths ): self | ||
| $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;
}
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 setFlushInterval( integer $flushInterval ): self | ||
| $flushInterval | integer |
The number of messages to accumulate before flushing. |
public function setFlushInterval(int $flushInterval): self
{
$this->flushInterval = $flushInterval;
return $this;
}
Sets how much call stack information (file name and line number) should be logged for each log message.
| public setTraceLevel( integer $traceLevel ): self | ||
| $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;
}
Returns the text display of the specified level.
| public static validateLevel( mixed $level ): string | ||
| $level | mixed |
The message level, e.g. \Psr\Log\LogLevel::ERROR, \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
{
self::assertLevelIsValid($level);
return $level;
}
Signup or Login in order to comment.