-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMyException.php
127 lines (110 loc) · 2.68 KB
/
MyException.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php if ( ! defined('DENY_ACCESS')) exit('403: No direct file access allowed');
/**
* A Bright CMS
*
* Open source, lightweight, web application framework and content management
* system in PHP.
*
* @package A Bright CMS
* @author Gabriel Liwerant
*/
/**
* MyException Class
*
* @subpackage core
* @author Gabriel Liwerant
*
* @uses Exception
*/
class MyException extends Exception
{
/**
* Holds an instance of the log class for error logging
*
* @var object $_logger
*/
private $_logger;
/**
* Call the parent constructor when we throw a new exception.
*
* @param object $logger_obj
* @param string|void $msg Exception message
* @param integer|void $code Reference code for exception
* @param object|void $previous Previous exception, if one exists
*/
public function __construct($logger_obj, $msg = null, $code = null, $previous = null)
{
$this->_setLogger($logger_obj);
if (PHP_VERSION < 5.3)
{
parent::__construct($msg, $code);
}
else
{
parent::__construct($msg, $code, $previous);
}
}
/**
* Logger Setter
*
* @param object $logger_obj
*
* @return object MyException
*/
private function _setLogger($logger_obj)
{
$this->_logger = $logger_obj;
return $this;
}
/**
* Create error logs for exceptions.
*
* @param object $exception Exception object to use for logging
*
* @return object MyException
*/
public function createLog($exception)
{
if ( ! IS_MODE_LOGGING)
{
return $this;
}
$log_data['message'] = $exception->getMessage();
$log_data['code'] = $exception->getCode();
$log_data['file'] = $exception->getFile();
$log_data['line'] = $exception->getLine();
$log_data['stack_trace']= "\r\n" . $exception->getTraceAsString();
$log_message = null;
foreach ($log_data as $key => $value)
{
$log_message .= $key . ' => ' . $value . ', ';
}
$log_message = rtrim($log_message, ', ');
$this->_logger->writeLogToFile($log_message, 'exception', 'exceptionLog');
return $this;
}
/**
* Caught exceptions are handled here and logged to a file for reference.
*/
public function caughtException()
{
$this
->createLog($this)
->exception_msg = 'Caught ' . $this->getMessage() . '. Exception code: #' . $this->getCode();
require_once TEMPLATE_PATH . '/exception.php';
}
/**
* Uncaught exceptions are handled here.
*
* @param object $e Exception object to handle
*/
public function uncaughtException($e)
{
$this
->createLog($e)
->exception_msg = 'Uncaught ' . $e->getMessage() . '. Exception code: #' . $e->getCode();
require_once TEMPLATE_PATH . '/exception.php';
}
}
// End of MyException Class
/* EOF system/core/MyException.php */