This package provides PSR-3 compatible logging library. It is used in Yii Framework but is usable separately.
The logger sends passes messages to multiple targets. Each target may filter messages by their severity levels and categories and then export them to some medium such as file, email or syslog.
The package could be installed with Composer:
composer require yiisoft/log
Creating a logger:
/**
* List of class instances that extend the \Yiisoft\Log\Target abstract class.
*
* @var \Yiisoft\Log\Target[] $targets
*/
$logger = new \Yiisoft\Log\Logger($targets);
Writing logs:
$logger->emergency('Emergency message', ['key' => 'value']);
$logger->alert('Alert message', ['key' => 'value']);
$logger->critical('Critical message', ['key' => 'value']);
$logger->warning('Warning message', ['key' => 'value']);
$logger->notice('Notice message', ['key' => 'value']);
$logger->info('Info message', ['key' => 'value']);
$logger->debug('Debug message', ['key' => 'value']);
Log messages are collected and stored in memory. To limit memory consumption, the logger will flush
the recorded messages to the log targets each time a certain number of log messages accumulate.
You can customize this number by calling the \Yiisoft\Log\Logger::setFlushInterval()
method:
$logger->setFlushInterval(100); // default is 1000
Each log target also collects and stores messages in memory.
Message exporting in a target follows the same principle as in the logger.
To change the number of stored messages, call the \Yiisoft\Log\Target::setExportInterval()
method:
$target->setExportInterval(100); // default is 1000
Note: All message flushing and exporting also occurs when the application ends.
This package contains two targets:
Yiisoft\Log\PsrTarget
- passes log messages to another PSR-3 compatible logger.Yiisoft\Log\StreamTarget
- writes log messages to the specified output stream.Extra logging targets are implemented as separate packages:
Context providers are used to provide additional context data for log messages. You can define your own context provider
in Logger
constructor:
$logger = new \Yiisoft\Log\Logger(contextProvider: $myContextProvider);
Out of the box, the following context providers are available:
SystemContextProvider
— adds system information (time, memory usage, trace, default category);CommonContextProvider
— adds common data;CompositeContextProvider
— allows combining multiple context providers.By default, the logger uses built-in SystemContextProvider
.
SystemContextProvider
SystemContextProvider
added following data to context:
time
— current Unix timestamp with microseconds (float value);trace
— array of call stack information;memory
— memory usage in bytes.category
— category of the log message (always "application").Yiisoft\Log\ContextProvider\SystemContextProvider
constructor parameters:
traceLevel
— how much call stack information (file name and line number) should be logged for eachexcludedTracePaths
— array of paths to exclude from tracing when tracing is enabled with traceLevel
.Example of custom parameters' usage:
$logger = new \Yiisoft\Log\Logger(
contextProvider: new Yiisoft\Log\ContextProvider\SystemContextProvider(
traceLevel: 3,
excludedTracePaths: [
'/vendor/yiisoft/di',
],
),
);
CommonContextProvider
CommonContextProvider
allows to add additional common information to the log context. For example:
$logger = new \Yiisoft\Log\Logger(
contextProvider: new Yiisoft\Log\ContextProvider\CommonContextProvider([
'environment' => 'production',
]),
);
CompositeContextProvider
CompositeContextProvider
allows to combine multiple context providers into one. For example:
$logger = new \Yiisoft\Log\Logger(
contextProvider: new Yiisoft\Log\ContextProvider\CompositeContextProvider(
new Yiisoft\Log\ContextProvider\SystemContextProvider(),
new Yiisoft\Log\ContextProvider\CommonContextProvider(['environment' => 'production'])
),
);
If you need help or have a question, the Yii Forum is a good place for that. You may also check out other Yii Community Resources.
The Yii Logging Library is free software. It is released under the terms of the BSD License.
Please see LICENSE
for more information.
Maintained by Yii Software.