-
Notifications
You must be signed in to change notification settings - Fork 0
/
eventlogger.php
236 lines (202 loc) · 4.91 KB
/
eventlogger.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
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
<?php
/**
* This is a simple API wrapper for EventLog (http://eventlogapp.com)
*
* Basic Usage:
*
* $logger = new EventLogger('[email protected]', 'password', 'your_app_api_key');
* $logger->log('Something went horribly wrong!');
*
* @package EventLogger
* @author Dan Horrigan
* @copyright 2011 Dan Horrigan
* @license MIT License
*/
class EventLogger {
const ERROR = 1;
const WARNING = 2;
const NOTICE = 3;
const SUCCESS = 4;
const GENERAL = 5;
/**
* Holds the Curl handle.
*
* @var handle
*/
protected $curl = null;
/**
* The log queue
*
* @var array
*/
protected $logs = array();
/**
* The log levels -- used for sending multiple logs at a time
*
* @var array
*/
protected $levels = array();
/**
* The EventLog username
*
* @var string
*/
protected $username = '';
/**
* The password for EventLog
*
* @var string
*/
protected $password = '';
/**
* The API Key
*
* @var string
*/
protected $api_key = '';
/**
* The API Url
*
* @var string
*/
protected $url = 'http://eventlogapp.com/api/log_message';
/**
* Set everything up.
*
* @param string $username the EventLog username (email)
* @param string $password the EventLog password
* @param string $api_key the EventLog API key
*/
public function __construct($username = '', $password = '', $api_key = '') {
$this->username = $username;
$this->password = $password;
$this->api_key = $api_key;
$this->curl = curl_init();
curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($this->curl, CURLOPT_POST, 1);
curl_setopt($this->curl, CURLOPT_URL, $this->url);
register_shutdown_function(array($this, 'send_it'));
}
/**
* Shut the mother down!
*
* @return void
*/
public function __destruct() {
curl_close($this->curl);
}
/**
* Sets the username for EventLog
*
* @param string $username the username
* @return EventLogger $this
*/
public function set_username($username) {
$this->username = $username;
return $this;
}
/**
* Sets the password for EventLog
*
* @param string $password the password
* @return EventLogger $this
*/
public function set_password($password) {
$this->password = $password;
return $this;
}
/**
* Sets the API Key for EventLog
*
* @param string $api_key the api key
* @return EventLogger $this
*/
public function set_api_key($api_key) {
$this->api_key = $api_key;
return $this;
}
/**
* Adds the given log to the queue. Defaults to an ERROR message.
*
* @param string $message the message
* @param string $type the type
* @return EventLogger $this
*/
public function log($message, $type = EventLogger::ERROR) {
$this->logs[] = $message;
$this->levels[] = $type;
return $this;
}
/**
* Sends all the logs to EventLog
*
* @return EventLogger $this
*/
public function send_it() {
if (empty($this->logs)) {
return;
}
if(count($this->levels) == 1)
{
$this->write_single($this->levels[0], $this->logs[0]);
}
else
{
$data['event_type'] = $this->levels;
$data['message'] = $this->logs;
$final_data = json_encode($data);
$this->write_multiple($final_data);
}
return $this;
}
/**
* Writes the log out to EventLog. Will throw an exception on error.
*
* @param string $message the message
* @param string $type the event type
* @return bool true on success
* @throws Exception
*/
public function write_single($event_type, $message) {
$fields = array(
'email' => $this->username,
'password' => $this->password,
'event_type' => $event_type,
'message' => $message,
);
curl_setopt($this->curl, CURLOPT_URL, $this->url);
curl_setopt($this->curl, CURLOPT_POSTFIELDS, http_build_query($fields));
curl_setopt($this->curl, CURLOPT_HTTPHEADER, array("X_API_KEY: {$this->api_key}"));
$result = json_decode(curl_exec($this->curl));
var_dump($result);
if ( ! $result->status) {
throw new Exception('EventLog Error: '.$result->message);
}
return true;
}
/**
* Writes the log out to EventLog. Will throw an exception on error.
*
* @param string $message the message
* @param string $type the event type
* @return bool true on success
* @throws Exception
*/
public function write_multiple($final_data) {
$fields = array(
'email' => $this->username,
'password' => $this->password,
'messages' => $final_data,
);
curl_setopt($this->curl, CURLOPT_URL, $this->url);
curl_setopt($this->curl, CURLOPT_POSTFIELDS, http_build_query($fields));
curl_setopt($this->curl, CURLOPT_HTTPHEADER, array("X_API_KEY: {$this->api_key}"));
$result = json_decode(curl_exec($this->curl));
var_dump($result);
if ( ! $result->status) {
throw new Exception('EventLog Error: '.$result->message);
}
return true;
}
}
/* End of file eventlogger.php */