-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogEx.php
More file actions
150 lines (131 loc) · 3.77 KB
/
Copy pathLogEx.php
File metadata and controls
150 lines (131 loc) · 3.77 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
<?php
namespace logex;
class LogEx
{
const L_ALL = 4;
const L_DEBUG = 3;
const L_INFO = 2;
const L_WARN = 1;
const L_ERROR = 0;
const LEVEL = array(
'ALL' => 4,
'DEBUG' => 3,
'INFO' => 2,
'WARNING' => 1,
'ERROR' => 0
);
private static $instance;
private static $level = self::L_ERROR;
private static $logfile = 'debug.log';
public $is_pid = FALSE;
private function __construct() {}
private function __clone() {}
private function __wakeup() {}
public static function getInstance()
{
if ( empty(self::$instance) )
{
self::$instance = new self();
if(!defined('STDIN')) { define('STDIN', fopen('php://stdin', 'r')); }
if(!defined('STDOUT')) { define('STDOUT', fopen('php://stdout', 'w')); }
if(!defined('STDERR')) { define('STDERR', fopen('php://stderr', 'w')); }
}
return self::$instance;
}
public static function setLevel($level)
{
if ($level < 0 || $level > 4) {
self::$level = self::L_ERROR;
} else {
self::$level = $level;
}
}
public static function setLogFile($logfile)
{
// :TODO: Сделать проверку на возможность записи файла
self::$logfile = $logfile;
}
public static function getLogFile()
{
return self::$logfile;
}
public static function clear()
{
if (self::$logfile == '-') { return; }
$fh = fopen(self::$logfile, 'w');
fclose($fh);
}
public function log($level, $message)
{
if (!isset($message) || !$message || !preg_match('/^\d+$/', $level)) {
return FALSE;
}
$this->isCorrectLog();
if ($level < 0 || $level > 4) { $level = 0; }
if ($level <= self::$level) {
switch ($level)
{
case self::L_ERROR:
$prefix = "ERROR";
break;
case self::L_WARN:
$prefix = "WARNING";
break;
case self::L_INFO:
$prefix = "INFO";
break;
case self::L_DEBUG:
$prefix = "DEBUG";
break;
case self::L_ALL:
$prefix = "ALL";
break;
default:
$prefix = "UNKNOWN";
break;
}
$stamp = date("[d-m-Y H:i:s]");
preg_replace('/[\t\s\n]+/', ' ', $message);
$message = "$stamp" . ($this->is_pid == 0 ? "" : " [" . getmypid() . "]" ) . " $prefix: $message\n";
if (self::$logfile == '-') {
fwrite(STDERR, $message);
} else {
$fh = fopen(self::$logfile, 'a');
fwrite($fh, $message);
fclose($fh);
if ($level == self::L_ERROR)
fwrite(STDERR, $message);
}
}
if ($level == self::L_ERROR) { exit(2); }
}
public function error($message)
{
$this->log(self::L_ERROR, $message);
}
public function warn($message)
{
$this->log(self::L_WARN, $message);
}
public function info($message)
{
$this->log(self::L_INFO, $message);
}
public function debug($message)
{
$this->log(self::L_DEBUG, $message);
}
private function isCorrectLog()
{
if (self::$logfile == "-") { return; }
if (!file_exists(self::$logfile) && !is_writable(dirname(self::$logfile))) {
self::$logfile = "-";
$this->log(self::L_WARN, "Cannot create the log file, changing to STDERR.");
}
if (file_exists(self::$logfile) && !is_writable(self::$logfile)) {
self::$logfile = "-";
$this->log(self::L_WARN, "The log file is NOT writable, changing to STDERR.");
}
}
}
?>