-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogger.class.php
More file actions
106 lines (84 loc) · 2.35 KB
/
Copy pathLogger.class.php
File metadata and controls
106 lines (84 loc) · 2.35 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
<?php
/**
* Logger class. This will write out input to a log file
*/
class Logger {
const SEVERITY_DEBUG = 0;
const SEVERITY_INFO = 1;
const SEVERITY_WARN = 2;
const SEVERITY_ERROR = 3;
const SEVERITY_FATAL = 4;
var $severities = array(
Logger::SEVERITY_DEBUG => 'DEBUG',
Logger::SEVERITY_INFO => 'INFO',
Logger::SEVERITY_WARN => 'WARN',
Logger::SEVERITY_ERROR => 'ERROR',
Logger::SEVERITY_FATAL => 'FATAL'
);
function debug($message) {
$this->log(Logger::SEVERITY_DEBUG, $message);
}
function info($message) {
$this->log(Logger::SEVERITY_INFO, $message);
}
function warn($message) {
$this->log(Logger::SEVERITY_WARN, $message);
}
function error($message) {
$this->log(Logger::SEVERITY_ERROR, $message);
}
function fatal($message) {
$this->log(Logger::SEVERITY_FATAL, $message);
}
private function log($severity, $message) {
if (LOG_LEVEL > $severity) {
return;
}
$this->openLog();
$date = date(DATE_FORMAT);
$fn = $this->getCallingFunction();
$toWrite = sprintf('%1$s - %2$5s - %3$s - %4$s%5$s', $date, $this->severities[$severity], $fn, $message, LINE_BREAK);
fwrite($this->file, $toWrite);
$this->closeLog();
$this->rotateLog();
}
private function openLog() {
$this->file = fopen(LOG_FILE, 'ab');
}
private function closeLog() {
fclose($this->file);
}
private function rotateLog() {
if (LOG_MAX_SIZE > 0) {
$bytes = LOG_MAX_SIZE * 1024; // convert KB to bytes
$size = filesize(LOG_FILE);
if ($size > $bytes) {
if (MAX_LOG_BACKUPS > 0) {
if (file_exists(LOG_FILE . '.' . MAX_LOG_BACKUPS)) {
unlink(LOG_FILE . '.' . MAX_LOG_BACKUPS);
}
for ($i = MAX_LOG_BACKUPS - 1; $i > 0; $i--) {
$filename = LOG_FILE . '.' . $i;
if (file_exists($filename)) {
rename($filename, LOG_FILE . '.' . ($i + 1));
}
}
rename(LOG_FILE, LOG_FILE . '.1');
}
else {
unlink(LOG_FILE);
}
}
}
}
private function getCallingFunction() {
$trace = debug_backtrace(FALSE);
// $trace[0] will be this function
// $trace[1] will be log()
// $trace[2] will be (e.g.) debug()
// $trace[3] will be the actual function
$fn = $trace[3];
return $fn['class'] . $fn['type'] . $fn['function'] . '()';
}
}
?>