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

Last change on this file since 7834 was 7803, checked in by ddboer, 15 years ago

added mail to config

  • Property svn:eol-style set to native
File size: 3.5 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 * Actions on log levels
32 */
33define('LOG_LEVEL_ECHO', SYSLOG_DEBUG);
34define('LOG_LEVEL_WRITE', SYSLOG_DEBUG);
35define('LOG_LEVEL_MAIL', SYSLOG_DEBUG);
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 if ($logno <= LOG_LEVEL_MAIL) {
119 global $config;
120 mail($config['mail'], 'Nodemap error', $errorString);
121 }
122 }
123
124 function __destruct() {
125 echo '<!-- ' . $this->logString . ' -->';
126 }
127}
128
129$LOG_HANDLER = new LogHandler();
130
131/*
132 * Function: trigger_log
133 * Parameters: int $logno, string $logstr, string $logfile, int $logline
134 * Function: Handling with log entries, forwarding them to our $LOG_HANDLER object.
135 */
136function trigger_log($logno, $logstr, $logfile, $logline) {
137 global $LOG_HANDLER;
138
139 $LOG_HANDLER->logEntry($logno, $logstr, $logfile, $logline);
140}
141?>
Note: See TracBrowser for help on using the repository browser.