forked from dimitri/libphp-pgq
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleLogger.php
More file actions
212 lines (177 loc) · 4.62 KB
/
SimpleLogger.php
File metadata and controls
212 lines (177 loc) · 4.62 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
<?php
// log levels
define("FATAL", 60);
define("ERROR", 50);
define("WARNING", 40);
define("NOTICE", 30);
define("VERBOSE", 20);
define("DEBUG", 10);
defined("DEFAULT_TZ") || define("DEFAULT_TZ", "Europe/Paris");
class SimpleLogger
{
/**
* @var resource
*/
private $logfile_fd = false;
/**
* @var int
*/
public $loglevel = WARNING;
/**
* @var string
*/
public $logfile;
/**
* @param int $loglevel
* @param string $logfile
*/
public function __construct($loglevel, $logfile)
{
$this->loglevel = $loglevel;
if (empty($logfile)) {
$this->logfile = tempnam("/tmp", "SimpleLogger-");
if ($this->logfile !== false) {
rename($this->logfile, sprintf("%s.log", $this->logfile));
}
} else {
$this->logfile = $logfile;
}
date_default_timezone_set(DEFAULT_TZ);
$this->open();
}
public function __destruct()
{
/* Only close the logfile when we opened it ourselves */
if (!is_resource($this->logfile)) {
$this->notice("Closing log file " . $this->logfile);
fclose($this->logfile_fd);
}
}
/**
* Opens the given filename, or use the given stream resource (STDOUT)
*/
private function open()
{
if (is_resource($this->logfile)) {
$this->logfile_fd = $this->logfile;
} else {
$this->logfile_fd = fopen($this->logfile, "a+");
}
if ($this->logfile_fd === false) {
fprintf(STDERR, "FATAL: couldn't open '%s' \n", $this->logfile);
} else {
$this->notice("Logging to file '%s'", $this->logfile);
}
}
/**
* At reload time, don't forget to reopen $this->logfile
* This allows for log rotating.
*/
public function reopen()
{
$this->warning("Closing log file " . $this->logfile);
fclose($this->logfile_fd);
$this->logfile_fd = false;
$this->open();
}
/**
* Check that the logfile has been opened with success.
*
* @return bool
*/
public function check()
{
return $this->logfile_fd !== false;
}
public function debug()
{
$args = func_get_args();
$this->_log(DEBUG, $args);
}
public function verbose()
{
$args = func_get_args();
$this->_log(VERBOSE, $args);
}
public function notice()
{
$args = func_get_args();
$this->_log(NOTICE, $args);
}
public function warning()
{
$args = func_get_args();
$this->_log(WARNING, $args);
}
public function error()
{
$args = func_get_args();
$this->_log(ERROR, $args);
}
public function fatal()
{
$args = func_get_args();
$this->_log(FATAL, $args);
}
/**
* @param int $level
* @param array $args
*/
function _log($level, $args)
{
if ($level >= $this->loglevel) {
$format = array_shift($args);
$date = date("Y-m-d H:i:s");
$vargs = array_merge(array($date, $this->strlevel($level)), $args);
$mesg = vsprintf("%s\t%s\t" . $format . "\n", $vargs);
fwrite($this->logfile_fd, $mesg);
}
}
/**
* @param int $level
*
* @return string
*/
public function strlevel($level)
{
switch ($level) {
case DEBUG:
return "DEBUG";
break;
case VERBOSE:
return "VERBOSE";
break;
case NOTICE:
return "NOTICE";
break;
case WARNING:
return "WARNING";
break;
case ERROR:
return "ERROR";
break;
case FATAL:
return "FATAL";
break;
default:
return $level;
}
}
// On the fly log level control utility functions
public function logless()
{
$this->_log($this->loglevel, array("Incrementing loglevel"));
if ($this->loglevel < FATAL) {
$this->loglevel += 10;
}
$this->_log($this->loglevel, array("loglevel is now %s", $this->strlevel($this->loglevel)));
}
public function logmore()
{
$this->_log($this->loglevel, array("Decrementing loglevel"));
if ($this->loglevel > DEBUG) {
$this->loglevel -= 10;
}
$this->_log($this->loglevel, array("loglevel is now %s", $this->strlevel($this->loglevel)));
}
}