-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy patheventLog.js
More file actions
438 lines (399 loc) · 14.9 KB
/
eventLog.js
File metadata and controls
438 lines (399 loc) · 14.9 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
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
/*
* Event logging.
* Handles logging of events that are deemed interesting enough to be recorded and sent to a log server for analysis.
* The log produced here is in JSON and sent to a different output than the usual fluid.log.
*
* Copyright 2017 Raising the Floor - International
*
* Licensed under the New BSD license. You may not use this file except in
* compliance with this License.
*
* The R&D leading to these results received funding from the
* Department of Education - Grant H421A150005 (GPII-APCP). However,
* these results do not necessarily represent the policy of the
* Department of Education, and you should not assume endorsement by the
* Federal Government.
*
* You may obtain a copy of the License at
* https://github.com/GPII/universal/blob/master/LICENSE.txt
*/
"use strict";
var fluid = require("infusion");
var fs = require("fs"),
moment = require("moment"),
net = require("net");
var gpii = fluid.registerNamespace("gpii"),
$ = fluid.registerNamespace("jQuery");
fluid.registerNamespace("gpii.eventLog");
fluid.defaults("gpii.eventLog", {
gradeNames: ["fluid.component", "fluid.contextAware"],
components: {
installID: {
type: "gpii.installID"
},
settingsDir: {
type: "gpii.settingsDir"
}
},
invokers: {
logEvent: {
funcName: "gpii.eventLog.log", // moduleName, event, data, level
args: ["{that}", "{arguments}.0", "{arguments}.1", "{arguments}.2", "{arguments}.3"]
},
logError: "gpii.eventLog.logError",
getGpiiSettingsDir: "{settingsDir}.getGpiiSettingsDir",
getVersion: "gpii.eventLog.getVersion",
setState: {
funcName: "gpii.eventLog.setState",
args: ["{eventLog}", "{arguments}.0", "{arguments}.1"] // name, value
}
},
members: {
sequence: 0,
// Buffered log lines while there's not log server connection
logBuffer: [],
logLevel: fluid.logLevel.INFO,
// Maximum number of lines to buffer.
maxBufferlength: 0xfff,
// A TCP socket to send the log entries to (filebeat service).
logServer: {
host: null,
port: null
},
// A file to write the log entries to.
logPath: null,
// Data to include in every log entry.
eventData: {
// The installation ID
installID: "@expand:{that}.installID.getInstallID()",
version: "@expand:{that}.getVersion()",
sessionID: undefined,
gpiiKey: undefined
},
// The start times for events which require durations to be recorded.
eventTimes: {}
},
listeners: {
"onCreate.logFile": {
func: "gpii.eventLog.initLogDestination",
args: ["{that}", "{that}.options.logDestination"]
},
"onCreate.log": {
func: "gpii.eventLog.logStartStop",
args: ["{that}", "start"]
},
"onDestroy.log": {
func: "gpii.eventLog.logStartStop",
args: ["{that}", "stop"]
}
},
// File path, or tcp://host:port
logDestination: null
});
/**
* A log event
* @typedef {Object} LogEvent
* @property {String} module The area of GPII the event is from.
* @property {String} event The name of the event.
* @property {String} data [optional] Extra information about the event.
* @property {Object} level The severity of the event (from fluid.logLevelsSpec, default: fluid.INFO).
* @property {String} version The eventLog module version. (automatically added)
* @property {String} timestamp Current time of the event. (automatically added)
* @property {String} sequence Ordered unique identifier to the event. (automatically added)
*/
/**
* Returns the actual date and time, as a string, in ISO 8601 format with the localtime + offset from UTC.
* eg: '2018-07-13T13:03:03.863+01:00'
* @return {String} The current date, in ISO-8601 format with localtime and UTC offset.
*/
gpii.eventLog.getTimestamp = function () {
return moment().toISOString(true);
};
/**
* Gets the version of the logger. This is logged so the log processing can adjust for any updates to this module.
*
* @return {String} The version of this module, from package.json.
*/
gpii.eventLog.getVersion = function () {
var packageJson = fluid.require("%eventLog/package.json");
return packageJson.version;
};
/**
* Logs the start or stop of GPII, without duplication.
* @param {Component} that - The gpii.eventLog instance.
* @param {String} state - "start" or "stop".
*/
gpii.eventLog.logStartStop = function (that, state) {
if (state !== gpii.eventLog.logStartStop.currentState) {
gpii.eventLog.logStartStop.currentState = state;
if (state === "start") {
// Start the event numbering based on the current timestamp to ensure it's unique (per instance).
that.sequence = Date.now() - 15e11;
}
that.logEvent("gpii", state);
}
};
/**
* Creates an object for the log. Everything in this object is what will be logged, and the "time" field will be added
* later when it is actually logged.
*
* @param {String} moduleName - The part of GPII causing this event.
* @param {String} event - Name of the event.
* @param {Any} [data] - [optional] Event specific data.
* @param {Object} level -[optional] Level of the log, see fluid.logLevelsSpec [FATAL,FAIL,WARN,IMPORTANT,INFO,TRACE].
* @return {LogEvent} The log object.
*/
gpii.eventLog.createLogObject = function (moduleName, event, data, level) {
var eventObject = {
module: moduleName || "GPII",
event: event,
level: level
};
var hasValue = (data !== null && data !== undefined);
if (hasValue && fluid.isPlainObject(data)) {
hasValue = !$.isEmptyObject(data);
}
if (hasValue) {
eventObject.data = fluid.copy(data);
// Replace any components in the data object with their id.
fluid.each(eventObject.data, function (value, key) {
if (fluid.isComponent(value)) {
eventObject.data[key] = value.id;
}
});
}
return eventObject;
};
/**
* Logs an event.
*
* @param {Component} that - The gpii.eventLog instance.
* @param {String} moduleName - The part of GPII causing this event.
* @param {String} event - The event name.
* @param {Object} [data] - [optional] Event specific data. Can (shallowly) contain components, in which case just the ID is
* logged.
* @param {Object} [level] - [optional] Level of the log, see fluid.logLevelsSpec [FATAL,FAIL,WARN,IMPORTANT,INFO,TRACE].
*/
gpii.eventLog.log = function (that, moduleName, event, data, level) {
var eventObject = gpii.eventLog.createLogObject(moduleName, event, data, level);
gpii.eventLog.writeLog(that, level, eventObject);
};
/**
* Logs an error.
*
* @param {Component} that - The gpii.eventLog instance.
* @param {String} moduleName - The part of GPII causing this error.
* @param {String} errType - Type of error.
* @param {Object} err - The error.
* @param {Object} [level] - [optional] Level of the log. default: fluid.logLevel.FAIL.
*/
gpii.eventLog.logError = function (that, moduleName, errType, err, level) {
if (!level) {
level = fluid.logLevel.FAIL;
}
var data = {};
if (err instanceof Error) {
// Error doesn't serialise
data.error = {};
fluid.each(Object.getOwnPropertyNames(err), function (source) {
// Ensure the first character of the field is lowercase - "Message" vs "message" was causing a duplicate
// field during the analysis.
var dest = source.charAt(0).toLowerCase() + source.slice(1);
data.error[dest] = err[source];
});
} else if (fluid.isPlainObject(err, true)) {
data.error = Object.assign({}, err);
} else {
data.error = {
message: err
};
}
if (!data.error.stack) {
data.error.stack = new Error().stack;
}
var eventObject = gpii.eventLog.createLogObject(moduleName, "Error." + errType, data);
gpii.eventLog.writeLog(that, fluid.logLevel.FAIL, eventObject);
};
// Log fluid.fail.
fluid.failureEvent.addListener(function (args) {
var err = Array.isArray(args) ? args.join(" ") : args;
gpii.eventLog.gotError(err, "Fail");
}, "gpii-eventLog", "before:fail");
/**
* Logs an error caught by onUncaughtException or failureEvent.
*
* @param {String} err - The error.
* @param {String} errType - The error type (defaults to "Exception").
*/
gpii.eventLog.gotError = function (err, errType) {
var eventLog = fluid.queryIoCSelector(fluid.rootComponent, "gpii.eventLog");
if (eventLog.length > 0) {
fluid.each(eventLog, function (inst) {
inst.logError(inst, null, errType || "Exception", err);
});
}
};
// Log uncaught exceptions.
fluid.onUncaughtException.addListener(gpii.eventLog.gotError, "gpii-eventLog");
/**
* Parses the log path (which can be overridden by the GPII_EVENT_LOG environment variable), and sets either the
* logServer or logPath member.
*
* @param {Component} that - The gpii.eventLog instance.
* @param {String} logPath - The path to the log file (a file, or a tcp socket in the format of `tcp://<host>:<port>`).
*/
gpii.eventLog.initLogDestination = function (that, logPath) {
logPath = process.env.GPII_EVENT_LOG || logPath;
var match = logPath && /tcp:\/*([^:]+):([0-9]+)/.exec(logPath);
if (match) {
that.logServer.host = match[1];
that.logServer.port = match[2];
} else {
that.logPath = logPath;
}
var dest = that.logServer.host ? ("tcp://" + that.logServer.host + ":" + that.logServer.port) : that.logPath;
fluid.log(fluid.logLevel.IMPORTANT, "Writing event log to " + dest);
};
/**
* Writes an event to the log file.
*
* @param {Component} that - The gpii.eventLog instance.
* @param {Object} level - Level of the log, see fluid.logLevelsSpec [FATAL,FAIL,WARN,IMPORTANT,INFO,TRACE].
* @param {LogEvent} event - The object. This will be modified to what has been sent to the log, adding the installID and
* timestamp fields.
*/
gpii.eventLog.writeLog = function (that, level, event) {
var eventLevel = gpii.eventLog.checkLevel(level);
event.level = eventLevel.value;
gpii.eventLog.recordDuration(that, event);
Object.assign(event, that.eventData);
event.timestamp = gpii.eventLog.getTimestamp();
event.sequence = that.sequence++;
if (eventLevel.priority <= that.logLevel.priority) {
var logLine = JSON.stringify(event) + "\n";
if (that.logServer.host && that.logServer.port) {
gpii.eventLog.writeLogTcp(that, logLine);
}
if (that.logPath) {
fs.appendFileSync(that.logPath, logLine);
}
}
};
/**
* Handles the timing of a pair of duration events.
*
* Some events can be paired into having a duration. For example, tooltip-shown and tooltip-hidden. The timestamp for
* the start event is recorded, so when the end event is seen the duration is calculated and added to the event data.
*
* @param {Component} that The gpii.eventLog instance.
* @param {LogEvent} event The event.
*/
gpii.eventLog.recordDuration = function (that, event) {
var endEvent = that.options.durationEvents[event.event];
if (endEvent) {
// This is the start of a pair of duration events.
if (!that.eventTimes[endEvent]) {
that.eventTimes[endEvent] = [];
}
that.eventTimes[endEvent].push(process.hrtime());
} else {
var startTimes = that.eventTimes[event.event];
var startTime = startTimes && startTimes.pop();
if (startTime) {
// This is the ending of a pair of duration events.
if (!event.data) {
event.data = {};
}
var memberName = event.data.hasOwnProperty("duration") ? "duration_auto" : "duration";
event.data[memberName] = process.hrtime(startTime)[0];
}
}
};
/**
* Sends a log line to the configured log server.
*
* @param {Component} that - The gpii.eventLog instance.
* @param {String} logLine - The log line, including the newline.
*/
gpii.eventLog.writeLogTcp = function (that, logLine) {
if (!that.logSocket) {
gpii.eventLog.connectLog(that);
}
if (that.logSocket.connecting) {
// Buffer the output until the connection is made. Socket.write already buffers, however if the connection
// fails the data will be lost.
if (that.logBuffer.length > that.maxBufferlength) {
fluid.log("Dropping metrics data - exceeded initial buffer size of " + that.maxBufferlength);
} else {
that.logBuffer.push(logLine);
}
} else {
that.logSocket.write(logLine);
}
};
/**
* Connect to a TCP port which receives the log data.
* @param {Component} that - The gpii.eventLog instance.
*/
gpii.eventLog.connectLog = function (that) {
if (that.logSocket) {
fluid.fail("Already connecting to log server");
}
// Connect to the server.
that.logSocket = new net.Socket();
that.logSocket.connect(that.logServer.port, that.logServer.host, function () {
gpii.eventLog.connectLog.lastError = null;
fluid.log("Connected to log server");
// Send the initial data.
if (that.logBuffer.length > 0) {
that.logSocket.write(that.logBuffer.join(""));
that.logBuffer = [];
}
});
that.logSocket.on("close", function () {
if (that.logSocket) {
that.logSocket.destroy();
that.logSocket = null;
}
});
that.logSocket.on("error", function (err) {
if (gpii.eventLog.connectLog.lastError !== err.code) {
fluid.log("Log server socket error:", err);
if (that.logSocket) {
that.logSocket.destroy();
}
// Don't repeat the same error again.
gpii.eventLog.connectLog.lastError = err.code;
}
});
};
/**
* Ensure that the loglevel has a valid value. The levels are defined in the fluid.logLevelsSpec
* Sets INFO as default loglevel
*
* @param {Object} level - Level to check, can be a string that represents the value or a property of fluid.logLevel.
* @return {Object} A valid fluid.logLevel, with INFO as default.
*/
gpii.eventLog.checkLevel = function (level) {
var togo;
if (typeof level === "string" && level in fluid.logLevelsSpec) {
togo = fluid.logLevel[level];
} else {
togo = fluid.isLogLevel(level) && level;
}
return togo || fluid.logLevel.INFO;
};
/**
* Specifies a field which gets logged with every subsequent event.
*
* @param {Component} that The gpii.eventLog instance.
* @param {String} name The name of the field.
* @param {Object} value The field value. undefined or null will remove the field.
*/
gpii.eventLog.setState = function (that, name, value) {
if (value === undefined || value === null) {
delete that.eventData[name];
} else {
that.eventData[name] = value;
}
};