-
Notifications
You must be signed in to change notification settings - Fork 0
/
WatchTower.php
260 lines (209 loc) · 8.41 KB
/
WatchTower.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
<?php
/*
* WatchTower Logger - Easier CI logging.
*
* WatchTower is a basic Codeigniter logging library that is not meant to
* replace CodeIgniters logging or the server level error logs.
*
* The whole purpose of WatchTower is to provide an easy means to set up and
* write to a number of various log files in a consistent and easy way.
*
* The two main points of WatchTower are:
* - Writing to log files.
* - Optionally notifying a list of email addresses if it's a SHTF situation.
*
* @author James McFall <[email protected]>
* @version 1.1
*/
class WatchTower {
/**
* Class Properties
*/
private $_conf = null; # Stores the config file watchtower settings
private $_ci = null; # Instance of CI
/**
* Constructor
*
* Check the config file for the config entries. Try to set up the log files
* and structure based on that config.
*
* @return <void>
*/
public function __construct() {
$this->_ci = &get_instance();
# Load up the CI configuration information & validate it
$this->_conf = $this->_ci->config->item("watchtower");
$this->_validateConfig();
# Build the log dirs and log files if they don't exist
$this->_buildFileStructure();
}
/**
* Write message to a log file
*
* This method will write the supplied message to the specified log file
* (stream) using the current server time in the log.
*
* @param <string> $stream - The log stream
* @param <string> $message - The error message
* @param <boolean> $notify - Whether to notify the person specified in
* the config file.
* @return <void>
*/
public function log($stream, $message, $notify = false) {
$time = new DateTime();
# Open the log file for editing (a = append mode).
$logFilePath = $this->_conf['logDir'] . "/" . $stream . $this->_getStreamSuffix() . ".log";
$logFile = new SplFileObject($logFilePath, "a");
# Append to the end of the log file with a time and a message
$logFile->fwrite(
$time->format($this->_conf["timeFormat"]) . " - " .
$message . "\n\n"
);
if ($notify === true) {
$this->_sendNotificationEmail($time, $message);
}
}
/**
* Send Notification Email
*
* This method builds a very basic html email to send to the people in the
* notify array in the config file. Basic fallback to non-html is just the
* log line entry.
*
* @param <DateTime> $time
* @param <string> $message
* @return <boolean>
*/
private function _sendNotificationEmail($time, $message) {
# If the email library is not loaded, load it up
if (!isset($this->_ci->email)) {
$this->_ci->load->library('email');
}
$timeString = $time->format($this->_conf["timeFormat"]);
# Build the basic HTML message for the email
$htmlMessage = "<h3>WatchTower Notification: " . $_SERVER['HTTP_HOST'] . "</h3>";
$htmlMessage .= "<b>Time:</b> " . $timeString . "<br /><br />";
$htmlMessage .= "<b>Message:</b> " . $message;
# Not sure if this is required, but set the mailtype to HTML
$this->_ci->email->initialize(array('mailtype' => 'html'));
# Set up message details
$this->_ci->email->from("watchtower@" . $_SERVER['HTTP_HOST'], "WatchTower Logger");
$this->_ci->email->reply_to("watchtower@" . $_SERVER['HTTP_HOST']);
$this->_ci->email->subject("WatchTower Notification: " . $_SERVER['HTTP_HOST']);
$this->_ci->email->message($htmlMessage);
$this->_ci->email->set_alt_message($timeString . " - " . $message);
# Add all of the "who to notify" entries to the email
$count = 0;
foreach ($this->_conf["whoToNotify"] as $emailAddress) {
# First email address set to "to", others are cc'd.
if ($count === 0) {
$this->_ci->email->to($emailAddress);
} else {
$this->_ci->email->cc($emailAddress);
}
$count++;
}
return $this->_ci->email->send();
}
/**
* Dump a variable into the outpub buffer and catch it as a string.
*
* @param <any> $var
* @return <string>
*/
public function dumpVarToString($var) {
ob_start();
var_dump($var);
return "\n\n".ob_get_clean();
}
/**
* Build the log dir and files.
*
* This method tries to set up the log directory from the config file and an
* individual config file for each of the logging streams specified in the
* config. If it fails it throwns an exception and these actions have to be
* manually completed.
*
* @return <void>
*/
private function _buildFileStructure() {
# Log Directory Creation
# Try to make the log directory if it doesn't exist
if (!is_dir($this->_conf['logDir'])) {
# Lets see if we can make a log directory
$mkdir = @mkdir($this->_conf['logDir']);
if (!$mkdir) {
throw new Exception("WatchTower Exception: Failed to create log
directory at " . $this->_conf['logDir'] . ". Please
manually create and set permissions to 777");
}
}
# Log File Creation
# Try to create an individual log file for each stream if they don't exist.
foreach ($this->_conf['streams'] as $stream) {
$filePath = $this->_conf['logDir'] . "/" . $stream . $this->_getStreamSuffix() . ".log";
# Skip if this file already exists
if (file_exists($filePath)) {
continue;
}
# Try to create the log file.
$touch = @touch($filePath);
if (!$touch) {
throw new Exception("WatchTower Exception: Failed to create log
files for each stream. Please set permissions for the log
directory (" . $this->_conf['logDir'] . ") to 777 and try again.");
}
}
}
/**
* Validate The Cofig File Entry
*
* This method checks that the config file has all the required information
* set up.
*
* @return <void>
*/
private function _validateConfig() {
if (!$this->_conf) {
throw new Exception("WatchTower Exception: No WatchTower
configuration located in config file. Please see documentation.");
}
if (!is_array($this->_conf['streams'])) {
throw new Exception("WatchTower Exception: No streams specified in
configuration file. Please refer to documentation");
}
if (!is_array($this->_conf['whoToNotify'])) {
throw new Exception("WatchTower Exception: No one to notify
specified in configuration file. Please refer to documentation");
}
}
/**
* Get the log file suffix.
*
* With the introduction of the daily and monthly modes, we need to be able
* to suffix the end of the file name with a date portion. This method
* returns this portion.
*
* @return <string>
*/
private function _getStreamSuffix() {
# Are we using a single file, a monthly file or a daily file for each stream?
$streamSuffix = null;
switch ($this->_conf['storageMode']) {
# If the monthly option is required, suffix with i.e. -jun-2013
case "monthly":
$streamSuffix = "-" . strtolower(date("M")) . "-" . date("Y");
break;
# If the daily option is required, suffix with i.e. -1-jun-2013
case "daily":
$streamSuffix = "-" . date("j") . "-" . strtolower(date("M")) . "-" . date("Y");
break;
# Single file mode doesn't need any suffix
default:
$streamSuffix = "";
break;
}
return $streamSuffix;
}
}
?>