Skip to content

Commit

Permalink
add docs
Browse files Browse the repository at this point in the history
  • Loading branch information
klimov-paul committed Jul 19, 2023
1 parent afc45d1 commit 8933903
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 1 deletion.
91 changes: 90 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ For example:

use Psr\log\LogLevel;

Yii::log('psr message', LogLevel::INFO); // same as `Yii::log('psr message', CLogger::LEVEL_INFO);`
Yii::log('psr message', LogLevel::INFO, 'psr-category'); // same as `Yii::log('psr message', CLogger::LEVEL_INFO, 'psr-category');`

Yii::log('context message', LogLevel::INFO, [
'foo' => 'bar', // specifying log context, which will be passed to the related PSR logged, and added as JSON to the Yii log message, if it is enabled
Expand All @@ -105,9 +105,42 @@ try {
}
```

You may also specify a global log context, which should be written with every message. For example:

```php
<?php

// set custom logger:
Yii::setLogger(
\yii1tech\psr\log\Logger::new()
->setPsrLogger(function () {
// ...
})
->withContext(function () {
$context = [];

// log remote IP address if available:
if (!empty($_SERVER['REMOTE_ADDR'])) {
$context['ip'] = $_SERVER['REMOTE_ADDR'];
}

// log authenticated user ID, if available:
$webUser = Yii::app()->getComponent('user', false);
if ($webUser !== null && !$webUser->getIsGuest()) {
$context['auth_user_id'] = $webUser->getId();
}

return $context;
})
);
```


## PSR Log Route <span id="psr-log-route"></span>

It is not necessary to `\yii1tech\psr\log\Logger` if you need to pass logs to PSR logger.
As an alternative you can add `\yii1tech\psr\log\PsrLogRoute` log route to the standard Yii "log" component.

Application configuration example:

```php
Expand Down Expand Up @@ -136,5 +169,61 @@ return [
];
```

> Note: even if you use `\yii1tech\psr\log\Logger` as Yii logger, this `\yii1tech\psr\log\PsrLogRoute` will be unable to handle
passed log context correctly.


## Wrap Yii logger into PSR logger <span id="wrap-yii-logger-into-psr-logger"></span>

There is another use case related to PSR logger besides bootstrapping eternal log storage.
Sometimes 3rd party libraries may require PSR logger instance to be passed to them in order to function.
For example, imagine we have a 3rd party library for "daemon" application running:

```php
<?php

namespace vendor\daemon;

use Psr\Log\LoggerInterface;

class DaemonApplication
{
public function __construct(LoggerInterface $logger)
{
// ....
}
}
```

You can use `\yii1tech\psr\log\PsrLogger` to wrap standard Yii logging mechanism into PSR interface.

Application configuration example:

```php
<?php

return [
'components' => [
\Psr\Log\LoggerInterface::class => [
'class' => \yii1tech\psr\log\PsrLogger::class,
],
// ...
],
// ...
];
```

Now while working with our example external "daemon" application, we can use following code:

```php
<?php

use Psr\Log\LoggerInterface;
use vendor\daemon\DaemonApplication;

$daemon = new DaemonApplication(Yii::app()->getComponent(LoggerInterface::class));
// ...
```

> Note: in order to handle log context properly `\yii1tech\psr\log\PsrLogger` should be used in junction with `\yii1tech\psr\log\Logger`.
16 changes: 16 additions & 0 deletions src/AbstractPsrLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,22 @@
* AbstractArrayLogger is an intermediate class for {@see PsrLogger} creation.
* Its existence required since {@see \Psr\Log\LoggerInterface} changes signature over different PHP versions.
*
* Application configuration example:
*
* ```php
* return [
* 'components' => [
* \Psr\Log\LoggerInterface::class => [
* 'class' => \yii1tech\psr\log\PsrLogger::class,
* ],
* // ...
* ],
* // ...
* ];
* ```
*
* > Note: in order to handle log context properly this class should be used in junction with {@see \yii1tech\psr\log\Logger}
*
* @author Paul Klimov <[email protected]>
* @since 1.0
*/
Expand Down

0 comments on commit 8933903

Please sign in to comment.