-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayLogger.php
More file actions
167 lines (151 loc) · 4.73 KB
/
Copy pathArrayLogger.php
File metadata and controls
167 lines (151 loc) · 4.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<?php
/**
* ArrayLogger class - ArrayLogger.php file
*
* @author Dmitriy Tyurin <fobia3d@gmail.com>
* @copyright Copyright (c) 2014 Dmitriy Tyurin
*/
namespace Fobia\Debug;
use Psr\Log\AbstractLogger;
use Psr\Log\LogLevel;
/**
* ArrayLogger class
*
* @package Fobia.Debug
*/
class ArrayLogger extends AbstractLogger
{
protected $list = array();
public $level = 0;
public $enableRender = true;
protected $display;
protected $handle;
public function __construct($level = 0)
{
$this->level = $level;
$cli = defined('IS_CLI') ? IS_CLI : !isset($_SERVER['HTTP_HOST']);
if ( $cli && !defined('TEST_BOOTSTRAP_FILE') && !@$_ENV['no_stderr']) {
$this->handle = fopen('php://stderr', 'a+');
}
}
public function log($level, $message, array $context = array())
{
if (self::getLevelCode($level) < $this->level) {
return;
}
$row = array(
'time' => sprintf("%6s", substr(microtime(true) - $_SERVER["REQUEST_TIME_FLOAT"], 0, 6)),
'memory' => sprintf("%6s", round(memory_get_usage() / 1024 / 1024, 2) . 'MB'),
'level' => $level,
'message' => $message,
'context' => ($context) ? json_encode($context) : ''
);
$this->list[] = $row;
if ($this->handle) {
/*
$color = array(
'black' => array('set' => 30, 'unset' => 39),
'red' => array('set' => 31, 'unset' => 39),
'green' => array('set' => 32, 'unset' => 39),
'yellow' => array('set' => 33, 'unset' => 39),
'blue' => array('set' => 34, 'unset' => 39),
'magenta' => array('set' => 35, 'unset' => 39),
'cyan' => array('set' => 36, 'unset' => 39),
'white' => array('set' => 37, 'unset' => 39)
);
*/
switch ($level) {
case 'emergency':
case 'alert':
case 'critical':
case 'error':
$color = 31;
break;
case 'warning':
$color = 33;
break;
case 'notice':
$color = 35;
break;
case 'info':
$color = 36;
break;
case 'debug':
$color = 37;
break;
}
$string = sprintf("%-7s %s %s\n", "[{$row['level']}]", $row['message'], $row['context']);
$string = sprintf("\033[%sm%s\033[%sm", $color, $string, "00");
fwrite($this->handle, $string);
}
return $row;
}
public function getRows()
{
return $this->list;
}
public static function getLevelCode($level)
{
switch ($level) {
case LogLevel::EMERGENCY: return 600;
case LogLevel::ALERT: return 550;
case LogLevel::CRITICAL: return 500;
case LogLevel::ERROR: return 400;
case LogLevel::WARNING: return 300;
case LogLevel::NOTICE: return 250;
case LogLevel::INFO: return 200;
case LogLevel::DEBUG: return 100;
default: return 50;
}
}
public static function getLevelName($code)
{
$code = (int) $code;
if ($code <= 100) {
return LogLevel::DEBUG;
}
if ($code <= 200) {
return LogLevel::INFO;
}
if ($code <= 250) {
return LogLevel::NOTICE;
}
if ($code <= 300) {
return LogLevel::WARNING;
}
if ($code <= 400) {
return LogLevel::ERROR;
}
if ($code <= 500) {
return LogLevel::CRITICAL;
}
if ($code <= 550) {
return LogLevel::ALERT;
}
if ($code <= 600) {
return LogLevel::EMERGENCY;
}
}
public function render($format = 'html')
{
// echo "handle: {$this->handle}; enableRender: {$this->enableRender}" .BR;
if ($this->handle || ! $this->enableRender) {
return;
}
$file = __DIR__ . '/view/bootstrap/' . $format . '.php';
if (!file_exists($file)) {
trigger_error("Файл не найден '{$file}'", E_USER_WARNING);
}
ob_start();
include $file;
return ob_get_clean();
}
public function registry()
{
register_shutdown_function(function(){
register_shutdown_function(function(){
echo $this->render('html');
});
});
}
}