Completed
Pull Request — master (#380)
by Anton
05:26
created

Logger   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 133
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 84%

Importance

Changes 0
Metric Value
dl 0
loc 133
ccs 21
cts 25
cp 0.84
rs 10
c 0
b 0
f 0
wmc 6
lcom 1
cbo 2

4 Methods

Rating   Name   Duplication   Size   Complexity  
A interpolate() 0 11 2
A info() 0 23 2
A log() 0 4 1
A get() 0 4 1
1
<?php
2
/**
3
 * Bluz Framework Component
4
 *
5
 * @copyright Bluz PHP Team
6
 * @link https://github.com/bluzphp/framework
7
 */
8
9
/**
10
 * @namespace
11
 */
12
namespace Bluz\Logger;
13
14
use Bluz\Common\Options;
15
use Psr\Log\AbstractLogger;
16
17
/**
18
 * Logger
19
 *
20
 * @package  Bluz\Logger
21
 * @author   Taras Omelianenko <[email protected]>
22
 * @link     https://github.com/bluzphp/framework/wiki/Logger
23
 */
24
class Logger extends AbstractLogger
25
{
26
    use Options;
27
28
    /**
29
     * @var float start time
30
     */
31
    protected $startTime;
32
33
    /**
34
     * @var float part time
35
     */
36
    protected $timer;
37
38
    /**
39
     * @var integer
40
     */
41
    protected $memory = 0;
42
43
    /**
44
     * @var array list of alerts
45
     */
46
    protected $alert = [];
47
48
    /**
49
     * @var array list of critical
50
     */
51
    protected $critical = [];
52
53
    /**
54
     * @var array list of debug messages
55
     */
56
    protected $debug = [];
57
58
    /**
59
     * @var array list of emergency
60
     */
61
    protected $emergency = [];
62
63
    /**
64
     * @var array list of errors
65
     */
66
    protected $error = [];
67
68
    /**
69
     * @var array list of info
70
     */
71
    protected $info = [];
72
73
    /**
74
     * @var array list of notices
75
     */
76
    protected $notice = [];
77
78
    /**
79
     * @var array list of warnings
80
     */
81
    protected $warning = [];
82
83
    /**
84
     * Interpolates context values into the message placeholders
85
     *
86
     * @param  string $message
87
     * @param  array  $context
88
     * @return string
89
     */
90 32
    protected function interpolate($message, array $context = [])
91
    {
92
        // build a replacement array with braces around the context keys
93 32
        $replace = [];
94 32
        foreach ($context as $key => $val) {
95
            $replace['{' . $key . '}'] = $val;
96
        }
97
98
        // interpolate replacement values into the message and return
99 32
        return strtr($message, $replace);
100
    }
101
102
    /**
103
     * Log info message
104
     *
105
     * @param  string $message
106
     * @param  array  $context
107
     * @return void
108
     */
109 32
    public function info($message, array $context = [])
110
    {
111 32
        $message = $this->interpolate($message, $context);
112
113 32
        if (!$this->startTime) {
114
            $this->startTime = $this->timer = $_SERVER['REQUEST_TIME_FLOAT'] ?? microtime(true);
115
        }
116
117 32
        $curTimer = microtime(true);
118 32
        $curMemory = memory_get_usage();
119
120 32
        $key = sprintf(
121 32
            "%f :: %f :: %d",
122 32
            ($curTimer - $this->startTime),
123 32
            ($curTimer - $this->timer),
124 32
            ($curMemory - $this->memory)
125
        );
126
127 32
        $this->info[$key] = $message;
128
129 32
        $this->timer = $curTimer;
130 32
        $this->memory = $curMemory;
131 32
    }
132
133
    /**
134
     * Logs with an arbitrary level
135
     *
136
     * @param  mixed  $level
137
     * @param  string $message
138
     * @param  array  $context
139
     * @return void
140
     */
141 1
    public function log($level, $message, array $context = [])
142
    {
143 1
        $this->{$level}[] = $this->interpolate($message, $context);
144 1
    }
145
146
    /**
147
     * Get logs records by level
148
     *
149
     * @param  $level
150
     * @return array
151
     */
152
    public function get($level)
153
    {
154
        return $this->{$level};
155
    }
156
}
157