Odyssey
hcuLogError.i
1 <?php
2 /**
3  *
4  * Log Error and Exception Handling Class
5  *
6  * Required
7  * hcuCommon.i
8  * logging.i
9  *
10  */
11 
12 
13 require_once(dirname(__FILE__) . '/../../shared/library/logging.i');
14 require_once(dirname(__FILE__) . '/../../shared/library/hcuCommon.i');
15 
16 define('HCU_PRODUCT_BANKING', 0);
17 define('HCU_PRODUCT_ADMIN', 1);
18 define('HCU_PRODUCT_MONITOR', 2);
19 
20 abstract class CatchHandler {
21 
22  /**
23  * PROPERTIES
24  */
25 
26  /**
27  * The logger object used for writing to error log
28  */
29  protected $errLogger = null;
30  protected $errReportLvl = E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED;
31 
32  /**
33  * private @var boolean $saveLog -- By default does the log get saved when this class handles a raised error
34  * private @var boolean $showErrPage -- Redirect to an error page?
35  *
36  * private @var string $errPageUrl
37  */
38  private $saveLog = false;
39  private $showErrPage = false;
40 
41  private $errPageUrl = '';
42 
43  /**
44  * private @var boolean $noticeOnly -- {true, false(default)}Should the message be printed to the console as a NOTICE
45  */
46  private $noticeOnly = false;
47  /**
48  *
49  * private @var integer $hcuProduct -- This is the homecu product that has called raised the error
50  * {HCU_PRODUCT_MONITOR, HCU_PRODUCT_BANKING, HCU_PRODUCT_ADMIN}
51  *
52  */
53  private $hcuProduct = null;
54 
55 
56  /**
57  *
58  * private @var integer $hcuDevMode -- This will tell us if we are in Dev Mode or Production
59  * 1 -- dev mode is enabled, log files may have more information
60  * 0 -- production mode
61  */
62  private $hcuDevMode = 0;
63 
64  /**
65  * Values from the PHP _SERVER structure
66  * protected @var string $errRemoteAddr _SERVER REMOTE_ADDR
67  * protected @var string $errRequestUri _SERVER REQUEST_URI
68  * protected @var string $errServerName _SERVER SERVER_NAME
69  * protected @var string $errPhpSelf _SERVER PHP_SELF
70  * protected @var string $errScriptName _SERVER SCRIPT_NAME
71  * protected @var string $errQueryString _SERVER QUERY_STRING
72  *
73  */
74  protected $errRemoteAddr;
75  protected $errRequestUri;
76  protected $errServerName;
77  protected $errPhpSelf;
78  protected $errScriptName;
79  protected $errQueryString;
80 
81  /**
82  *
83  * PARENT CONSTRUCTOR
84  *
85  */
86  function __construct($pSetProduct, $pSetLogger=null) {
87 
88  $this->hcuProduct = $pSetProduct;
89 
90  /**
91  * Set the logger object if it is defined
92  */
93  $this->setLogger($pSetLogger);
94  $this->setErrorReporting($this->errReportLvl);
95 
96  }
97 
98  /**
99  *
100  * PRIVATE METHODS
101  *
102  */
103 
104  /**
105  * Save the log message for the error being raised
106  *
107  */
108  private function SaveLogMsg() {
109 
110  $lLogMsg = $this->getErrMsg();
111  //** If this->errLogger is set then use this for logging the error,
112  // * If not, then save to the default error_log
113  if (!is_null($this->errLogger)) {
114  if ($this->noticeOnly) {
115  // * Default error log destination
116  $this->errLogger->notice($lLogMsg);
117  } else {
118  // * Default error log destination
119  $this->errLogger->error($lLogMsg);
120  }
121 
122  } else {
123  // ** logger not set, save with built in error_log function
124  error_log($lLogMsg);
125  }
126  }
127 
128  /**
129  *
130  * PROTECTED METHODS
131  *
132  */
133  protected function handleError() {
134 
135  /**
136  * Record the _SERVER variables
137  */
138 
139  $this->errRemoteAddr = (array_key_exists("REMOTE_ADDR", $_SERVER) ? $_SERVER['REMOTE_ADDR'] : '0.0.0.0');
140  $this->errRequestUri = (array_key_exists("REQUEST_URI", $_SERVER) ? $_SERVER['REQUEST_URI'] : '127.0.0.1');
141  $this->errServerName = (array_key_exists("SERVER_NAME", $_SERVER) ? $_SERVER['SERVER_NAME'] : '');
142  $this->errPhpSelf = (array_key_exists("PHP_SELF", $_SERVER) ? $_SERVER['PHP_SELF'] : '');
143  $this->errScriptFileName = (array_key_exists("SCRIPT_FILENAME", $_SERVER) ? $_SERVER['SCRIPT_FILENAME'] : __FILE__);
144  $this->errQueryString = (array_key_exists("QUERY_STRING", $_SERVER) ? $_SERVER['QUERY_STRING'] : '');
145 
146  if ($this->saveLog) {
147  $this->SaveLogMsg();
148  }
149  }
150 
151  protected function setSaveLog($pSaveLog) {
152  $this->saveLog = $pSaveLog;
153  }
154  protected function getErrMsg() {
155  }
156 
157  protected function setNotice($pNoticeOnly) {
158  $this->noticeOnly = $pNoticeOnly;
159  }
160 
161 
162  /**
163  *
164  * PUBLIC METHODS
165  *
166  */
167  /**
168  *
169  * this will set the logger object for the current error exception handler
170  * @param object $pLogger This is an instance of the ColorStandardLogger object
171  *
172  */
173  public function setLogger($pLogger) {
174 
175  if (is_a($pLogger, 'StandardLogger')) {
176  $this->errLogger = $pLogger;
177  }
178  }
179 
180  public function setDevMode($pDevMode) {
181  $this->hcuDevMode = $pDevMode;
182 
183 
184  if ($pDevMode) {
185  // ** DEV MODE -- ADD E_NOTICE
186  $this->setErrorReporting($this->getErrorReporting() | E_NOTICE);
187  }
188  }
189  public function getDevMode() {
190  return $this->hcuDevMode;
191  }
192 
193  /**
194  *
195  * Set the level of php debugging
196  * @param int The integer value representing the error reporting level
197  *
198  * @return int the previous value of error reporting
199  *
200  */
201  public function setErrorReporting($pNewReportLvl) {
202  $this->errReportLvl = $pNewReportLvl;
203  return error_reporting($pNewReportLvl);
204  }
205 
206  /**
207  *
208  * Get the current level of php debugging
209  *
210  * @return int the previous value of error reporting
211  *
212  */
213  public function getErrorReporting() {
214  return $this->errReportLvl;
215  }
216 }
217 
219 
220  /**
221  * @var object $errException This is the error exception class that is thrown to catchHandler
222  */
223  private $errException = null;
224 
225  private function getSaveLog() {
226  return true;
227  }
228 
229  function __construct($pSetProduct, $pSetLogger=null, $pDevMode=false) {
230  parent::__construct($pSetProduct, $pSetLogger);
231 
232  $this->setDevMode($pDevMode);
233  }
234 
235  public function catchException($pException) {
236 
237  $this->logException($pException);
238 
239  /**
240  *
241  * Exception was triggered
242  * Include an error handling script that will be presented to the user
243  *
244  */
245 
246  include_once(dirname(__FILE__) . '/../../shared/includes/hcuBadRequest.i');
247  exit;
248 
249  }
250  /**
251  * This will log the exception information to the specified error log
252  *
253  * @param object $pException This is the exception object that was thrown
254  *
255  */
256  public function logException($pException) {
257 
258  $this->errException = $pException;
259 
260  $this->setSaveLog($this->getSaveLog());
261 
262  parent::handleError();
263 
264  }
265 
266 
267  /**
268  *
269  * Build the error message from the exception information
270  * Specific to the exception object
271  *
272  * @param int $pType - (0-Format error for Log File, 1-Format error for e-mail)
273  *
274  * @return string Returns formatted error string
275  */
276  protected function getErrMsg() {
277  $lRetStr = '';
278  $lCurDate = date("Ymd:His");
279 
280  $lRetStr = <<< LOGMSG
281 [{$lCurDate}] {$this->errException->getCode()} [{$this->errRemoteAddr}] [{$this->errServerName}{$this->errPhpSelf}?{$this->errQueryString}] [{$this->errException->getFile()}] [Line {$this->errException->getLine()}] [{$this->errException->getCode()}] {$this->errException->getMessage()}
282 LOGMSG;
283 
284  if ($this->getDevMode()) {
285  /**
286  * For dev mode add extra informaition to the log file
287  *
288  */
289  $lRetStr .= $this->errException->getTraceAsString();
290  }
291  return $lRetStr;
292  }
293 }
294 
295 class CatchErrorHandler extends CatchHandler {
296 
297  /**
298  * @var string $errNo The Error Number sent to the error handler
299  */
300  private $errNo;
301  /**
302  * @var string $errStr The Error String sent to the error handler
303  */
304  private $errStr;
305  /**
306  * @var string $errFile The File Information where the error was raised
307  */
308  private $errFile;
309  /**
310  * @var string $errLine The line number in the file that raised the error
311  */
312  private $errLine;
313  /**
314  * @var string $errType A printable representation of the errNo
315  */
316  private $errType;
317 
318  /**
319  * @var array $errConstants A list to translate the error numbers into readable string
320  */
321  private $errConstants = Array (
322  "1" => "[E_ERROR]",
323  "2" => "[E_WARNING]",
324  "4" => "[E_PARSE]",
325  "8" => "[E_NOTICE]",
326  "16" => "[E_CORE_ERROR]",
327  "32" => "[E_CORE_WARNING]",
328  "64" => "[E_COMPILE_ERROR]",
329  "128" => "[E_COMPILE_WARNING]",
330  "256" => "[E_USER_ERROR]",
331  "512" => "[E_USER_WARNING]",
332  "1024" => "[E_USER_NOTICE]",
333  "2048" => "[E_STRICT]",
334  "4096" => "[E_RECOVERABLE_ERROR]",
335  "8192" => "[E_DEPRECATED]",
336  "16384" => "[E_USER_DEPRECATED]",
337  "32767" => "[E_ALL]"
338  );
339 
340  /**
341  * Default list of files to ignore errors
342  */
343  private $ignoreFileErrors = Array(
344  "db.postgres.i"
345  );
346 
347 
348  /**
349  * Default list of error strings to ignore
350  */
351  private $ignoreErrorString = Array(
352  "file-get-contents", "SSL: fatal protocol"
353  );
354 
355  /**
356  *
357  * determine if we will save the log file
358  * This will examine the {ignored files/error string} arrays and determine
359  * if the log should be saved
360  *
361  * @return boolean - {true/false} true if the log should be saved
362  *
363  */
364  private function getSaveLog() {
365  $retVal = false;
366  parent::setNotice(false);
367 
368  // * Verify we want to report the error based on the Error
369  if (($this->getErrorReporting() & $this->errNo) > 0) {
370  switch ($this->errNo) {
371  case E_NOTICE:
372  // ** for E_NOTICE -- set the flag to show the errors as a notice
373  parent::setNotice(true);
374 
375  case E_USER_ERROR:
376  case E_USER_WARNING:
377  case E_USER_NOTICE:
378 
379  case E_ERROR:
380 
381  case E_PARSE:
382 
383  case E_DEPRECATED:
384  case E_USER_DEPRECATED:
385  $retVal = true;
386  break;
387  case E_WARNING:
388 
389  // ** check to see if the file raising the error is in the exception list
390  // Determine if it the file exists in the ignoreFileList array
391 
392  $retVal = (in_array(basename($this->errFile), $this->ignoreFileErrors) === false);
393 
394  // * ONLY consider the next check if retVal is true, if false, then skip
395  if ($retVal) {
396  $retVal = (in_array($this->errStr, $this->ignoreErrorString) === false);
397  }
398  }
399  }
400  return $retVal;
401  }
402 
403  function __construct($pSetProduct, $pSetLogger=null, $pDevMode=false) {
404  parent::__construct($pSetProduct, $pSetLogger);
405 
406  $this->setDevMode($pDevMode);
407  }
408 
409 
410  public function catchError($pErrNo, $pErrStr, $pErrFile, $pErrLine) {
411  // ** Do what I need to do here to setup common details about the error that I want to capture
412  // ** Include the back trace as well.
413  $this->errNo = $pErrNo;
414  $this->errStr = $pErrStr;
415  $this->errFile = $pErrFile;
416  $this->errLine = $pErrLine;
417  $this->errType = (array_key_exists($pErrNo, $this->errConstants) ? $this->errConstants[$pErrNo] : "UNKNOWN");
418 
419  $this->setSaveLog($this->getSaveLog());
420 
421  parent::handleError();
422 
423  }
424 
425  /**
426  *
427  * Build the error message from the exception information
428  * Specific to the fields that were passed into the Error Handler
429  *
430  * @return string Returns formatted error string
431  */
432  public function getErrMsg() {
433  $lRetStr = "";
434  $lCurDate = date("Ymd:His");
435  $lCurDate = date("d/M/Y:H:i:s");
436 
437  // ** Format for Log File
438  $lRetStr = <<< LOGMSG
439 [{$lCurDate}] $this->errType [{$this->errRemoteAddr}] [{$this->errServerName}{$this->errPhpSelf}?{$this->errQueryString}] [$this->errFile] [Line $this->errLine] [$this->errNo] $this->errStr
440 LOGMSG;
441 
442  // ** At this time I don't see a need to add trace to an error log
443 
444  return $lRetStr;
445  }
446 
447  /**
448  * Add a file to the ignore list. This may be added to ignore a particular script on the fly
449  *
450  * @param string $pFileName - The name of the file to be added to the array
451  */
452  public function addFileIgnore($pFileName) {
453  $this->ignoreFileErrors[] = $pFileName;
454  }
455 
456  /**
457  * Add a file to the ignore list. This may be added to ignore a particular script on the fly
458  *
459  * @param string $pFileName - The name of the file to be added to the array
460  */
461  public function addErrStrIgnore($pFileName) {
462  $this->ignoreErrorString[] = $pFileName;
463  }
464 }
setLogger($pLogger)
Definition: hcuLogError.i:173
logException($pException)
Definition: hcuLogError.i:256
__construct($pSetProduct, $pSetLogger=null)
Definition: hcuLogError.i:86
catchException($pException)
Definition: hcuLogError.i:235
addErrStrIgnore($pFileName)
Definition: hcuLogError.i:459
setErrorReporting($pNewReportLvl)
Definition: hcuLogError.i:201
addFileIgnore($pFileName)
Definition: hcuLogError.i:450