-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add ability to setup PSR logger as
Closure
- Loading branch information
1 parent
1dd4290
commit 5e563a5
Showing
4 changed files
with
80 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,7 +11,27 @@ | |
* | ||
* This class can be used in case you with to utilize 3rd party PSR logger library like "Monolog" in your Yii application. | ||
* | ||
* @property \Psr\Log\LoggerInterface|string|array|null $psrLogger related PSR logger. | ||
* Configuration example: | ||
* | ||
* ```php | ||
* require __DIR__ . '../vendor/autoload.php'; | ||
* // ... | ||
* | ||
* $app = Yii::createWebApplication($config); | ||
* $app->setLogger( | ||
* \yii1tech\psr\log\Logger::new() | ||
* ->setPsrLogger(function () { | ||
* $log = new \Monolog\Logger('yii'); | ||
* $log->pushHandler(new \Monolog\Handler\StreamHandler('path/to/your.log', \Monolog\Level::Warning)); | ||
* | ||
* return $log; | ||
* }) | ||
* ->enableYiiLog(true) | ||
* ); | ||
* $app->run(); | ||
* ``` | ||
* | ||
* @property \Psr\Log\LoggerInterface|\Closure|string|array|null $psrLogger related PSR logger. | ||
* | ||
* @author Paul Klimov <[email protected]> | ||
* @since 1.0 | ||
|
@@ -39,13 +59,18 @@ public function getPsrLogger(): ?LoggerInterface | |
|
||
if (!is_object($this->_psrLogger)) { | ||
$this->_psrLogger = Yii::createComponent($this->_psrLogger); | ||
} elseif ($this->_psrLogger instanceof \Closure) { | ||
$this->_psrLogger = call_user_func($this->_psrLogger); | ||
} | ||
|
||
return $this->_psrLogger; | ||
} | ||
|
||
/** | ||
* @param \Psr\Log\LoggerInterface|string|array|null $psrLogger | ||
* Sets the PSR logger to pass logs to. | ||
* If `null` provided - no PSR logger will be used. | ||
* | ||
* @param \Psr\Log\LoggerInterface|\Closure|array|string|null $psrLogger related PSR logger. | ||
* @return static self reference. | ||
*/ | ||
public function setPsrLogger($psrLogger): self | ||
|
@@ -104,4 +129,15 @@ public function log($message, $level = 'info', $category = 'application'): void | |
); | ||
} | ||
} | ||
|
||
/** | ||
* Creates new self instance. | ||
* This method can be useful when writing chain methods calls. | ||
* | ||
* @return static new self instance. | ||
*/ | ||
public static function new(): self | ||
{ | ||
return new static(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,10 +12,36 @@ | |
* | ||
* This class can be used in case you with to utilize 3rd party PSR logger library like "Monolog" in your Yii application. | ||
* | ||
* Application configuration example: | ||
* | ||
* ```php | ||
* return [ | ||
* 'components' => [ | ||
* 'log' => [ | ||
* 'class' => \CLogRouter::class, | ||
* 'routes' => [ | ||
* [ | ||
* 'class' => \yii1tech\psr\log\PsrLogRoute::class, | ||
* 'psrLogger' => function () { | ||
* $log = new \Monolog\Logger('yii'); | ||
* $log->pushHandler(new \Monolog\Handler\StreamHandler('path/to/your.log', \Monolog\Level::Warning)); | ||
* | ||
* return $log; | ||
* }, | ||
* ], | ||
* // ... | ||
* ], | ||
* ], | ||
* // ... | ||
* ], | ||
* // ... | ||
* ]; | ||
* ``` | ||
* | ||
* > Note: even if you use {@see \yii1tech\psr\log\Logger} as Yii logger, this log route will be unable to handle | ||
* passed log context correctly. | ||
* | ||
* @property \Psr\Log\LoggerInterface|string|array $psrLogger related PSR logger. | ||
* @property \Psr\Log\LoggerInterface|\Closure|string|array $psrLogger related PSR logger. | ||
* | ||
* @author Paul Klimov <[email protected]> | ||
* @since 1.0 | ||
|
@@ -38,13 +64,15 @@ public function getPsrLogger(): LoggerInterface | |
|
||
if (!is_object($this->_psrLogger)) { | ||
$this->_psrLogger = Yii::createComponent($this->_psrLogger); | ||
} elseif ($this->_psrLogger instanceof \Closure) { | ||
$this->_psrLogger = call_user_func($this->_psrLogger); | ||
} | ||
|
||
return $this->_psrLogger; | ||
} | ||
|
||
/** | ||
* @param \Psr\Log\LoggerInterface|array|string $psrLogger | ||
* @param \Psr\Log\LoggerInterface|\Closure|array|string $psrLogger | ||
* @return static self reference. | ||
*/ | ||
public function setPsrLogger($psrLogger): self | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters