@crimson-education/browser-logger 5.0.1-beta.3 → 5.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +127 -73
- package/lib/index.d.ts +1 -1
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +47 -19
- package/lib/index.js.map +1 -1
- package/lib/logger/consoleTransport.d.ts +1 -1
- package/lib/logger/consoleTransport.d.ts.map +1 -1
- package/lib/logger/consoleTransport.js +22 -16
- package/lib/logger/consoleTransport.js.map +1 -1
- package/lib/logger/datadogTransport.d.ts +1 -1
- package/lib/logger/datadogTransport.d.ts.map +1 -1
- package/lib/logger/datadogTransport.js +8 -4
- package/lib/logger/datadogTransport.js.map +1 -1
- package/lib/logger/index.js +49 -29
- package/lib/logger/index.js.map +1 -1
- package/lib/logger/index.test.js +18 -16
- package/lib/logger/index.test.js.map +1 -1
- package/lib/logger/utils.js +9 -4
- package/lib/logger/utils.js.map +1 -1
- package/lib/reporters/amplifyReporter.d.ts +86 -0
- package/lib/reporters/amplifyReporter.d.ts.map +1 -0
- package/lib/reporters/amplifyReporter.js +225 -0
- package/lib/reporters/amplifyReporter.js.map +1 -0
- package/lib/reporters/amplifyReporter.test.d.ts +2 -0
- package/lib/reporters/amplifyReporter.test.d.ts.map +1 -0
- package/lib/reporters/amplifyReporter.test.js +51 -0
- package/lib/reporters/amplifyReporter.test.js.map +1 -0
- package/lib/reporters/datadogReporter.d.ts +1 -13
- package/lib/reporters/datadogReporter.d.ts.map +1 -1
- package/lib/reporters/datadogReporter.js +49 -122
- package/lib/reporters/datadogReporter.js.map +1 -1
- package/lib/reporters/gtmReporter.d.ts +1 -1
- package/lib/reporters/gtmReporter.d.ts.map +1 -1
- package/lib/reporters/gtmReporter.js +5 -1
- package/lib/reporters/gtmReporter.js.map +1 -1
- package/lib/reporters/index.js +71 -46
- package/lib/reporters/index.js.map +1 -1
- package/lib/reporters/logReporter.js +34 -24
- package/lib/reporters/logReporter.js.map +1 -1
- package/lib/reporters/posthogReporter.d.ts +51 -0
- package/lib/reporters/posthogReporter.d.ts.map +1 -0
- package/lib/reporters/posthogReporter.js +254 -0
- package/lib/reporters/posthogReporter.js.map +1 -0
- package/lib/reporters/posthogReporter.test.d.ts +2 -0
- package/lib/reporters/posthogReporter.test.d.ts.map +1 -0
- package/lib/reporters/posthogReporter.test.js +197 -0
- package/lib/reporters/posthogReporter.test.js.map +1 -0
- package/lib/types/index.js +18 -2
- package/lib/types/index.js.map +1 -1
- package/lib/types/logger.d.ts +3 -3
- package/lib/types/logger.d.ts.map +1 -1
- package/lib/types/logger.js +5 -2
- package/lib/types/logger.js.map +1 -1
- package/lib/types/reporter.d.ts +20 -6
- package/lib/types/reporter.d.ts.map +1 -1
- package/lib/types/reporter.js +2 -1
- package/lib/utils.js +5 -1
- package/lib/utils.js.map +1 -1
- package/lib/utils.test.js +4 -2
- package/lib/utils.test.js.map +1 -1
- package/package.json +17 -11
- package/src/index.ts +11 -0
- package/src/reporters/amplifyReporter.test.ts +61 -0
- package/src/reporters/amplifyReporter.ts +344 -0
- package/src/reporters/datadogReporter.ts +22 -133
- package/src/reporters/posthogReporter.test.ts +244 -0
- package/src/reporters/posthogReporter.ts +374 -0
- package/src/types/reporter.ts +16 -0
package/lib/logger/index.js
CHANGED
|
@@ -1,33 +1,52 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
exports.logger = exports.createLogger = exports.setLogLevel = exports.globalLogLevel = exports.logTransports = exports.globalMetadata = void 0;
|
|
1
18
|
/* eslint-disable prefer-rest-params */
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
export let globalLogLevel;
|
|
19
|
+
const types_1 = require("../types");
|
|
20
|
+
const utils_1 = require("../utils");
|
|
21
|
+
const consoleTransport_1 = require("./consoleTransport");
|
|
22
|
+
const utils_2 = require("./utils");
|
|
23
|
+
__exportStar(require("./consoleTransport"), exports);
|
|
24
|
+
__exportStar(require("./datadogTransport"), exports);
|
|
25
|
+
exports.globalMetadata = {};
|
|
26
|
+
exports.logTransports = [(0, consoleTransport_1.consoleTransport)()];
|
|
11
27
|
/**
|
|
12
28
|
* Sets the global log level.
|
|
13
29
|
*/
|
|
14
|
-
|
|
15
|
-
globalLogLevel = level
|
|
30
|
+
function setLogLevel(level) {
|
|
31
|
+
exports.globalLogLevel = level !== null && level !== void 0 ? level : undefined;
|
|
16
32
|
}
|
|
33
|
+
exports.setLogLevel = setLogLevel;
|
|
17
34
|
/**
|
|
18
35
|
* Creates a Logger Instance.
|
|
19
36
|
*/
|
|
20
|
-
|
|
21
|
-
|
|
37
|
+
function createLogger(options = {}) {
|
|
38
|
+
var _a;
|
|
39
|
+
const rootMetadata = (_a = options.metadata) !== null && _a !== void 0 ? _a : {};
|
|
22
40
|
const logger = {
|
|
23
41
|
// impl loosely based on Winston Logger's dynamic log function
|
|
24
42
|
// https://github.com/winstonjs/winston/blob/master/lib/winston/logger.js
|
|
25
43
|
log() {
|
|
44
|
+
var _a, _b;
|
|
26
45
|
let logInfo;
|
|
27
46
|
// Optimize for the hotpath of logging JSON literals
|
|
28
47
|
if (arguments.length === 1) {
|
|
29
48
|
logInfo = {
|
|
30
|
-
level: LogLevel.Info,
|
|
49
|
+
level: types_1.LogLevel.Info,
|
|
31
50
|
...arguments[0],
|
|
32
51
|
};
|
|
33
52
|
}
|
|
@@ -37,7 +56,7 @@ export function createLogger(options = {}) {
|
|
|
37
56
|
if (message && typeof message === 'object') {
|
|
38
57
|
logInfo = {
|
|
39
58
|
...message,
|
|
40
|
-
level: message.level
|
|
59
|
+
level: (_a = message.level) !== null && _a !== void 0 ? _a : level,
|
|
41
60
|
};
|
|
42
61
|
}
|
|
43
62
|
else {
|
|
@@ -58,7 +77,7 @@ export function createLogger(options = {}) {
|
|
|
58
77
|
message,
|
|
59
78
|
};
|
|
60
79
|
if (meta.message) {
|
|
61
|
-
logInfo.message = `${getLogMessage(logInfo.message)} ${getLogMessage(meta.message)}`;
|
|
80
|
+
logInfo.message = `${(0, utils_2.getLogMessage)(logInfo.message)} ${(0, utils_2.getLogMessage)(meta.message)}`;
|
|
62
81
|
}
|
|
63
82
|
if (meta.stack) {
|
|
64
83
|
logInfo.stack = meta.stack;
|
|
@@ -73,27 +92,27 @@ export function createLogger(options = {}) {
|
|
|
73
92
|
}
|
|
74
93
|
}
|
|
75
94
|
// Check if we should log this message
|
|
76
|
-
const minLevel = globalLogLevel
|
|
77
|
-
if (minLevel && !isAboveLevel(logInfo.level, minLevel)) {
|
|
95
|
+
const minLevel = exports.globalLogLevel !== null && exports.globalLogLevel !== void 0 ? exports.globalLogLevel : options.logLevel;
|
|
96
|
+
if (minLevel && !(0, utils_2.isAboveLevel)(logInfo.level, minLevel)) {
|
|
78
97
|
return logger;
|
|
79
98
|
}
|
|
80
99
|
// Add timestamp and root metadata.
|
|
81
100
|
// Ensure there is a message property.
|
|
82
101
|
logInfo = {
|
|
83
|
-
...globalMetadata,
|
|
102
|
+
...exports.globalMetadata,
|
|
84
103
|
...rootMetadata,
|
|
85
104
|
timestamp: new Date().toISOString(),
|
|
86
105
|
...logInfo,
|
|
87
|
-
message: logInfo.message
|
|
106
|
+
message: (_b = logInfo.message) !== null && _b !== void 0 ? _b : '',
|
|
88
107
|
};
|
|
89
|
-
for (const transport of logTransports) {
|
|
108
|
+
for (const transport of exports.logTransports) {
|
|
90
109
|
if (transport.logLevel) {
|
|
91
|
-
if (!isAboveLevel(logInfo.level, transport.logLevel)) {
|
|
110
|
+
if (!(0, utils_2.isAboveLevel)(logInfo.level, transport.logLevel)) {
|
|
92
111
|
continue;
|
|
93
112
|
}
|
|
94
113
|
}
|
|
95
114
|
transport.log({
|
|
96
|
-
...filterReporterMetadata(logInfo, transport),
|
|
115
|
+
...(0, utils_1.filterReporterMetadata)(logInfo, transport),
|
|
97
116
|
// These would break the transport if excluded.
|
|
98
117
|
level: logInfo.level,
|
|
99
118
|
message: logInfo.message,
|
|
@@ -111,18 +130,19 @@ export function createLogger(options = {}) {
|
|
|
111
130
|
logger: this,
|
|
112
131
|
done: ({ message, level, metadata } = {}) => {
|
|
113
132
|
const duration = new Date().getTime() - start.getTime();
|
|
114
|
-
logger.log(level
|
|
133
|
+
logger.log(level !== null && level !== void 0 ? level : types_1.LogLevel.Info, message !== null && message !== void 0 ? message : 'Timer Completed', { duration, ...(metadata !== null && metadata !== void 0 ? metadata : {}) });
|
|
115
134
|
return true;
|
|
116
135
|
},
|
|
117
136
|
};
|
|
118
137
|
},
|
|
119
138
|
// Forward the log levels into the log function, essentially shortcuts.
|
|
120
|
-
debug: (...args) => logger.log(LogLevel.Debug, ...args),
|
|
121
|
-
info: (...args) => logger.log(LogLevel.Info, ...args),
|
|
122
|
-
warn: (...args) => logger.log(LogLevel.Warn, ...args),
|
|
123
|
-
error: (...args) => logger.log(LogLevel.Error, ...args),
|
|
139
|
+
debug: (...args) => logger.log(types_1.LogLevel.Debug, ...args),
|
|
140
|
+
info: (...args) => logger.log(types_1.LogLevel.Info, ...args),
|
|
141
|
+
warn: (...args) => logger.log(types_1.LogLevel.Warn, ...args),
|
|
142
|
+
error: (...args) => logger.log(types_1.LogLevel.Error, ...args),
|
|
124
143
|
};
|
|
125
144
|
return logger;
|
|
126
145
|
}
|
|
127
|
-
|
|
146
|
+
exports.createLogger = createLogger;
|
|
147
|
+
exports.logger = createLogger();
|
|
128
148
|
//# sourceMappingURL=index.js.map
|
package/lib/logger/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/logger/index.ts"],"names":[],"mappings":"AAAA,uCAAuC;AACvC,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/logger/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AAAA,uCAAuC;AACvC,oCAA8G;AAC9G,oCAAkD;AAClD,yDAAsD;AACtD,mCAAsD;AAEtD,qDAAmC;AACnC,qDAAmC;AAEtB,QAAA,cAAc,GAAa,EAAE,CAAC;AAE9B,QAAA,aAAa,GAAoB,CAAC,IAAA,mCAAgB,GAAE,CAAC,CAAC;AAGnE;;GAEG;AACH,SAAgB,WAAW,CAAC,KAAsB;IAChD,sBAAc,GAAG,KAAK,aAAL,KAAK,cAAL,KAAK,GAAI,SAAS,CAAC;AACtC,CAAC;AAFD,kCAEC;AAED;;GAEG;AACH,SAAgB,YAAY,CAAC,UAAyB,EAAE;;IACtD,MAAM,YAAY,GAAG,MAAA,OAAO,CAAC,QAAQ,mCAAI,EAAE,CAAC;IAE5C,MAAM,MAAM,GAAY;QACtB,8DAA8D;QAC9D,yEAAyE;QACzE,GAAG;;YACD,IAAI,OAAsB,CAAC;YAE3B,oDAAoD;YACpD,IAAI,SAAS,CAAC,MAAM,KAAK,CAAC,EAAE;gBAC1B,OAAO,GAAG;oBACR,KAAK,EAAE,gBAAQ,CAAC,IAAI;oBACpB,GAAG,SAAS,CAAC,CAAC,CAAC;iBAChB,CAAC;aACH;YACD,mDA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ACxD,MAAM,CAAC,GAAG,CAAC,KAAK,aAAL,KAAK,cAAL,KAAK,GAAI,gBAAQ,CAAC,IAAI,EAAE,OAAO,aAAP,OAAO,cAAP,OAAO,GAAI,iBAAiB,EAAE,EAAE,QAAQ,EAAE,GAAG,CAAC,QAAQ,aAAR,QAAQ,cAAR,QAAQ,GAAI,EAAE,CAAC,EAAE,CAAC,CAAC;oBACpG,OAAO,IAAI,CAAC;gBACd,CAAC;aACF,CAAC;QACJ,CAAC;QACD,uEAAuE;QACvE,KAAK,EAAE,CAAC,GAAG,IAAI,EAAE,EAAE,CAAC,MAAM,CAAC,GAAG,CAAC,gBAAQ,CAAC,KAAK,EAAE,GAAG,IAAI,CAAC;QACvD,IAAI,EAAE,CAAC,GAAG,IAAI,EAAE,EAAE,CAAC,MAAM,CAAC,GAAG,CAAC,gBAAQ,CAAC,IAAI,EAAE,GAAG,IAAI,CAAC;QACrD,IAAI,EAAE,CAAC,GAAG,IAAI,EAAE,EAAE,CAAC,MAAM,CAAC,GAAG,CAAC,gBAAQ,CAAC,IAAI,EAAE,GAAG,IAAI,CAAC;QACrD,KAAK,EAAE,CAAC,GAAG,IAAI,EAAE,EAAE,CAAC,MAAM,CAAC,GAAG,CAAC,gBAAQ,CAAC,KAAK,EAAE,GAAG,IAAI,CAAC;KACxD,CAAC;IAEF,OAAO,MAAM,CAAC;AAChB,CAAC;AAhHD,oCAgHC;AAEY,QAAA,MAAM,GAAG,YAAY,EAAE,CAAC"}
|
package/lib/logger/index.test.js
CHANGED
|
@@ -1,31 +1,33 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const _1 = require(".");
|
|
4
|
+
const __1 = require("..");
|
|
5
|
+
const reporters_1 = require("../reporters");
|
|
6
|
+
const types_1 = require("../types");
|
|
5
7
|
// This isn't actually asserting anything.
|
|
6
8
|
// But it ensures that the logs look correct, and no errors get thrown.
|
|
7
9
|
describe('logger', () => {
|
|
8
|
-
const logger = createLogger({
|
|
10
|
+
const logger = (0, _1.createLogger)({
|
|
9
11
|
metadata: {
|
|
10
12
|
service: 'test',
|
|
11
13
|
},
|
|
12
14
|
});
|
|
13
15
|
// Manually set the log transports.
|
|
14
|
-
logTransports.length = 0;
|
|
15
|
-
const transport = consoleTransport({
|
|
16
|
+
_1.logTransports.length = 0;
|
|
17
|
+
const transport = (0, _1.consoleTransport)({
|
|
16
18
|
// So we can see everything.
|
|
17
19
|
ignoreMetadataPatterns: [],
|
|
18
20
|
});
|
|
19
|
-
logTransports.push(transport);
|
|
21
|
+
_1.logTransports.push(transport);
|
|
20
22
|
it('should log', async () => {
|
|
21
23
|
logger.log({
|
|
22
24
|
level: 'info',
|
|
23
25
|
message: 'data',
|
|
24
26
|
});
|
|
25
27
|
logger.log({ data: 'obj' });
|
|
26
|
-
logger.log(LogLevel.Debug, 'level, message', { data: 'obj' });
|
|
27
|
-
logger.log(LogLevel.Debug, 'level, message, metadata', { data: 'obj' });
|
|
28
|
-
init({
|
|
28
|
+
logger.log(types_1.LogLevel.Debug, 'level, message', { data: 'obj' });
|
|
29
|
+
logger.log(types_1.LogLevel.Debug, 'level, message, metadata', { data: 'obj' });
|
|
30
|
+
(0, __1.init)({
|
|
29
31
|
service: 'test-service',
|
|
30
32
|
environment: 'test',
|
|
31
33
|
version: 'no-version',
|
|
@@ -39,17 +41,17 @@ describe('logger', () => {
|
|
|
39
41
|
const timer = logger.startTimer();
|
|
40
42
|
await new Promise((resolve) => setTimeout(resolve, 1000));
|
|
41
43
|
timer.done({ message: 'timer done', metadata: { id: 1 } });
|
|
42
|
-
|
|
43
|
-
addMetadata({ global: 'metadata' });
|
|
44
|
+
(0, _1.setLogLevel)(types_1.LogLevel.Info);
|
|
45
|
+
(0, reporters_1.addMetadata)({ global: 'metadata' });
|
|
44
46
|
logger.debug('You should not see this');
|
|
45
47
|
logger.info('You should see this with { global: "metadata" }');
|
|
46
|
-
|
|
47
|
-
setUser({
|
|
48
|
+
(0, _1.setLogLevel)(null);
|
|
49
|
+
(0, reporters_1.setUser)({
|
|
48
50
|
id: '123',
|
|
49
51
|
email: 'test@example.com',
|
|
50
52
|
});
|
|
51
53
|
logger.debug('debug call, user metadata');
|
|
52
|
-
transport.logLevel = LogLevel.Info;
|
|
54
|
+
transport.logLevel = types_1.LogLevel.Info;
|
|
53
55
|
logger.debug('You should not see this');
|
|
54
56
|
logger.info('You should see this');
|
|
55
57
|
transport.logLevel = undefined;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.test.js","sourceRoot":"","sources":["../../src/logger/index.test.ts"],"names":[],"mappings":"AAAA,
|
|
1
|
+
{"version":3,"file":"index.test.js","sourceRoot":"","sources":["../../src/logger/index.test.ts"],"names":[],"mappings":";;AAAA,wBAAoG;AACpG,0BAA0B;AAC1B,4CAAoD;AACpD,oCAAoC;AAEpC,0CAA0C;AAC1C,uEAAuE;AACvE,QAAQ,CAAC,QAAQ,EAAE,GAAG,EAAE;IACtB,MAAM,MAAM,GAAG,IAAA,eAAY,EAAC;QAC1B,QAAQ,EAAE;YACR,OAAO,EAAE,MAAM;SAChB;KACF,CAAC,CAAC;IAEH,mCAAmC;IACnC,gBAAa,CAAC,MAAM,GAAG,CAAC,CAAC;IACzB,MAAM,SAAS,GAAG,IAAA,mBAAgB,EAAC;QACjC,4BAA4B;QAC5B,sBAAsB,EAAE,EAAE;KAC3B,CAAC,CAAC;IACH,gBAAa,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;IAE9B,EAAE,CAAC,YAAY,EAAE,KAAK,IAAI,EAAE;QAC1B,MAAM,CAAC,GAAG,CAAC;YACT,KAAK,EAAE,MAAM;YACb,OAAO,EAAE,MAAM;SAChB,CAAC,CAAC;QACH,MAAM,CAAC,GAAG,CAAC,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC,CAAC;QAC5B,MAAM,CAAC,GAAG,CAAC,gBA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;QAEH,MAAM,CAAC,KAAK,CAAC,2BAA2B,CAAC,CAAC;QAE1C,SAAS,CAAC,QAAQ,GAAG,gBAAQ,CAAC,IAAI,CAAC;QACnC,MAAM,CAAC,KAAK,CAAC,yBAAyB,CAAC,CAAC;QACxC,MAAM,CAAC,IAAI,CAAC,qBAAqB,CAAC,CAAC;QACnC,SAAS,CAAC,QAAQ,GAAG,SAAS,CAAC;IACjC,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
|
package/lib/logger/utils.js
CHANGED
|
@@ -1,20 +1,24 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getLogMessage = exports.isAboveLevel = void 0;
|
|
4
|
+
const types_1 = require("../types");
|
|
2
5
|
/**
|
|
3
6
|
* Checks if a log level is above the global log level.
|
|
4
7
|
* @param level The log level to check
|
|
5
8
|
* @param checkLevel THe log level to check against
|
|
6
9
|
* @returns Is above the checkLevel?
|
|
7
10
|
*/
|
|
8
|
-
|
|
9
|
-
const levels = [LogLevel.Debug, LogLevel.Info, LogLevel.Warn, LogLevel.Error];
|
|
11
|
+
function isAboveLevel(level, checkLevel) {
|
|
12
|
+
const levels = [types_1.LogLevel.Debug, types_1.LogLevel.Info, types_1.LogLevel.Warn, types_1.LogLevel.Error];
|
|
10
13
|
return levels.indexOf(level) >= levels.indexOf(checkLevel);
|
|
11
14
|
}
|
|
15
|
+
exports.isAboveLevel = isAboveLevel;
|
|
12
16
|
/**
|
|
13
17
|
* Ensures a message output is a string.
|
|
14
18
|
* @param message The message to assert is/convert to a string
|
|
15
19
|
* @returns The message as a string
|
|
16
20
|
*/
|
|
17
|
-
|
|
21
|
+
function getLogMessage(message) {
|
|
18
22
|
if (typeof message === 'string') {
|
|
19
23
|
return message;
|
|
20
24
|
}
|
|
@@ -24,4 +28,5 @@ export function getLogMessage(message) {
|
|
|
24
28
|
}
|
|
25
29
|
return String(message);
|
|
26
30
|
}
|
|
31
|
+
exports.getLogMessage = getLogMessage;
|
|
27
32
|
//# sourceMappingURL=utils.js.map
|
package/lib/logger/utils.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../src/logger/utils.ts"],"names":[],"mappings":"AAAA,
|
|
1
|
+
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../src/logger/utils.ts"],"names":[],"mappings":";;;AAAA,oCAAoC;AAEpC;;;;;GAKG;AACH,SAAgB,YAAY,CAAC,KAAe,EAAE,UAAoB;IAChE,MAAM,MAAM,GAAG,CAAC,gBAAQ,CAAC,KAAK,EAAE,gBAAQ,CAAC,IAAI,EAAE,gBAAQ,CAAC,IAAI,EAAE,gBAAQ,CAAC,KAAK,CAAC,CAAC;IAC9E,OAAO,MAAM,CAAC,OAAO,CAAC,KAAK,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,UAAU,CAAC,CAAC;AAC7D,CAAC;AAHD,oCAGC;AAED;;;;GAIG;AACH,SAAgB,aAAa,CAAC,OAAY;IACxC,IAAI,OAAO,OAAO,KAAK,QAAQ,EAAE;QAC/B,OAAO,OAAO,CAAC;KAChB;IACD,IAAI,OAAO,OAAO,KAAK,QAAQ,IAAI,OAAO,KAAK,IAAI,EAAE;QACnD,MAAM,IAAI,GAAG,EAAE,GAAG,OAAO,EAAE,CAAC;QAC5B,OAAO,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC;KAC7B;IACD,OAAO,MAAM,CAAC,OAAO,CAAC,CAAC;AACzB,CAAC;AATD,sCASC"}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { IReporter, Metadata, ReporterConfigBase, ServiceInfo } from '../types';
|
|
2
|
+
declare type AttributeMap = Record<string, string[] | string | null>;
|
|
3
|
+
declare type AmplifyAutoTrackSource = 'pageView' | 'event' | 'session';
|
|
4
|
+
export interface AmplifyReporterConfig extends ReporterConfigBase {
|
|
5
|
+
/**
|
|
6
|
+
* AWS Region for Amplify.
|
|
7
|
+
*/
|
|
8
|
+
region: string;
|
|
9
|
+
/**
|
|
10
|
+
* The Identity Pool Id to use for reporting, if set to false, Auth.configure is not called.
|
|
11
|
+
* This must be called manually for the reporter to work.
|
|
12
|
+
*/
|
|
13
|
+
identityPoolId: string | false;
|
|
14
|
+
/**
|
|
15
|
+
* The Pinpoint App Id to report to.
|
|
16
|
+
*/
|
|
17
|
+
analyticsAppId: string;
|
|
18
|
+
/** Optional proxy URL */
|
|
19
|
+
proxyUrl?: string;
|
|
20
|
+
/**
|
|
21
|
+
* The Cognito User Pool to configure in Auth.configure.
|
|
22
|
+
* If you are using Cognito, it is better to set identityPoolId to false and configure Auth manually.
|
|
23
|
+
*/
|
|
24
|
+
userPoolId?: string;
|
|
25
|
+
/**
|
|
26
|
+
* The Cognito Web Client Id to configure in Auth.configure.
|
|
27
|
+
* If you are using Cognito, it is better to set identityPoolId to false and configure Auth manually.
|
|
28
|
+
*/
|
|
29
|
+
userPoolWebClientId?: string;
|
|
30
|
+
/**
|
|
31
|
+
* If you want to track which page/url in your webapp is the most frequently viewed one, you can use this feature.
|
|
32
|
+
* It will automatically send events containing url information when the page is visited.
|
|
33
|
+
*/
|
|
34
|
+
autoTrackPageViews?: boolean;
|
|
35
|
+
/**
|
|
36
|
+
* If you want to track user interactions with elements on the page, you can use this feature.
|
|
37
|
+
* All you need to do is attach the specified selectors to your dom element and turn on the auto tracking.
|
|
38
|
+
*/
|
|
39
|
+
autoTrackEvents?: boolean;
|
|
40
|
+
/**
|
|
41
|
+
* A web session can be defined in different ways.
|
|
42
|
+
* To keep it simple we define that the web session is active when the page is not hidden and inactive when the page is hidden.
|
|
43
|
+
*/
|
|
44
|
+
autoTrackSessions?: boolean;
|
|
45
|
+
/**
|
|
46
|
+
* Optional function to run before autotracked analytics events are sent out.
|
|
47
|
+
* The returned metadata is attached to the event.
|
|
48
|
+
*/
|
|
49
|
+
beforeAutoTrack?(source: AmplifyAutoTrackSource): Metadata | undefined;
|
|
50
|
+
/**
|
|
51
|
+
* The data tag prefix to use for attributing HTML elements. Defaults to data-analytics-
|
|
52
|
+
*/
|
|
53
|
+
selectorPrefix?: string;
|
|
54
|
+
/**
|
|
55
|
+
* Modify how the reporter sends events to Amplify.
|
|
56
|
+
*/
|
|
57
|
+
buffering?: AmplifyReporterBufferingConfig;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Configuration options for the buffering behavior of Pinpoint's event tracker.
|
|
61
|
+
*
|
|
62
|
+
* @see https://docs.amplify.aws/lib/analytics/getting-started/q/platform/js/#set-up-existing-analytics-backend
|
|
63
|
+
*/
|
|
64
|
+
declare type AmplifyReporterBufferingConfig = {
|
|
65
|
+
/** Number of items to buffer for sending. */
|
|
66
|
+
bufferSize?: number;
|
|
67
|
+
/** Number of events sent each time Pinpoint flushes. */
|
|
68
|
+
flushSize?: number;
|
|
69
|
+
/** Interval Pinpoint flushes analytics events. Measured in milliseconds. */
|
|
70
|
+
flushInterval?: number;
|
|
71
|
+
/** The maximum number of times Pinpoint will retry to send an event. */
|
|
72
|
+
resendLimit?: number;
|
|
73
|
+
};
|
|
74
|
+
export declare function amplifyReporter(info: ServiceInfo, config: AmplifyReporterConfig): IReporter;
|
|
75
|
+
/**
|
|
76
|
+
* Pinpoint has strict attribute name and value length limits
|
|
77
|
+
*/
|
|
78
|
+
export declare function asAttributeMap(values: Record<string, unknown>, groupValues?: boolean): AttributeMap;
|
|
79
|
+
/**
|
|
80
|
+
* Pinpoint expects `endpoint.attributes` and `endpoint.userAttributes` to have
|
|
81
|
+
* values which are string arrays. This function takes in an object and ensures
|
|
82
|
+
* all of its values are of type `string[]` to appease Pinpoint.
|
|
83
|
+
*/
|
|
84
|
+
export declare function buildAttributeMap(values: Record<string, any>, parentKey?: string | undefined, groupValues?: boolean): AttributeMap;
|
|
85
|
+
export {};
|
|
86
|
+
//# sourceMappingURL=amplifyReporter.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
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;IACxE,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB,CAAC;AAEF,wBAAgB,eAAe,CAAC,IAAI,EAAE,WAAW,EAAE,MAAM,EAAE,qBAAqB,GAAG,SAAS,CA8H3F;AAED;;GAEG;AACH,wBAAgB,cAAc,CAAC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAAE,WAAW,UAAO,GAAG,YAAY,CAqBhG;AAED;;;;GAIG;AACH,wBAAgB,iBAAiB,CAC/B,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EAC3B,SAAS,GAAE,MAAM,GAAG,SAAqB,EACzC,WAAW,UAAO,GACjB,YAAY,CAsBd"}
|
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.buildAttributeMap = exports.asAttributeMap = exports.amplifyReporter = void 0;
|
|
4
|
+
const auth_1 = require("@aws-amplify/auth");
|
|
5
|
+
const analytics_1 = require("@aws-amplify/analytics");
|
|
6
|
+
const protocol_http_1 = require("@aws-sdk/protocol-http");
|
|
7
|
+
const logger_1 = require("../logger");
|
|
8
|
+
function amplifyReporter(info, config) {
|
|
9
|
+
var _a;
|
|
10
|
+
if (config.identityPoolId !== false) {
|
|
11
|
+
auth_1.Auth.configure({
|
|
12
|
+
region: config.region,
|
|
13
|
+
identityPoolId: config.identityPoolId,
|
|
14
|
+
userPoolId: config.userPoolId,
|
|
15
|
+
userPoolWebClientId: config.userPoolWebClientId,
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
const wrapAutoTrackMiddleware = (source) => {
|
|
19
|
+
const { beforeAutoTrack } = config;
|
|
20
|
+
return typeof beforeAutoTrack === 'function' ? () => { var _a; return (_a = beforeAutoTrack(source)) !== null && _a !== void 0 ? _a : {}; } : undefined;
|
|
21
|
+
};
|
|
22
|
+
const allMetadata = asAttributeMap({
|
|
23
|
+
appName: info.service,
|
|
24
|
+
service: info.service,
|
|
25
|
+
domain: window.location.host,
|
|
26
|
+
environment: info.environment,
|
|
27
|
+
version: info.version,
|
|
28
|
+
});
|
|
29
|
+
analytics_1.Analytics.configure({
|
|
30
|
+
region: config.region,
|
|
31
|
+
appId: config.analyticsAppId,
|
|
32
|
+
endpoint: {
|
|
33
|
+
attributes: allMetadata,
|
|
34
|
+
},
|
|
35
|
+
...config.buffering,
|
|
36
|
+
});
|
|
37
|
+
// Session autotracking is enabled by default for backwards compatibility reasons, so we _must_
|
|
38
|
+
// call this unconditionally to ensure we opt out of session tracking when `autoTrackSessions` isn't set
|
|
39
|
+
// See: https://docs.amplify.aws/lib/analytics/autotrack/q/platform/js/#session-tracking
|
|
40
|
+
analytics_1.Analytics.autoTrack('session', {
|
|
41
|
+
enable: config.autoTrackSessions === true,
|
|
42
|
+
attributes: wrapAutoTrackMiddleware('session'),
|
|
43
|
+
});
|
|
44
|
+
if (config.autoTrackPageViews) {
|
|
45
|
+
analytics_1.Analytics.autoTrack('pageView', {
|
|
46
|
+
enable: true,
|
|
47
|
+
eventName: 'pageView',
|
|
48
|
+
type: 'SPA',
|
|
49
|
+
provider: 'AWSPinpoint',
|
|
50
|
+
attributes: wrapAutoTrackMiddleware('pageView'),
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
if (config.autoTrackEvents) {
|
|
54
|
+
analytics_1.Analytics.autoTrack('event', {
|
|
55
|
+
enable: true,
|
|
56
|
+
selectorPrefix: (_a = config.selectorPrefix) !== null && _a !== void 0 ? _a : 'data-analytics-',
|
|
57
|
+
attributes: wrapAutoTrackMiddleware('event'),
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
if (config.proxyUrl) {
|
|
61
|
+
installPinpointProxy(new URL(config.proxyUrl));
|
|
62
|
+
}
|
|
63
|
+
const reporter = {
|
|
64
|
+
trackEvent: function (event) {
|
|
65
|
+
analytics_1.Analytics.record({
|
|
66
|
+
name: event.message,
|
|
67
|
+
attributes: asAttributeMap({
|
|
68
|
+
...event.metadata,
|
|
69
|
+
...event.tags,
|
|
70
|
+
}, false),
|
|
71
|
+
metrics: event.metrics,
|
|
72
|
+
}).catch(() => {
|
|
73
|
+
// Swallow; see: https://crimsonhq.slack.com/archives/G4UN6Q4KF/p1648599302847539
|
|
74
|
+
});
|
|
75
|
+
},
|
|
76
|
+
addBreadcrumb: function (breadcrumb) {
|
|
77
|
+
reporter.trackEvent({
|
|
78
|
+
message: breadcrumb.message,
|
|
79
|
+
metadata: {
|
|
80
|
+
category: breadcrumb.category,
|
|
81
|
+
...breadcrumb.metadata,
|
|
82
|
+
},
|
|
83
|
+
});
|
|
84
|
+
},
|
|
85
|
+
addMetadata: function (metadata) {
|
|
86
|
+
Object.assign(allMetadata, asAttributeMap(metadata, true));
|
|
87
|
+
analytics_1.Analytics.updateEndpoint({
|
|
88
|
+
attributes: allMetadata,
|
|
89
|
+
}).catch(() => {
|
|
90
|
+
// Swallow; see: https://crimsonhq.slack.com/archives/G4UN6Q4KF/p1648599302847539
|
|
91
|
+
});
|
|
92
|
+
},
|
|
93
|
+
setUser: function (user) {
|
|
94
|
+
var _a, _b;
|
|
95
|
+
const userMetadata = user
|
|
96
|
+
? asAttributeMap({
|
|
97
|
+
userId: user.id,
|
|
98
|
+
})
|
|
99
|
+
: {};
|
|
100
|
+
Object.assign(allMetadata, asAttributeMap(userMetadata, true));
|
|
101
|
+
analytics_1.Analytics.updateEndpoint({
|
|
102
|
+
userId: (_a = user === null || user === void 0 ? void 0 : user.id) !== null && _a !== void 0 ? _a : '',
|
|
103
|
+
attributes: allMetadata,
|
|
104
|
+
userAttributes: user
|
|
105
|
+
? asAttributeMap({
|
|
106
|
+
id: user.id,
|
|
107
|
+
email: user.email,
|
|
108
|
+
name: (_b = user.name) !== null && _b !== void 0 ? _b : user.email,
|
|
109
|
+
username: user.username,
|
|
110
|
+
})
|
|
111
|
+
: {},
|
|
112
|
+
}).catch(() => {
|
|
113
|
+
// Swallow; see: https://crimsonhq.slack.com/archives/G4UN6Q4KF/p1648599302847539
|
|
114
|
+
});
|
|
115
|
+
},
|
|
116
|
+
setRouteName: function (routeName) {
|
|
117
|
+
reporter.addMetadata({ routeName });
|
|
118
|
+
},
|
|
119
|
+
setPageName: function (pageName) {
|
|
120
|
+
reporter.addMetadata({ pageName });
|
|
121
|
+
},
|
|
122
|
+
};
|
|
123
|
+
return reporter;
|
|
124
|
+
}
|
|
125
|
+
exports.amplifyReporter = amplifyReporter;
|
|
126
|
+
/**
|
|
127
|
+
* Pinpoint has strict attribute name and value length limits
|
|
128
|
+
*/
|
|
129
|
+
function asAttributeMap(values, groupValues = true) {
|
|
130
|
+
const attributeMap = buildAttributeMap(values, undefined, groupValues);
|
|
131
|
+
const checkedEntries = Object.entries(attributeMap).map(([key, value]) => {
|
|
132
|
+
var _a, _b;
|
|
133
|
+
const truncatedKey = key.length > 50 ? `___${key.slice(-47)}` : key;
|
|
134
|
+
const truncatedValue = Array.isArray(value)
|
|
135
|
+
? (_a = value === null || value === void 0 ? void 0 : value.map((val) => val.slice(0, 100))) !== null && _a !== void 0 ? _a : null
|
|
136
|
+
: (_b = value === null || value === void 0 ? void 0 : value.slice(0, 100)) !== null && _b !== void 0 ? _b : null;
|
|
137
|
+
return [truncatedKey, truncatedValue];
|
|
138
|
+
});
|
|
139
|
+
// Pinpoint only accepts 40 attributes
|
|
140
|
+
if (checkedEntries.length > 40) {
|
|
141
|
+
logger_1.logger.error(`Amplify only allows 40 attributes per event, truncating to 40 attributes`, {
|
|
142
|
+
attributes: checkedEntries,
|
|
143
|
+
});
|
|
144
|
+
checkedEntries.length = 40;
|
|
145
|
+
}
|
|
146
|
+
return Object.fromEntries(checkedEntries);
|
|
147
|
+
}
|
|
148
|
+
exports.asAttributeMap = asAttributeMap;
|
|
149
|
+
/**
|
|
150
|
+
* Pinpoint expects `endpoint.attributes` and `endpoint.userAttributes` to have
|
|
151
|
+
* values which are string arrays. This function takes in an object and ensures
|
|
152
|
+
* all of its values are of type `string[]` to appease Pinpoint.
|
|
153
|
+
*/
|
|
154
|
+
function buildAttributeMap(values, parentKey = undefined, groupValues = true) {
|
|
155
|
+
const valuesWithStringArrays = {};
|
|
156
|
+
Object.entries(values).forEach(([key, value]) => {
|
|
157
|
+
const combinedKey = parentKey ? [parentKey, key].join('.') : key;
|
|
158
|
+
if (!value) {
|
|
159
|
+
valuesWithStringArrays[combinedKey] = null;
|
|
160
|
+
}
|
|
161
|
+
else if (groupValues && Array.isArray(value)) {
|
|
162
|
+
valuesWithStringArrays[combinedKey] = value.map((element) => typeof element === 'string' ? element : JSON.stringify(element));
|
|
163
|
+
}
|
|
164
|
+
else if (typeof value === 'object') {
|
|
165
|
+
const flattenedAttribute = buildAttributeMap(value, combinedKey, groupValues);
|
|
166
|
+
Object.assign(valuesWithStringArrays, flattenedAttribute);
|
|
167
|
+
}
|
|
168
|
+
else {
|
|
169
|
+
const stringValue = typeof value === 'string' ? value : JSON.stringify(value);
|
|
170
|
+
valuesWithStringArrays[combinedKey] = groupValues ? [stringValue] : stringValue;
|
|
171
|
+
}
|
|
172
|
+
});
|
|
173
|
+
return valuesWithStringArrays;
|
|
174
|
+
}
|
|
175
|
+
exports.buildAttributeMap = buildAttributeMap;
|
|
176
|
+
function installPinpointProxy(proxyUrl) {
|
|
177
|
+
// No public API for overriding where the Pinpoint client sends events to... 🤮
|
|
178
|
+
// In theory you can pass in an `endpoint` to the Pinpoint client's constructor like any other AWS
|
|
179
|
+
// client, but Amplify's analytics doesn't expose anything we can use to get an endpoint threaded
|
|
180
|
+
// down to the Pinpoint client's constructor.
|
|
181
|
+
//
|
|
182
|
+
// The Pinpoint client _also_ isn't available synchronously because it is instantiated when events
|
|
183
|
+
// get sent out, and then reconfigured whenever the API credentials change. We need to hook `_initClients`
|
|
184
|
+
// to ensure that the Pinpoint client being used is always patched with our custom endpoint.
|
|
185
|
+
const provider = analytics_1.Analytics.getPluggable('AWSPinpoint');
|
|
186
|
+
if (!provider || typeof provider._initClients !== 'function') {
|
|
187
|
+
logger_1.logger.error('Installation of the Pinpoint proxy failed. This likely means the internals of the @aws-amplify/analytics package have changed.');
|
|
188
|
+
return;
|
|
189
|
+
}
|
|
190
|
+
const originalInitClients = provider._initClients;
|
|
191
|
+
const requestMiddleware = (next) => async (args) => {
|
|
192
|
+
const pinpointClient = provider.pinpointClient;
|
|
193
|
+
if (pinpointClient && proxyUrl.pathname !== '/' && protocol_http_1.HttpRequest.isInstance(args.request)) {
|
|
194
|
+
// Add proxyUrl.pathname to final request url if it was provided
|
|
195
|
+
const shouldStripSlash = proxyUrl.pathname.endsWith('/');
|
|
196
|
+
args.request.path = `${proxyUrl.pathname}${args.request.path.slice(shouldStripSlash ? 1 : 0)}`;
|
|
197
|
+
// Wrap request body so the proxy has signing info
|
|
198
|
+
if (typeof args.request.body === 'string') {
|
|
199
|
+
const credentials = await pinpointClient.config.credentials();
|
|
200
|
+
args.request.body = JSON.stringify({
|
|
201
|
+
credentials,
|
|
202
|
+
data: JSON.parse(args.request.body),
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
return next(args);
|
|
207
|
+
};
|
|
208
|
+
provider._initClients = async (credentials) => {
|
|
209
|
+
const result = await originalInitClients.call(provider, credentials);
|
|
210
|
+
const pinpointClient = provider.pinpointClient;
|
|
211
|
+
if (pinpointClient) {
|
|
212
|
+
pinpointClient.config.endpoint = () => Promise.resolve({
|
|
213
|
+
hostname: proxyUrl.hostname,
|
|
214
|
+
// Passing proxyUrl.pathname here doesn't work; it gets overridden
|
|
215
|
+
path: '/',
|
|
216
|
+
port: undefined,
|
|
217
|
+
protocol: proxyUrl.protocol,
|
|
218
|
+
});
|
|
219
|
+
pinpointClient.middlewareStack.remove(requestMiddleware);
|
|
220
|
+
pinpointClient.middlewareStack.add(requestMiddleware, { step: 'finalizeRequest' });
|
|
221
|
+
}
|
|
222
|
+
return result;
|
|
223
|
+
};
|
|
224
|
+
}
|
|
225
|
+
//# sourceMappingURL=amplifyReporter.js.map
|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