Logger   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 157
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 3
dl 0
loc 157
ccs 0
cts 59
cp 0
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 7 3
A log() 0 14 1
A emergency() 0 4 1
A alert() 0 4 1
A critical() 0 4 1
A error() 0 4 1
A warning() 0 4 1
A notice() 0 4 1
A info() 0 4 1
A debug() 0 4 1
B interpolate() 0 14 5
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
    /**
25
     * The Yii2 category to use when logging
26
     *
27
     * @var string
28
     */
29
    public $category = 'PSR-3';
30
31
    /**
32
     * The logger
33
     *
34
     * @var null|YiiLogger
35
     */
36
    public $logger;
37
38
    /**
39
     * The default level to use when an arbitrary level is used.
40
     *
41
     * @var string
42
     */
43
    public $level = YiiLogger::LEVEL_INFO;
44
45
    /**
46
     * The PSR-3 to Yii2 log level map
47
     *
48
     * @var array
49
     */
50
    public $map = [
51
        'emergency' => YiiLogger::LEVEL_ERROR,
52
        'alert' => YiiLogger::LEVEL_ERROR,
53
        'critical' => YiiLogger::LEVEL_ERROR,
54
        'error' => YiiLogger::LEVEL_ERROR,
55
        'warning' => YiiLogger::LEVEL_WARNING,
56
        'notice' => YiiLogger::LEVEL_INFO,
57
        'info' => YiiLogger::LEVEL_INFO,
58
        'debug' => YiiLogger::LEVEL_TRACE,
59
    ];
60
61
    /**
62
     * @inheritdoc
63
     */
64
    public function init()
65
    {
66
        if ($this->logger === null || !$this->logger instanceof YiiLogger) {
67
            $this->logger = Craft::getLogger();
68
        }
69
        parent::init();
70
    }
71
72
    /**
73
     * Log a message, transforming from PSR3 to the closest Yii2.
74
     *
75
     * @inheritdoc
76
     */
77
    public function log($level, $message, array $context = [])
78
    {
79
        // Resolve category from 'context'
80
        $category = ArrayHelper::remove($context, 'category', $this->category);
81
82
        // Resolve level
83
        $level = ArrayHelper::getValue($this->map, $level, $this->level);
84
85
        $this->logger->log(
86
            $this->interpolate($message, $context),
87
            $level,
88
            $category
89
        );
90
    }
91
92
    /**
93
     * @inheritdoc
94
     */
95
    public function emergency($message, array $context = [])
96
    {
97
        $this->log('emergency', $message, $context);
98
    }
99
100
    /**
101
     * @inheritdoc
102
     */
103
    public function alert($message, array $context = [])
104
    {
105
        $this->log('alert', $message, $context);
106
    }
107
108
    /**
109
     * @inheritdoc
110
     */
111
    public function critical($message, array $context = [])
112
    {
113
        $this->log('critical', $message, $context);
114
    }
115
116
    /**
117
     * @inheritdoc
118
     */
119
    public function error($message, array $context = [])
120
    {
121
        $this->log('error', $message, $context);
122
    }
123
124
    /**
125
     * @inheritdoc
126
     */
127
    public function warning($message, array $context = [])
128
    {
129
        $this->log('warning', $message, $context);
130
    }
131
132
    /**
133
     * @inheritdoc
134
     */
135
    public function notice($message, array $context = [])
136
    {
137
        $this->log('notice', $message, $context);
138
    }
139
140
    /**
141
     * @inheritdoc
142
     */
143
    public function info($message, array $context = [])
144
    {
145
        $this->log('info', $message, $context);
146
    }
147
148
    /**
149
     * @inheritdoc
150
     */
151
    public function debug($message, array $context = [])
152
    {
153
        $this->log('debug', $message, $context);
154
    }
155
156
    /**
157
     * Interpolates context values into the message placeholders.
158
     *
159
     * @param string $message
160
     * @param array $context
161
     * @return string
162
     */
163
    private function interpolate(string $message, array $context = [])
164
    {
165
        // build a replacement array with braces around the context keys
166
        $replace = array();
167
        foreach ($context as $key => $val) {
168
            // check that the value can be casted to string
169
            if (!is_array($val) && (!is_object($val) || method_exists($val, '__toString'))) {
170
                $replace['{' . $key . '}'] = $val;
171
            }
172
        }
173
174
        // interpolate replacement values into the message and return
175
        return strtr($message, $replace);
176
    }
177
}
178