Completed
Push — 2.1 ( 4d9204...3e6f8b )
by
unknown
11:56
created

LoggerTarget::export()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 0
cts 7
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 0
crap 6
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;
0 ignored issues
show
Bug introduced by
The variable $level does not exist. Did you forget to declare it?

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.

Loading history...
Bug introduced by
The variable $text does not exist. Did you forget to declare it?

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.

Loading history...
Bug introduced by
The variable $context does not exist. Did you forget to declare it?

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.

Loading history...
88
            $this->getLogger()->log($level, $text, $context);
89
        }
90
    }
91
}