-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
182 lines (162 loc) · 7.21 KB
/
Copy pathindex.js
File metadata and controls
182 lines (162 loc) · 7.21 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
/**
* @typedef {import("express").Request} Express.Request
* @typedef {{[x: string]: any}} Dictionary.Any
*/
const appInsights = require("applicationinsights"),
util = require("util");
// MARK: class Log
/**
* A static class that handles logging to Application Insights.
*/
class Log {
/** @type {Dictionary.Any} */
static #baseProperties = {};
/** @type {string} */
static #instrumentationKeyOrConnectionString = void 0;
// MARK: static #getData
/**
* Gets the tag overrides and properties from the options.
* @param {object} [options] The options to pass into the properties.
* @param {Express.Request} [options.req] The Express request object.
* @param {Dictionary.Any} [options.properties] The properties to include.
* @returns {{properties: object}} The properties object.
*/
static #getData(options = {}) {
const data = {
properties: options.properties ? {...Log.#baseProperties, ...options.properties} : {...Log.#baseProperties}
};
if (options.req) {
if (options.req.path) {
data.properties.path = options.req.path;
}
if (options.req.ip) {
data.properties.ip = options.req.ip;
}
}
return data;
}
// MARK: static #log
/**
* Logs a message or exception to Application Insights or the console.
* @param {"Verbose" | "Information" | "Warning" | "Error" | "Critical"} level The severity level.
* @param {string} message The message to log.
* @param {object} [options] The options to pass into the properties.
* @param {Express.Request} [options.req] The Express request object.
* @param {Dictionary.Any} [options.properties] The properties to include.
* @param {Error} [options.err] The error object to log as the exception (for "Error" and "Critical" levels).
* @returns {void}
*/
static #log(level, message, options) {
if (Log.#instrumentationKeyOrConnectionString && typeof Log.#instrumentationKeyOrConnectionString === "string" && Log.#instrumentationKeyOrConnectionString !== "") {
const data = Log.#getData(options);
if (level === appInsights.KnownSeverityLevel.Error || level === appInsights.KnownSeverityLevel.Critical) {
data.properties.message = message;
appInsights.defaultClient.trackException({
time: new Date(),
severity: level,
properties: data.properties,
exception: options?.err
});
} else {
appInsights.defaultClient.trackTrace({
message,
time: new Date(),
severity: appInsights.KnownSeverityLevel[level],
properties: data.properties
});
}
} else if (options?.err) {
console.error(`${level}: ${message}`, util.inspect(options.err, false, Infinity));
} else {
console.log(`${level}: ${message}`);
}
}
// MARK: static setupApplicationInsights
/**
* Sets up logging to use Application Insights. If not called, this library will log to the console instead.
* @param {string} instrumentationKeyOrConnectionString The Application Insights instrumentation key or connection string.
* @param {Dictionary.Any} [properties] The base properties to include with every trace and exception.
* @param {boolean} [autoCollectRequests] Whether to automatically collect requests. Defaults to true.
* @returns {void}
*/
static setupApplicationInsights(instrumentationKeyOrConnectionString, properties, autoCollectRequests = true) {
if (Log.#instrumentationKeyOrConnectionString) {
throw new Error("You have already setup Application Insights.");
}
if (!instrumentationKeyOrConnectionString || typeof instrumentationKeyOrConnectionString !== "string") {
throw new Error("The Application Insights instrumentation key or connection string is required.");
}
if (properties !== void 0 && (typeof properties !== "object" || properties === null || Array.isArray(properties))) {
throw new Error("Base properties must be a non-null object.");
}
Log.#instrumentationKeyOrConnectionString = instrumentationKeyOrConnectionString;
appInsights.setup(Log.#instrumentationKeyOrConnectionString).setAutoCollectRequests(autoCollectRequests);
appInsights.start();
if (properties) {
Log.#baseProperties = {...properties};
}
}
// MARK: static verbose
/**
* Logs a verbose message.
* @param {string} message The message to log.
* @param {object} [options] The options to pass into the properties.
* @param {Express.Request} [options.req] The Express request object.
* @param {Dictionary.Any} [options.properties] The properties to include.
* @returns {void}
*/
static verbose(message, options) {
Log.#log(appInsights.KnownSeverityLevel.Verbose, message, options);
}
// MARK: static info
/**
* Logs an informational message.
* @param {string} message The message to log.
* @param {object} [options] The options to pass into the properties.
* @param {Express.Request} [options.req] The Express request object.
* @param {Dictionary.Any} [options.properties] The properties to include.
* @returns {void}
*/
static info(message, options) {
Log.#log(appInsights.KnownSeverityLevel.Information, message, options);
}
// MARK: static warn
/**
* Logs a warning.
* @param {string} message The message to log.
* @param {object} [options] The options to pass into the properties.
* @param {Express.Request} [options.req] The Express request object.
* @param {Dictionary.Any} [options.properties] The properties to include.
* @returns {void}
*/
static warn(message, options) {
Log.#log(appInsights.KnownSeverityLevel.Warning, message, options);
}
// MARK: static error
/**
* Logs an error.
* @param {string} message The message describing the error.
* @param {object} options The options to pass into the properties.
* @param {Error} options.err The error object to log as the exception.
* @param {Express.Request} [options.req] The Express request object.
* @param {Dictionary.Any} [options.properties] The properties to include.
* @returns {void}
*/
static error(message, options) {
Log.#log(appInsights.KnownSeverityLevel.Error, message, options);
}
// MARK: static critical
/**
* Logs a critical error.
* @param {string} message The message describing the error.
* @param {object} options The options to pass into the properties.
* @param {Error} options.err The error object to log as the exception.
* @param {Express.Request} [options.req] The Express request object.
* @param {Dictionary.Any} [options.properties] The properties to include.
* @returns {void}
*/
static critical(message, options) {
Log.#log(appInsights.KnownSeverityLevel.Critical, message, options);
}
}
module.exports = Log;