-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
101 lines (81 loc) · 3.25 KB
/
index.js
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
const ETLogger = require("etlogger");
const {Logging} = require('@google-cloud/logging');
// See https://cloud.google.com/logging/docs/reference/libraries
class GcpHandler {
constructor(opts) {
this.cfg = Object.assign({
projectId: null,
keyFilename: null,
structured: true,
logName: "_Default",
resourceType: "global", // if anything other than global then there will be specific label requirements
labels: {}, // key/value pairs - you can't actually pass unexpected things here
}, opts);
let credentials = undefined;
if (this.cfg.projectId && this.cfg.keyFilename) {
credentials = {
projectId: this.cfg.projectId,
keyFilename: this.cfg.keyFilename,
};
} else {
if (process.env.NODE_ENV !== "production" && !process.env.GOOGLE_APPLICATION_CREDENTIALS) {
ETLogger.logFromHandler(this, "GOOGLE_APPLICATION_CREDENTIALS is not set. Default credentials are probably missing.");
}
}
// ETLogger.logFromHandler(this, "GcpLogger Config = ", this.cfg);
// ETLogger.logFromHandler(this, "Credentials = ", credentials);
this._logging = new Logging(credentials);
this._log = this._logging.log(this.cfg.logName);
}
handleLog(logMsg) {
const metadata = {
resource: {
type: this.cfg.resourceType,
labels: this.cfg.labels,
},
}
// TODO: Test the performance impact of a switch lookup table vs. a dictionary based one.
metadata.severity = "DEFAULT";
switch (logMsg.level) {
case logMsg.logger.LEVEL_ERROR:
metadata.severity = "ERROR";
break;
case logMsg.logger.LEVEL_WARN:
metadata.severity = "WARNING";
break;
case logMsg.logger.LEVEL_INFO:
metadata.severity = "INFO";
break;
case logMsg.logger.LEVEL_DEBUG:
metadata.severity = "DEBUG";
break;
}
// Learning about the timestamp value. Looks like adding it to the
// metadata as a Date object really does work properly
const msg = logMsg.getPlainMessage();
// const msg = `${logMsg.getPlainMessage()} @ ${logMsg.date.toString()}`;
metadata.timestamp = logMsg.date;
// metadata.timestamp = new Date(logMsg.date.valueOf() + 10000);
// TODO: Look into including flatten objects in the json data
// At the moment it looks like we probably need to duplicate
// a bunch of code from the formatters to duplicate the
// flattenLastArg stuff...
let entry;
if (this.cfg.structured) {
const data = {
logText: msg,
args: logMsg.args,
};
if (logMsg.flatten) {
data.flatten = logMsg.flatten;
}
entry = this._log.entry(metadata, data);
} else {
// Unstructured, just the text
entry = this._log.entry(metadata, msg);
}
this._log.write(entry);
// ETLogger.logFromHandler(this, "GCP Wrote: ", entry);
}
}
module.exports = GcpHandler;