source: trunk/src/inc/LogHandler.class.php@ 7843

Last change on this file since 7843 was 7843, checked in by rick, 15 years ago

Eeks, we sended over a 1000 message to test@…, example.org is the domain to be used for this matter :-)

  • Property svn:eol-style set to native
File size: 3.6 KB
Line 
1<?php
2/*
3 * Project: NodeMap2.0
4 * File: LogHandler.class.php
5 * Purpose: Handling our log file containing log entries generated by the application
6 */
7
8/*
9 * Log levels:
10 * SYSLOG_EMERG System is unusable
11 * SYSLOG_ALERT Action must be taken immediately
12 * SYSLOG_CRIT Critical conditions
13 * SYSLOG_ERR Error conditions
14 * SYSLOG_WARNING Warning conditions
15 * SYSLOG_NOTICE Normal, but significant, condition
16 * SYSLOG_INFO Informational message
17 * SYSLOG_DEBUG Debug-level message
18 */
19
20define('SYSLOG_NONE', 0);
21define('SYSLOG_EMERG', 1);
22define('SYSLOG_ALERT', 2);
23define('SYSLOG_CRIT', 3);
24define('SYSLOG_ERR', 4);
25define('SYSLOG_WARNING', 5);
26define('SYSLOG_NOTICE', 6);
27define('SYSLOG_INFO', 7);
28define('SYSLOG_DEBUG', 8);
29
30/*
31 * XXX: Actions on log levels, needs to go to the config
32 */
33define('LOG_LEVEL_ECHO', SYSLOG_DEBUG);
34define('LOG_LEVEL_WRITE', SYSLOG_DEBUG);
35define('LOG_LEVEL_MAIL', SYSLOG_EMERG);
36
37class LogHandler {
38 private $logString; // For caching purposes
39
40 /*
41 * Function: __construct (constructor)
42 * Parameters: -
43 * Function: Creating a LogHandler object
44 */
45 public function __construct() {
46 $logString = '';
47 }
48
49 /*
50 * Function: logEntry
51 * Parameters: int $logno, string $logstr, string $logfile, int $logline
52 * Function: Handling with log entries, decides what to do with it
53 */
54 public function logEntry($logno, $logstr, $logfile, $logline) {
55 $errorString = 'SysLog ';
56
57 switch ($logno) {
58 case SYSLOG_EMERG:
59 $errorString = 'Emergency';
60 break;
61 case SYSLOG_ALERT:
62 $errorString = 'Alert';
63 break;
64 case SYSLOG_CRIT:
65 $errorString = 'Critical';
66 break;
67 case SYSLOG_ERR:
68 $errorString = 'Error';
69 break;
70 case SYSLOG_WARNING:
71 $errorString = 'Warning';
72 break;
73 case SYSLOG_NOTICE:
74 $errorString = 'Notice';
75 break;
76 case SYSLOG_INFO:
77 $errorString = 'Information';
78 break;
79 case SYSLOG_DEBUG:
80 $errorString = 'Debug';
81 break;
82 default:
83 $errorString = 'SYSLOG_DEFAULT';
84 break;
85 }
86
87 $errorString .= ' (' . $logno . ' | ' . date('Y/d/m H:i:s') . '): ' . $logstr . "\r\n\t" . 'In file "' . $logfile . '" on line "' . $logline . '"' . "\r\n";
88
89 if ($logno <= LOG_LEVEL_ECHO) {
90 $this->logString .= $errorString;
91 }
92 if ((!defined('IGNORE_LOG_LEVEL_WRITE')) && ($logno <= LOG_LEVEL_WRITE)) {
93 $filename = 'logfile.txt';
94 if (is_writable($filename)) {
95
96 //Check if the file can be opened
97 if (!$handle = fopen($filename, 'a')) {
98 define('IGNORE_LOG_LEVEL_WRITE', true);
99 trigger_log(SYSLOG_EMERG, 'Cannot open file "' . $filename . '"', __FILE__, __LINE__);
100 exit;
101 }
102
103 // Write $errorString to file.
104 if (fwrite($handle, $errorString) === FALSE) {
105 define('LOG_LEVEL_WRITE', LOG_LEVEL_NONE);
106 trigger_log(SYSLOG_EMERG, 'Cannot write to "' . $filename . '"', __FILE__, __LINE__);
107 exit;
108 }
109
110 fclose($handle);
111
112 } else {
113 define('IGNORE_LOG_LEVEL_WRITE', true);
114 trigger_log(SYSLOG_EMERG, 'Cannot write to "' . $filename . '"', __FILE__, __LINE__);
115 }
116 }
117
118 /* XXX: Sends email out for every line, instead of all the lines of the current output */
119 if ($logno <= LOG_LEVEL_MAIL) {
120 global $config;
121 @mail($config['mail'], 'Nodemap error', $errorString);
122 }
123 }
124
125 function __destruct() {
126 echo '<!-- ' . $this->logString . ' -->';
127 }
128}
129
130$LOG_HANDLER = new LogHandler();
131
132/*
133 * Function: trigger_log
134 * Parameters: int $logno, string $logstr, string $logfile, int $logline
135 * Function: Handling with log entries, forwarding them to our $LOG_HANDLER object.
136 */
137function trigger_log($logno, $logstr, $logfile, $logline) {
138 global $LOG_HANDLER;
139
140 $LOG_HANDLER->logEntry($logno, $logstr, $logfile, $logline);
141}
142?>
Note: See TracBrowser for help on using the repository browser.