1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link http://www.yiiframework.com/ |
4
|
|
|
* @copyright Copyright (c) 2008 Yii Software LLC |
5
|
|
|
* @license http://www.yiiframework.com/license/ |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace yii\log; |
9
|
|
|
|
10
|
|
|
use Psr\Log\LoggerInterface; |
11
|
|
|
use yii\base\InvalidConfigException; |
12
|
|
|
use yii\di\Instance; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* PsrTarget is a log target which simply passes messages to another PSR-3 compatible logger, |
16
|
|
|
* which is specified via [[$logger]]. |
17
|
|
|
* |
18
|
|
|
* Application configuration example: |
19
|
|
|
* |
20
|
|
|
* ```php |
21
|
|
|
* return [ |
22
|
|
|
* 'logger' => [ |
23
|
|
|
* 'targets' => [ |
24
|
|
|
* [ |
25
|
|
|
* 'class' => yii\log\LoggerTarget::class, |
26
|
|
|
* 'logger' => function () { |
27
|
|
|
* $logger = new \Monolog\Logger('my_logger'); |
28
|
|
|
* $logger->pushHandler(new \Monolog\Handler\SlackHandler('slack_token', 'logs', null, true, null, \Monolog\Logger::DEBUG)); |
29
|
|
|
* return $logger; |
30
|
|
|
* }, |
31
|
|
|
* ], |
32
|
|
|
* ], |
33
|
|
|
* // ... |
34
|
|
|
* ], |
35
|
|
|
* // ... |
36
|
|
|
* ]; |
37
|
|
|
* ``` |
38
|
|
|
* |
39
|
|
|
* > Warning: make sure logger specified via [[$logger]] is not the same as [[Yii::getLogger()]], otherwise |
40
|
|
|
* your program may fall into infinite loop. |
41
|
|
|
* |
42
|
|
|
* @property LoggerInterface $logger logger to be used by this target. Refer to [[setLogger()]] for details. |
43
|
|
|
* |
44
|
|
|
* @author Paul Klimov <[email protected]> |
45
|
|
|
* @author Alexander Makarov <[email protected]> |
46
|
|
|
* @since 2.1 |
47
|
|
|
*/ |
48
|
|
|
class LoggerTarget extends Target |
49
|
|
|
{ |
50
|
|
|
/** |
51
|
|
|
* @var LoggerInterface logger instance to be used for messages processing. |
52
|
|
|
*/ |
53
|
|
|
private $_logger; |
54
|
|
|
|
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Sets the PSR-3 logger used to save messages of this target. |
58
|
|
|
* @param LoggerInterface|\Closure|array $logger logger instance or its DI compatible configuration. |
59
|
|
|
* @throws InvalidConfigException |
60
|
|
|
*/ |
61
|
|
|
public function setLogger($logger) |
62
|
|
|
{ |
63
|
|
|
if ($logger instanceof \Closure) { |
64
|
|
|
$logger = call_user_func($logger); |
65
|
|
|
} |
66
|
|
|
$this->_logger = Instance::ensure($logger, LoggerInterface::class); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @return LoggerInterface logger instance. |
71
|
|
|
* @throws InvalidConfigException if logger is not set. |
72
|
|
|
*/ |
73
|
|
|
public function getLogger() |
74
|
|
|
{ |
75
|
|
|
if ($this->_logger === null) { |
76
|
|
|
throw new InvalidConfigException('"' . get_class($this) . '::$logger" must be set to be "' . LoggerInterface::class . '" instance'); |
77
|
|
|
} |
78
|
|
|
return $this->_logger; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* {@inheritdoc} |
83
|
|
|
*/ |
84
|
|
|
public function export() |
85
|
|
|
{ |
86
|
|
|
foreach ($this->messages as $message) { |
87
|
|
|
[$level, $text, $context] = $message; |
|
|
|
|
88
|
|
|
$this->getLogger()->log($level, $text, $context); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
} |
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.