forked from guahan-web/PHP-SimpleQueue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLogger.php
100 lines (90 loc) · 2.28 KB
/
Logger.php
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
<?php
class Logger {
protected $file;
/**
* Constructor attempts to open the connection to the log file
*
* @access public
* @param String $file The name of the file to which to write logs
* @return Logger
*/
public function __construct($file) {
if (FALSE === $fp = fopen($file, 'a+', TRUE)) {
throw new Exception(sprintf('Could not open file [%s] for writing.', $file));
}
$this->file = $fp;
}
/**
* Writes a DEBUG entry to the log if DEBUG has been set
*
* @access public
* @param String $msg The log message
* @param String $override If provided, overrides the "DEBUG" tag on the log
* @return void
*/
public function debug($msg, $override = NULL) {
if (defined('DEBUG') && DEBUG === TRUE) {
$this->writeLn((NULL === $override) ? 'DEBUG' : $override, $msg);
}
}
/**
* Sets a debug entry for the latest MySql error
*
* @access public
* @param String $q The SQL that failed
* @return void
*/
public function debugQuery($q = NULL) {
$this->debug($q, 'SQL');
$this->debug(mysql_error());
}
/**
* Creates a ERROR entry in the log file
*
* @access public
* @param String $msg The log entry
* @return void
*/
public function error($msg) {
$this->writeLn('ERROR', $msg);
}
/**
* Creates a FATAL entry in the log file
*
* @access public
* @param String $msg The log entry
* @return void
*/
public function fatal($msg) {
$this->writeLn('FATAL', $msg);
}
/**
* Creates an INFO entry in the log file
*
* @access public
* @param String $msg The log entry
* @return void
*/
public function info($msg) {
$this->writeLn('INFO', $msg);
}
/**
* Writes a line to the log file
*
* @access protected
* @param String $type The type of log entry
* @param String $msg The log entry
* @return void
*/
protected function writeLn($type, $msg) {
if (NULL === $this->file) {
throw new Exception('QueueLogger not properly initialized. Cannot write line to log file.');
}
$msg = sprintf("%s [%s] %s\n", date('Y-m-d H:i:s'), $type, $msg);
if (FALSE === fwrite($this->file, $msg)) {
throw new Exception(sprintf('QueueLogger could not write line to file. LINE: [%s]', $msg));
}
return TRUE;
}
}
?>