Logger::critical()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
crap 2
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://github.com/flipbox/craft-psr3/blob/master/LICENSE
6
 * @link       https://github.com/flipbox/craft-psr3
7
 */
8
9
namespace flipbox\craft\psr3;
10
11
use Craft;
12
use Psr\Log\LoggerInterface;
13
use yii\base\component;
14
use yii\helpers\ArrayHelper;
15
use yii\log\Logger as YiiLogger;
16
17
/**
18
 * @author Flipbox Factory <[email protected]>
19
 * @since 1.0.0
20
 */
21
class Logger extends Component implements LoggerInterface
22
{
23
    /**
24
     * The Yii2 category to use when logging
25
     *
26
     * @var string
27
     */
28
    public $category = 'PSR-3';
29
30
    /**
31
     * The logger
32
     *
33
     * @var null|YiiLogger
34
     */
35
    public $logger;
36
37
    /**
38
     * The default level to use when an arbitrary level is used.
39
     *
40
     * @var string
41
     */
42
    public $level = YiiLogger::LEVEL_INFO;
43
44
    /**
45
     * The PSR-3 to Yii2 log level map
46
     *
47
     * @var array
48
     */
49
    public $map = [
50
        'emergency' => YiiLogger::LEVEL_ERROR,
51
        'alert' => YiiLogger::LEVEL_ERROR,
52
        'critical' => YiiLogger::LEVEL_ERROR,
53
        'error' => YiiLogger::LEVEL_ERROR,
54
        'warning' => YiiLogger::LEVEL_WARNING,
55
        'notice' => YiiLogger::LEVEL_INFO,
56
        'info' => YiiLogger::LEVEL_INFO,
57
        'debug' => YiiLogger::LEVEL_TRACE,
58
    ];
59
60
    /**
61
     * @inheritdoc
62
     */
63
    public function init()
64
    {
65
        if ($this->logger === null || !$this->logger instanceof YiiLogger) {
66
            $this->logger = Craft::getLogger();
67
        }
68
        parent::init();
69
    }
70
71
    /**
72
     * Log a message, transforming from PSR3 to the closest Yii2.
73
     *
74
     * @inheritdoc
75
     */
76
    public function log($level, $message, array $context = [])
77
    {
78
        // Resolve category from 'context'
79
        $category = ArrayHelper::remove($context, 'category', $this->category);
80
81
        // Resolve level
82
        $level = ArrayHelper::getValue($this->map, $level, $this->level);
83
84
        $this->logger->log(
85
            $this->interpolate($message, $context),
86
            $level,
87
            $category
88
        );
89
    }
90
91
    /**
92
     * @inheritdoc
93
     */
94
    public function emergency($message, array $context = [])
95
    {
96
        $this->log('emergency', $message, $context);
97
    }
98
99
    /**
100
     * @inheritdoc
101
     */
102
    public function alert($message, array $context = [])
103
    {
104
        $this->log('alert', $message, $context);
105
    }
106
107
    /**
108
     * @inheritdoc
109
     */
110
    public function critical($message, array $context = [])
111
    {
112
        $this->log('critical', $message, $context);
113
    }
114
115
    /**
116
     * @inheritdoc
117
     */
118
    public function error($message, array $context = [])
119
    {
120
        $this->log('error', $message, $context);
121
    }
122
123
    /**
124
     * @inheritdoc
125
     */
126
    public function warning($message, array $context = [])
127
    {
128
        $this->log('warning', $message, $context);
129
    }
130
131
    /**
132
     * @inheritdoc
133
     */
134
    public function notice($message, array $context = [])
135
    {
136
        $this->log('notice', $message, $context);
137
    }
138
139
    /**
140
     * @inheritdoc
141
     */
142
    public function info($message, array $context = [])
143
    {
144
        $this->log('info', $message, $context);
145
    }
146
147
    /**
148
     * @inheritdoc
149
     */
150
    public function debug($message, array $context = [])
151
    {
152
        $this->log('debug', $message, $context);
153
    }
154
155
    /**
156
     * Interpolates context values into the message placeholders.
157
     *
158
     * @param string $message
159
     * @param array $context
160
     * @return string
161
     */
162
    private function interpolate(string $message, array $context = [])
163
    {
164
        // build a replacement array with braces around the context keys
165
        $replace = array();
166
        foreach ($context as $key => $val) {
167
            // check that the value can be casted to string
168
            if (!is_array($val) && (!is_object($val) || method_exists($val, '__toString'))) {
169
                $replace['{' . $key . '}'] = $val;
170
            }
171
        }
172
173
        // interpolate replacement values into the message and return
174
        return strtr($message, $replace);
175
    }
176
}
177