Completed
Push — master ( 59b38c...5d312f )
by Anton
12s
created

Logger::info()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2.0491

Importance

Changes 0
Metric Value
cc 2
eloc 13
nc 2
nop 2
dl 0
loc 22
ccs 10
cts 13
cp 0.7692
crap 2.0491
rs 9.2
c 0
b 0
f 0
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 $start;
32
33
    /**
34
     * @var float part time
35
     */
36
    protected $timer;
37
38
    /**
39
     * @var array list of alerts
40
     */
41
    protected $alert = [];
42
43
    /**
44
     * @var array list of critical
45
     */
46
    protected $critical = [];
47
48
    /**
49
     * @var array list of debug messages
50
     */
51
    protected $debug = [];
52
53
    /**
54
     * @var array list of emergency
55
     */
56
    protected $emergency = [];
57
58
    /**
59
     * @var array list of errors
60
     */
61
    protected $error = [];
62
63
    /**
64
     * @var array list of info
65
     */
66
    protected $info = [];
67
68
    /**
69
     * @var array list of notices
70
     */
71
    protected $notice = [];
72
73
    /**
74
     * @var array list of warnings
75
     */
76
    protected $warning = [];
77
78
    /**
79
     * Interpolates context values into the message placeholders
80
     *
81
     * @param  string $message
82
     * @param  array  $context
83
     * @return string
84
     */
85 32
    protected function interpolate($message, array $context = [])
86
    {
87
        // build a replacement array with braces around the context keys
88 32
        $replace = [];
89 32
        foreach ($context as $key => $val) {
90
            $replace['{' . $key . '}'] = $val;
91
        }
92
93
        // interpolate replacement values into the message and return
94 32
        return strtr($message, $replace);
95
    }
96
97
    /**
98
     * Log info message
99
     *
100
     * @param  string $message
101
     * @param  array  $context
102
     * @return void
103
     */
104 32
    public function info($message, array $context = [])
105
    {
106 32
        $message = $this->interpolate($message, $context);
107
108 32
        if (!$this->start) {
109
            $this->start = $this->timer = $_SERVER['REQUEST_TIME_FLOAT'] ?? microtime(true);
110
        }
111
112
        $curTimer = microtime(true);
113
        $curMemory = ceil((memory_get_usage() / 1024));
114 32
115 32
        $key = sprintf(
116
            "%f :: %f :: %s kb",
117 32
            ($curTimer - $this->start),
118 32
            ($curTimer - $this->timer),
119 32
            $curMemory
120 32
        );
121
122
        $this->info[$key] = $message;
123
124 32
        $this->timer = $curTimer;
125
    }
126 32
127 32
    /**
128
     * Logs with an arbitrary level
129
     *
130
     * @param  mixed  $level
131
     * @param  string $message
132
     * @param  array  $context
133
     * @return void
134
     */
135
    public function log($level, $message, array $context = [])
136
    {
137 1
        $this->{$level}[] = $this->interpolate($message, $context);
138
    }
139 1
140 1
    /**
141
     * Get logs records by level
142
     *
143
     * @param  $level
144
     * @return array
145
     */
146
    public function get($level)
147
    {
148
        return $this->{$level};
149
    }
150
}
151