Get support for samdark/yii2-psr-log-target
If you're new to LTH, please see our FAQ for more information on what it is we do.
Support Options
Unfortunately, there are currently no active helpers for this repository on the platform. Until they become available, we reccomend the following actions:
View Open IssuesTake a look to see if anyone else has experienced the same issue as you and if they managed to solve it.
Open an IssueMake sure to read any relevant guidelines for opening issues on this repo before posting a new issue.
Sponsor directlyCheck out the page and see if there are any options to sponsor this project or it's developers directly.
samdark/yii2-psr-log-target
Yii 2 PSR Log Target
Allows you to process logs using any PSR-3 compatible logger such as Monolog.
Installation
composer require "samdark/yii2-psr-log-target"
Usage
In order to use PsrTarget
you should configure your log
application component like the following:
// $psrLogger should be an instance of PSR-3 compatible logger.
// As an example, we'll use Monolog to send log to Slack.
$psrLogger = new \Monolog\Logger('my_logger');
$psrLogger->pushHandler(new \Monolog\Handler\SlackHandler('slack_token', 'logs', null, true, null, \Monolog\Logger::DEBUG));
return [
// ...
'bootstrap' => ['log'],
// ...
'components' => [
// ...
'log' => [
'targets' => [
[
'class' => 'samdark\log\PsrTarget',
'logger' => $psrLogger,
// It is optional parameter. The message levels that this target is interested in.
// The parameter can be an array.
'levels' => ['info', yii\log\Logger::LEVEL_WARNING, Psr\Log\LogLevel::CRITICAL],
// It is optional parameter. Default value is false. If you use Yii log buffering, you see buffer write time, and not real timestamp.
// If you want write real time to logs, you can set addTimestampToContext as true and use timestamp from log event context.
'addTimestampToContext' => true,
],
// ...
],
],
],
];
Standard usage:
Yii::info('Info message');
Yii::error('Error message');
Usage with PSR logger levels:
Yii::getLogger()->log('Critical message', Psr\Log\LogLevel::CRITICAL);
Yii::getLogger()->log('Alert message', Psr\Log\LogLevel::ALERT);
Usage with original timestamp from context in the log:
// $psrLogger should be an instance of PSR-3 compatible logger.
// As an example, we'll use Monolog to send log to Slack.
$psrLogger = new \Monolog\Logger('my_logger');
$psrLogger->pushProcessor(function($record) {
if (isset($record['context']['timestamp'])) {
$dateTime = DateTime::createFromFormat('U.u', $record['context']['timestamp']);
$timeZone = $record['datetime']->getTimezone();
$dateTime->setTimezone($timeZone);
$record['datetime'] = $dateTime;
unset($record['context']['timestamp']);
}
return $record;
});
You can use PsrMessage instead of regular string messages to add custom context.
Standard usage:
Yii::error(new \samdark\log\PsrMessage("Critical message", [
'custom' => 'context',
'key' => 'value',
]));
Usage with PSR logger Levels:
Yii::getLogger()->log(new \samdark\log\PsrMessage("Critical message", [
'important' => 'context'
]), Psr\Log\LogLevel::CRITICAL);
Usage with PSR-3 log message processing:
$psrLogger = new \Monolog\Logger('my_logger');
$psrLogger->pushProcessor(new \Monolog\Processor\PsrLogMessageProcessor());
Yii::debug(new \samdark\log\PsrMessage("Greetings from {fruit}", [
'fruit' => 'banana'
]));
Running tests
In order to run tests perform the following commands:
composer install
./vendor/bin/phpunit
Our Mission
We want to make open source more sustainable. The entire platform was born from this and everything we do is in aid of this.
From the Blog
Interesting Articles
-
Generating income from open source
Jun 23 • 8 min read
-
2023 State of OSS
Apr 23 • 45 min read ★
-
A funding experiment...
Aug 19 • 10 min read
-
But You Said I could
Aug 19 • 2 min read
Thank you for checking out LiveTechHelper |
2025 © lth-dev incorporated
p-e622a1a2