Clean code
[irreco.git] / database / php / irreco_webdb_log.php
blob0a2f2d9bf328d61628fe02e6d74d784cb716af60
1 <?php
3 require_once 'PEAR.php';
4 require_once 'Log.php';
6 class IrrecoLog
8 private static $irreco_log;
9 private static $php_level;
11 public static $critical;
12 public static $php;
13 public static $exception;
14 public static $pear;
15 public static $index;
16 public static $database;
17 public static $server;
19 public static function getVarDump(&$var)
21 ob_start();
22 var_dump($var);
23 return ob_get_clean();
26 public static function init()
28 $file = 'webdb.log';
29 $conf = array('mode' => 0644,
30 'timeFormat' => '%Y-%m-%d %T');
32 /* Critical. */
33 if (!is_object(self::$critical)) {
34 self::$critical = Log::singleton(
35 'file', $file, NULL, $conf);
38 /* Setup PHP error logging. */
39 if (!is_object(self::$php)) {
40 self::$php = Log::singleton(
41 'file', $file, 'PHP', $conf);
42 set_error_handler(
43 array('IrrecoLog', 'phpErrorHandler'));
46 /* Setup PHP exception error logging. */
47 if (!is_object(self::$exception)) {
48 self::$exception = Log::singleton(
49 'file', $file, 'EXCEPTION', $conf);
50 set_exception_handler(
51 array('IrrecoLog', 'phpExceptionHandler'));
54 /* Setup PEAR error logging. */
55 if (!is_object(self::$pear)) {
56 self::$pear = Log::singleton(
57 'file', $file, 'PEAR', $conf);
58 PEAR::setErrorHandling(PEAR_ERROR_CALLBACK,
59 array('IrrecoLog', 'pearErrorHandler'));
62 /* Index */
63 if (!is_object(self::$index)) {
64 self::$index = Log::singleton(
65 'file', $file, 'Index', $conf);
68 /* Database */
69 if (!is_object(self::$database)) {
70 self::$database = Log::singleton(
71 'file', $file, 'Database', $conf);
74 /* Server */
75 if (!is_object(self::$server)) {
76 self::$server = Log::singleton(
77 'file', $file, 'Server', $conf);
81 /**
82 * Log critical error, and exit.
84 public static function critical($message)
86 IrrecoLog::$critical->log($message, PEAR_LOG_CRIT);
87 die($message);
90 /**
91 * PHP error handler for saving errors to Log.
93 public static function phpErrorHandler($code, $message, $file, $line)
95 if ($code > self::$php_level) return;
97 /* Map the PHP error to a Log priority. */
98 switch ($code) {
99 case E_WARNING:
100 case E_USER_WARNING:
101 $priority = PEAR_LOG_WARNING;
102 break;
104 case E_NOTICE:
105 case E_USER_NOTICE:
106 $priority = PEAR_LOG_NOTICE;
107 break;
109 case E_ERROR:
110 case E_USER_ERROR:
111 $priority = PEAR_LOG_ERR;
112 break;
114 default:
115 $priority = PEAR_LOG_INFO;
116 break;
119 IrrecoLog::$php->log('[' . $code . '] ' . $message . ' in ' .
120 $file . ' at line ' . $line . '.',
121 $priority);
125 * PHP exception handler for saving uncaught exceptions to log.
127 public static function phpExceptionHandler($exception)
129 IrrecoLog::$exception->log($exception->getMessage(),
130 PEAR_LOG_ALERT);
134 * PEAR error handler for saving errors to Log.
136 public static function pearErrorHandler($error)
138 $message = $error->getMessage();
140 if (!empty($error->backtrace[1]['file'])) {
141 $message .= ' (' . $error->backtrace[1]['file'];
142 if (!empty($error->backtrace[1]['line'])) {
143 $message .= ' at line ';
144 $message .= $error->backtrace[1]['line'];
146 $message .= ')';
149 $log = IrrecoLog::$pear;
151 /* Make sure the error code is a valid PEAR error code. */
152 switch ($error->code) {
153 case PEAR_LOG_EMERG:
154 case PEAR_LOG_ALERT:
155 case PEAR_LOG_CRIT:
156 case PEAR_LOG_ERR:
157 case PEAR_LOG_WARNING:
158 case PEAR_LOG_NOTICE:
159 case PEAR_LOG_INFO:
160 case PEAR_LOG_DEBUG:
161 $code = $error->code;
162 break;
164 default:
165 $message = '[' . $error->code . '] ' . $message;
166 $code = PEAR_LOG_NOTICE;
169 $log->log($message, $code);
173 * Set PEAR error reporting level.
175 * @param $level Errors of this level or more signifacant are logged.
177 public static function pearLevel($level)
179 $mask = IrrecoLog::$database->MAX($level);
181 IrrecoLog::$critical->setMask($mask);
182 IrrecoLog::$php->setMask($mask);
183 IrrecoLog::$exception->setMask($mask);
184 IrrecoLog::$pear->setMask($mask);
185 IrrecoLog::$index->setMask($mask);
186 IrrecoLog::$database->setMask($mask);
187 IrrecoLog::$server->setMask($mask);
191 * PHP logging level.
193 * @param $level Errors of this level or more signifacant are logged.
195 public static function phpLevel($level)
197 self::$php_level = $level;
198 error_reporting($level);