PsrTarget::getLogger()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace yrc\components\log;
4
5
use Psr\Log\LoggerAwareInterface;
6
use Psr\Log\LoggerAwareTrait;
7
use Psr\Log\LoggerInterface;
8
use Psr\Log\LogLevel;
9
use yii\base\InvalidConfigException;
10
use yii\helpers\VarDumper;
11
use yii\log\Logger;
12
use yii\log\Target;
13
use Yii;
14
15
class PsrTarget extends Target
16
{
17
    use LoggerAwareTrait;
18
19
    private $_psrLevels = [
20
        Logger::LEVEL_ERROR => LogLevel::ERROR,
21
        Logger::LEVEL_WARNING => LogLevel::WARNING,
22
        Logger::LEVEL_INFO => LogLevel::INFO,
23
        Logger::LEVEL_TRACE => LogLevel::DEBUG,
24
        Logger::LEVEL_PROFILE => LogLevel::DEBUG,
25
        Logger::LEVEL_PROFILE_BEGIN => LogLevel::DEBUG,
26
        Logger::LEVEL_PROFILE_END => LogLevel::DEBUG,
27
    ];
28
29
    /**
30
     * @return LoggerInterface
31
     * @throws InvalidConfigException
32
     */
33
    public function getLogger()
34
    {
35
        if ($this->logger === null) {
36
            throw new InvalidConfigException('Logger should be configured with Psr\Log\LoggerInterface.');
37
        }
38
        return $this->logger;
39
    }
40
41
    /**
42
     * @inheritdoc
43
     */
44
    public function export()
45
    {
46
        foreach ($this->messages as $message) {
47
            $context = [];
48
            if (isset($message[4])) {
49
                $context['trace'] = $message[4];
50
            }
51
52
            if (isset($message[5])) {
53
                $context['memory'] = $message[5];
54
            }
55
56
            if (isset($message[2])) {
57
                $context['category'] = $message[2];
58
            }
59
60
            $text = $message[0];
61
62
            if (!is_string($text)) {
63
                // exceptions may not be serializable if in the call stack somewhere is a Closure
64
                if ($text instanceof \Throwable || $text instanceof \Exception) {
65
                    $text = (string)$text;
66
                } elseif (\is_array($text)) {
67
                    $ctx = $text;
68
                    if (isset($ctx['message'])) {
69
                        $text = $ctx['message'];
70
                        unset($ctx['message']);
71
72
                        if (isset($ctx['exception'])) {
73
                            $e = $ctx['exception'];
74
                            unset($ctx['exception']);
75
                            $context['exception'] = [
76
                                'message' => $e->getMessage(),
77
                                'line' => $e->getLine(),
78
                                'file' => $e->getFile(),
79
                                'code' => $e->getCode(),
80
                                'trace' => $e->getTrace()
81
                            ];
82
                        }
83
                        foreach ($ctx as $k => $v) {
84
                            $context[$k] = $v;
85
                        }
86
                    } else {
87
                        $text = VarDumper::export($text);
88
                    }
89
                } else {
90
                    $text = VarDumper::export($text);
91
                }
92
            }
93
            
94
            // If the user_id is not passed, dynamically set it from the user identity object
95
            if (!isset($context['user_id'])) {
96
                $context['user_id'] = (isset(Yii::$app) && Yii::$app->has('user') && Yii::$app->user->id != null) ? Yii::$app->user->id : 'system';
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing Yii::app->user->id of type integer|string against null; this is ambiguous if the integer can be zero. Consider using a strict comparison !== instead.
Loading history...
97
            }
98
99
            // If the user_id of the event isn't the system, pre-load the policy number
100
            if (!\in_array($context['user_id'], ['system', null])) {
101
                if (isset(Yii::$app) && Yii::$app->has('user') && Yii::$app->user->id) {
102
                    $model = Yii::$app->user->identity;
0 ignored issues
show
Unused Code introduced by
The assignment to $model is dead and can be removed.
Loading history...
103
                } else {
104
                    $model = Yii::$app->user->identityClass::find()->where(['id' => $context['user_id']])->one();
105
                }
106
            }
107
            
108
            if (!isset($context['timestamp'])) {
109
                $context['timestamp'] = \microtime(true);
110
            }
111
112
            $this->getLogger()->log($this->_psrLevels[$message[1]], $text, $context);
113
        }
114
    }
115
}
116