Repository navigation
Expand file tree
/
Copy pathamon.php
More file actions
185 lines (147 loc) · 5.24 KB
/
Copy pathamon.php
File metadata and controls
185 lines (147 loc) · 5.24 KB
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<?php
require_once dirname(__FILE__)."/amon/data.php";
require_once dirname(__FILE__)."/amon/errors.php";
require_once dirname(__FILE__)."/amon/http.php";
require_once dirname(__FILE__)."/amon/zeromq.php";
class Amon
{
static $exceptions;
static $previous_exception_handler;
static $previous_error_handler;
static $controller;
static $action;
static $config_array;
/**
* Overwrite the default configuration
* Amon::config(array('address', 'secret_key','protocol'))
*
*/
public static function config($array)
{
self::$config_array = (object)$array;
}
/** Create a config array with some defaults */
private static function _get_config_object()
{
if(empty(self::$config_array))
{
// Defaults
$config = array("address" => 'http://127.0.0.1:2464', "protocol" => 'http');
}
else
{
$config = self::$config_array;
}
return $config;
}
/**
* Log!
*
* @param string $message
* @param string $level
*
* @return void
*/
public static function log($message, $tags='')
{
$data = array(
'message' => $message,
'tags' => $tags
);
$config = self::_get_config_object();
$log_url = sprintf("%s/api/log", $config->address);
if(property_exists($config, 'secret_key')){
$log_url = sprintf("%s/%s", $log_url, $config->secret_key);
}
if($config->protocol == 'http') {
AmonHTTP::request($log_url, $data);
} else if($config->protocol == 'zeromq') {
$requester = AmonZeroMQ::getInstance($config->address);
$zeromq_data = array('content' => $data,
'type' => 'log');
if(property_exists($config, 'secret_key')){
$zeromq_data['secret_key'] = $config->secret_key;
}
$requester->post($zeromq_data);
}
}
static function setup_exception_handler()
{
self::$exceptions = array();
self::$action = "";
self::$controller = "";
// set exception handler & keep old exception handler around
self::$previous_exception_handler = set_exception_handler(
array("Amon", "handle_exception")
);
self::$previous_error_handler = set_error_handler(
array("Amon", "handle_error")
);
register_shutdown_function(
array("Amon", "shutdown")
);
}
static function shutdown()
{
if ($e = error_get_last())
{
self::handle_error($e["type"], $e["message"], $e["file"], $e["line"]);
}
}
static function handle_error($errno, $errstr, $errfile, $errline)
{
if (!(error_reporting() & $errno)) {
return;
}
switch ($errno) {
case E_NOTICE:
case E_USER_NOTICE:
$ex = new PhpNotice($errstr, $errno, $errfile, $errline);
break;
case E_WARNING:
case E_USER_WARNING:
$ex = new PhpWarning($errstr, $errno, $errfile, $errline);
break;
case E_STRICT:
$ex = new PhpStrict($errstr, $errno, $errfile, $errline);
break;
case E_PARSE:
$ex = new PhpParse($errstr, $errno, $errfile, $errline);
break;
default:
$ex = new PhpError($errstr, $errno, $errfile, $errline);
}
self::handle_exception($ex, false);
if (self::$previous_error_handler) {
call_user_func(self::$previous_error_handler, $errno, $errstr, $errfile, $errline);
}
}
/*
* Exception handle class. Pushes the current exception onto the exception
* stack and calls the previous handler, if it exists. Ensures seamless
* integration.
*/
static function handle_exception($exception, $call_previous = true)
{
$config = self::_get_config_object();
self::$exceptions[] = $exception;
$data = new AmonData($exception);
if($config->protocol == 'http') {
$exception_url = sprintf("%s/api/exception", $config->address);
if(property_exists($config, 'secret_key')){
$exception_url = sprintf("%s/%s", $exception_url, $config->secret_key);
}
AmonHTTP::request($exception_url, $data->data);
} else if($config->protocol == 'zeromq') {
$zeromq_data = array('content'=> $data->data, 'type' => 'exception');
if(property_exists($config, 'secret_key')){
$zeromq_data['secret_key'] = $config->secret_key;
}
AmonZeroMQ::getInstance($config->address)->post($zeromq_data);
}
// if there's a previous exception handler, we call that as well
if ($call_previous && self::$previous_exception_handler) {
call_user_func(self::$previous_exception_handler, $exception);
}
}
}