Final Class Yiisoft\Log\Logger
| Inheritance | Yiisoft\ |
|---|---|
| Implements | Psr\ |
| Uses Traits | Psr\ |
Logger records logged messages in memory and sends them to different targets according to {@see Logger::$targets}.
You can call the method {@see \
For more details and usage information on Logger, see PSR-3 specification.
When the application ends or {@see \
Public Methods
| Method | Description | Defined By |
|---|---|---|
| __construct() | Initializes the logger by registering {@see Logger::flush()} as a shutdown function. | Yiisoft\ |
| __destruct() | Yiisoft\ |
|
| assertLevelIsString() | Asserts that the log message level is a string. | Yiisoft\ |
| assertLevelIsSupported() | Asserts that the log message level is supported. | Yiisoft\ |
| assertLevelIsValid() | Asserts that the log message level is valid. | Yiisoft\ |
| flush() | Flushes log messages from memory to targets. | Yiisoft\ |
| getTargets() | Yiisoft\ |
|
| log() | Yiisoft\ |
|
| setExcludedTracePaths() | Sets an array of paths to exclude from tracing when tracing is enabled with {@see Logger::setTraceLevel()}. | Yiisoft\ |
| setFlushInterval() | Sets how many log messages should be logged before they are flushed from memory and sent to targets. | Yiisoft\ |
| setTraceLevel() | Sets how much call stack information (file name and line number) should be logged for each log message. | Yiisoft\ |
| validateLevel() | Returns the text display of the specified level. | Yiisoft\ |
Constants
| Constant | Value | Description | Defined By |
|---|---|---|---|
| LEVELS | [
\ |
The list of log message levels. See {@see LogLevel} constants for valid level names. | Yiisoft\ |
Method Details
Initializes the logger by registering {@see Logger::flush()} as a shutdown function.
| public mixed __construct ( Yiisoft\ | ||
| $targets | Yiisoft\ |
The log targets. |
| $contextProvider | Yiisoft\ |
The context provider. If null, {@see \ |
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 void assertLevelIsString ( mixed $level ) | ||
| $level | mixed |
The message level. |
| throws | \ |
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 void assertLevelIsSupported ( string $level ) | ||
| $level | string |
The message level. |
| throws | \ |
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 void assertLevelIsValid ( mixed $level ) | ||
| $level | mixed |
The message level. |
| throws | \ |
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 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);
}
| public Yiisoft\ | ||
| return | Yiisoft\ |
The log targets. Each array element represents a single {@see \ |
|---|---|---|
public function getTargets(): array
{
return $this->targets;
}
| public void log ( mixed $level, string|\ | ||
| $level | mixed | |
| $message | string|\ |
|
| $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 {@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;
}
Sets how many log messages should be logged before they are flushed from memory and sent to targets.
See also \
| 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;
}
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;
}
Returns the text display of the specified level.
| public static string validateLevel ( mixed $level ) | ||
| $level | mixed |
The message level, e.g. {@see \ |
| return | string |
The text display of the level. |
|---|---|---|
| throws | \ |
for invalid log message level. |
public static function validateLevel(mixed $level): string
{
self::assertLevelIsValid($level);
return $level;
}
User Contributed Notes
Leave a comment
Join the conversation to share a note.