@crimson-education/browser-logger 2.0.2-cognito.2 → 3.0.0
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 +289 -18
- package/lib/index.d.ts +10 -24
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +45 -116
- package/lib/index.js.map +1 -1
- package/lib/logger/consoleTransport.d.ts +37 -0
- package/lib/logger/consoleTransport.d.ts.map +1 -0
- package/lib/logger/consoleTransport.js +81 -0
- package/lib/logger/consoleTransport.js.map +1 -0
- package/lib/logger/datadogTransport.d.ts +8 -0
- package/lib/logger/datadogTransport.d.ts.map +1 -0
- package/lib/logger/datadogTransport.js +21 -0
- package/lib/logger/datadogTransport.js.map +1 -0
- package/lib/logger/index.d.ts +16 -0
- package/lib/logger/index.d.ts.map +1 -0
- package/lib/logger/index.js +148 -0
- package/lib/logger/index.js.map +1 -0
- package/lib/logger/index.test.d.ts +2 -0
- package/lib/logger/index.test.d.ts.map +1 -0
- package/lib/logger/index.test.js +60 -0
- package/lib/logger/index.test.js.map +1 -0
- package/lib/logger/utils.d.ts +15 -0
- package/lib/logger/utils.d.ts.map +1 -0
- package/lib/logger/utils.js +32 -0
- package/lib/logger/utils.js.map +1 -0
- package/lib/reporters/amplifyReporter.d.ts +40 -14
- package/lib/reporters/amplifyReporter.d.ts.map +1 -1
- package/lib/reporters/amplifyReporter.js +15 -23
- package/lib/reporters/amplifyReporter.js.map +1 -1
- package/lib/reporters/amplifyReporter.test.js +0 -11
- package/lib/reporters/amplifyReporter.test.js.map +1 -1
- package/lib/reporters/datadogReporter.d.ts +64 -14
- package/lib/reporters/datadogReporter.d.ts.map +1 -1
- package/lib/reporters/datadogReporter.js +46 -101
- package/lib/reporters/datadogReporter.js.map +1 -1
- package/lib/reporters/gtmReporter.d.ts +3 -2
- package/lib/reporters/gtmReporter.d.ts.map +1 -1
- package/lib/reporters/gtmReporter.js +20 -6
- package/lib/reporters/gtmReporter.js.map +1 -1
- package/lib/reporters/index.d.ts +66 -28
- package/lib/reporters/index.d.ts.map +1 -1
- package/lib/reporters/index.js +210 -0
- package/lib/reporters/index.js.map +1 -1
- package/lib/reporters/logReporter.d.ts +35 -0
- package/lib/reporters/logReporter.d.ts.map +1 -0
- package/lib/reporters/logReporter.js +62 -0
- package/lib/reporters/logReporter.js.map +1 -0
- package/lib/types/index.d.ts +3 -0
- package/lib/types/index.d.ts.map +1 -0
- package/lib/types/index.js +19 -0
- package/lib/types/index.js.map +1 -0
- package/lib/types/logger.d.ts +78 -0
- package/lib/types/logger.d.ts.map +1 -0
- package/lib/{types.js → types/logger.js} +1 -1
- package/lib/types/logger.js.map +1 -0
- package/lib/types/reporter.d.ts +155 -0
- package/lib/types/reporter.d.ts.map +1 -0
- package/lib/types/reporter.js +3 -0
- package/lib/types/reporter.js.map +1 -0
- package/lib/utils.d.ts +9 -4
- package/lib/utils.d.ts.map +1 -1
- package/lib/utils.js +28 -43
- package/lib/utils.js.map +1 -1
- package/lib/utils.test.d.ts +2 -0
- package/lib/utils.test.d.ts.map +1 -0
- package/lib/utils.test.js +32 -0
- package/lib/utils.test.js.map +1 -0
- package/package.json +5 -4
- package/src/index.ts +41 -120
- package/src/logger/consoleTransport.ts +101 -0
- package/src/logger/datadogTransport.ts +20 -0
- package/src/logger/index.test.ts +68 -0
- package/src/logger/index.ts +139 -0
- package/src/logger/utils.ts +28 -0
- package/src/reporters/amplifyReporter.test.ts +1 -14
- package/src/reporters/amplifyReporter.ts +65 -36
- package/src/reporters/datadogReporter.ts +123 -115
- package/src/reporters/gtmReporter.ts +35 -8
- package/src/reporters/index.ts +208 -24
- package/src/reporters/logReporter.ts +86 -0
- package/src/types/index.ts +2 -0
- package/src/types/logger.ts +85 -0
- package/src/types/reporter.ts +167 -0
- package/src/utils.test.ts +32 -0
- package/src/utils.ts +39 -49
- package/lib/types.d.ts +0 -48
- package/lib/types.d.ts.map +0 -1
- package/lib/types.js.map +0 -1
- package/src/types.ts +0 -50
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.consoleTransport = exports.colorize = void 0;
|
|
4
|
+
// from browser-util-inspect
|
|
5
|
+
const colors = {
|
|
6
|
+
bold: [1, 22],
|
|
7
|
+
italic: [3, 23],
|
|
8
|
+
underline: [4, 24],
|
|
9
|
+
inverse: [7, 27],
|
|
10
|
+
white: [37, 39],
|
|
11
|
+
grey: [90, 39],
|
|
12
|
+
black: [30, 39],
|
|
13
|
+
blue: [34, 39],
|
|
14
|
+
cyan: [36, 39],
|
|
15
|
+
green: [32, 39],
|
|
16
|
+
magenta: [35, 39],
|
|
17
|
+
red: [31, 39],
|
|
18
|
+
yellow: [33, 39],
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* Colorize a string with a specified color.
|
|
22
|
+
* @param str String to colorize
|
|
23
|
+
* @param color Color to colorize the string with
|
|
24
|
+
* @returns Colorized string
|
|
25
|
+
*/
|
|
26
|
+
function colorize(str, color) {
|
|
27
|
+
const colorData = colors[color];
|
|
28
|
+
return '\u001b[' + colorData[0] + 'm' + str + '\u001b[' + colorData[1] + 'm';
|
|
29
|
+
}
|
|
30
|
+
exports.colorize = colorize;
|
|
31
|
+
/**
|
|
32
|
+
* Gets a stack trace from metadata or accompanying error.
|
|
33
|
+
*/
|
|
34
|
+
function getStackTrace(data) {
|
|
35
|
+
var _a;
|
|
36
|
+
const stack = ((_a = data === null || data === void 0 ? void 0 : data.err) === null || _a === void 0 ? void 0 : _a.stack) || data.stack || data['error.stack'];
|
|
37
|
+
delete data.stack;
|
|
38
|
+
return stack !== null && stack !== void 0 ? stack : '';
|
|
39
|
+
}
|
|
40
|
+
function consoleTransport(config = {}) {
|
|
41
|
+
var _a, _b, _c;
|
|
42
|
+
// Default ignore patterns
|
|
43
|
+
(_a = config.ignoreMetadataPatterns) !== null && _a !== void 0 ? _a : (config.ignoreMetadataPatterns = ['application', 'service', 'environment', 'version']);
|
|
44
|
+
// Get the colorizer function, or a no-op if not enabled.
|
|
45
|
+
const colorizer = config.colorize !== false ? (_b = config.colorize) !== null && _b !== void 0 ? _b : colorize : (str) => str;
|
|
46
|
+
// Get the log level color function, or use the default colors.
|
|
47
|
+
const getLogLevelColor = (_c = config.getLogLevelColor) !== null && _c !== void 0 ? _c : ((level) => {
|
|
48
|
+
return {
|
|
49
|
+
error: 'red',
|
|
50
|
+
warn: 'yellow',
|
|
51
|
+
info: 'cyan',
|
|
52
|
+
debug: 'green',
|
|
53
|
+
}[level];
|
|
54
|
+
});
|
|
55
|
+
const transport = {
|
|
56
|
+
...config,
|
|
57
|
+
log: ({ level, message, timestamp, splat, ...metadata }) => {
|
|
58
|
+
let splatData = splat !== null && splat !== void 0 ? splat : [];
|
|
59
|
+
const stackTrace = getStackTrace(metadata);
|
|
60
|
+
const strippedInfo = { ...metadata };
|
|
61
|
+
delete strippedInfo['level'];
|
|
62
|
+
delete strippedInfo['message'];
|
|
63
|
+
delete strippedInfo['splat'];
|
|
64
|
+
delete strippedInfo['timestamp'];
|
|
65
|
+
delete strippedInfo['stack'];
|
|
66
|
+
if (Object.keys(strippedInfo).length > 0) {
|
|
67
|
+
splatData = [strippedInfo, ...splatData];
|
|
68
|
+
}
|
|
69
|
+
if (message) {
|
|
70
|
+
splatData = [message, ...splatData];
|
|
71
|
+
}
|
|
72
|
+
if (stackTrace) {
|
|
73
|
+
splatData = [...splatData, stackTrace];
|
|
74
|
+
}
|
|
75
|
+
console.log(`${colorizer(timestamp, 'magenta')} ${colorizer(level, getLogLevelColor(level))}: `, ...splatData);
|
|
76
|
+
},
|
|
77
|
+
};
|
|
78
|
+
return transport;
|
|
79
|
+
}
|
|
80
|
+
exports.consoleTransport = consoleTransport;
|
|
81
|
+
//# sourceMappingURL=consoleTransport.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"consoleTransport.js","sourceRoot":"","sources":["../../src/logger/consoleTransport.ts"],"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,IAAI,MAAM,CAAC,IAAI,CAAC,YAAY,CAAC,CAAC,MAAM,GAAG,CAAC,EAAE;gBACxC,SAAS,GAAG,CAAC,YAAY,EAAE,GAAG,SAAS,CAAC,CAAC;aAC1C;YAED,IAAI,OAAO,EAAE;gBACX,SAAS,GAAG,CAAC,OAAO,EAAE,GAAG,SAAS,CAAC,CAAC;aACrC;YAED,IAAI,UAAU,EAAE;gBACd,SAAS,GAAG,CAAC,GAAG,SAAS,EAAE,UAAU,CAAC,CAAC;aACxC;YAED,OAAO,CAAC,GAAG,CAAC,GAAG,SAAS,CAAC,SAAS,EAAE,SAAS,CAAC,IAAI,SAAS,CAAC,KAAK,EAAE,gBAAgB,CAAC,KAAK,CAAC,CAAC,IAAI,EAAE,GAAG,SAAS,CAAC,CAAC;QACjH,CAAC;KACF,CAAC;IACF,OAAO,SAAS,CAAC;AACnB,CAAC;AAhDD,4CAgDC"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { ILogTransport, LogTransportConfigBase } from '../types';
|
|
2
|
+
export declare type DatadogLogTransportConfig = LogTransportConfigBase;
|
|
3
|
+
/**
|
|
4
|
+
* A transport that sends logs to Datadog.
|
|
5
|
+
* Note: This requires the datadog reporter to be initialized for it to work.
|
|
6
|
+
*/
|
|
7
|
+
export declare function datadogTransport(config?: DatadogLogTransportConfig): ILogTransport;
|
|
8
|
+
//# sourceMappingURL=datadogTransport.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"datadogTransport.d.ts","sourceRoot":"","sources":["../../src/logger/datadogTransport.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,sBAAsB,EAAE,MAAM,UAAU,CAAC;AAIjE,oBAAY,yBAAyB,GAAG,sBAAsB,CAAC;AAE/D;;;GAGG;AACH,wBAAgB,gBAAgB,CAAC,MAAM,GAAE,yBAA8B,iBAStE"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.datadogTransport = void 0;
|
|
4
|
+
const utils_1 = require("./utils");
|
|
5
|
+
const browser_logs_1 = require("@datadog/browser-logs");
|
|
6
|
+
/**
|
|
7
|
+
* A transport that sends logs to Datadog.
|
|
8
|
+
* Note: This requires the datadog reporter to be initialized for it to work.
|
|
9
|
+
*/
|
|
10
|
+
function datadogTransport(config = {}) {
|
|
11
|
+
const transport = {
|
|
12
|
+
...config,
|
|
13
|
+
log: ({ level, message, ...metadata }) => {
|
|
14
|
+
// Datadog expects a string message.
|
|
15
|
+
browser_logs_1.datadogLogs.logger[level]((0, utils_1.getLogMessage)(message), metadata);
|
|
16
|
+
},
|
|
17
|
+
};
|
|
18
|
+
return transport;
|
|
19
|
+
}
|
|
20
|
+
exports.datadogTransport = datadogTransport;
|
|
21
|
+
//# sourceMappingURL=datadogTransport.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"datadogTransport.js","sourceRoot":"","sources":["../../src/logger/datadogTransport.ts"],"names":[],"mappings":";;;AACA,mCAAwC;AACxC,wDAAoD;AAIpD;;;GAGG;AACH,SAAgB,gBAAgB,CAAC,SAAoC,EAAE;IACrE,MAAM,SAAS,GAAkB;QAC/B,GAAG,MAAM;QACT,GAAG,EAAE,CAAC,EAAE,KAAK,EAAE,OAAO,EAAE,GAAG,QAAQ,EAAE,EAAE,EAAE;YACvC,oCAAoC;YACpC,0BAAW,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,IAAA,qBAAa,EAAC,OAAO,CAAC,EAAE,QAAQ,CAAC,CAAC;QAC9D,CAAC;KACF,CAAC;IACF,OAAO,SAAS,CAAC;AACnB,CAAC;AATD,4CASC"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { ILogger, ILogTransport, LoggerOptions, LogLevel, Metadata } from '../types';
|
|
2
|
+
export * from './consoleTransport';
|
|
3
|
+
export * from './datadogTransport';
|
|
4
|
+
export declare const globalMetadata: Metadata;
|
|
5
|
+
export declare const logTransports: ILogTransport[];
|
|
6
|
+
export declare let globalLogLevel: LogLevel | undefined;
|
|
7
|
+
/**
|
|
8
|
+
* Sets the global log level.
|
|
9
|
+
*/
|
|
10
|
+
export declare function setLogLevel(level: LogLevel | null): void;
|
|
11
|
+
/**
|
|
12
|
+
* Creates a Logger Instance.
|
|
13
|
+
*/
|
|
14
|
+
export declare function createLogger(options?: LoggerOptions): ILogger;
|
|
15
|
+
export declare const logger: ILogger;
|
|
16
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/logger/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,OAAO,EAAE,aAAa,EAAE,aAAa,EAAE,QAAQ,EAAE,QAAQ,EAA2B,MAAM,UAAU,CAAC;AAK9G,cAAc,oBAAoB,CAAC;AACnC,cAAc,oBAAoB,CAAC;AAEnC,eAAO,MAAM,cAAc,EAAE,QAAa,CAAC;AAE3C,eAAO,MAAM,aAAa,EAAE,aAAa,EAAyB,CAAC;AAEnE,eAAO,IAAI,cAAc,EAAE,QAAQ,GAAG,SAAS,CAAC;AAChD;;GAEG;AACH,wBAAgB,WAAW,CAAC,KAAK,EAAE,QAAQ,GAAG,IAAI,GAAG,IAAI,CAExD;AAED;;GAEG;AACH,wBAAgB,YAAY,CAAC,OAAO,GAAE,aAAkB,GAAG,OAAO,CAgHjE;AAED,eAAO,MAAM,MAAM,SAAiB,CAAC"}
|
|
@@ -0,0 +1,148 @@
|
|
|
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;
|
|
18
|
+
/* eslint-disable prefer-rest-params */
|
|
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)()];
|
|
27
|
+
/**
|
|
28
|
+
* Sets the global log level.
|
|
29
|
+
*/
|
|
30
|
+
function setLogLevel(level) {
|
|
31
|
+
exports.globalLogLevel = level !== null && level !== void 0 ? level : undefined;
|
|
32
|
+
}
|
|
33
|
+
exports.setLogLevel = setLogLevel;
|
|
34
|
+
/**
|
|
35
|
+
* Creates a Logger Instance.
|
|
36
|
+
*/
|
|
37
|
+
function createLogger(options = {}) {
|
|
38
|
+
var _a;
|
|
39
|
+
const rootMetadata = (_a = options.metadata) !== null && _a !== void 0 ? _a : {};
|
|
40
|
+
const logger = {
|
|
41
|
+
// impl loosely based on Winston Logger's dynamic log function
|
|
42
|
+
// https://github.com/winstonjs/winston/blob/master/lib/winston/logger.js
|
|
43
|
+
log() {
|
|
44
|
+
var _a, _b;
|
|
45
|
+
let logInfo;
|
|
46
|
+
// Optimize for the hotpath of logging JSON literals
|
|
47
|
+
if (arguments.length === 1) {
|
|
48
|
+
logInfo = {
|
|
49
|
+
level: types_1.LogLevel.Info,
|
|
50
|
+
...arguments[0],
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
// Slightly less hotpath, but worth optimizing for.
|
|
54
|
+
else if (arguments.length === 2) {
|
|
55
|
+
const [level, message] = Array.from(arguments);
|
|
56
|
+
if (message && typeof message === 'object') {
|
|
57
|
+
logInfo = {
|
|
58
|
+
...message,
|
|
59
|
+
level: (_a = message.level) !== null && _a !== void 0 ? _a : level,
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
else {
|
|
63
|
+
logInfo = {
|
|
64
|
+
level,
|
|
65
|
+
message,
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
else {
|
|
70
|
+
const [level, message, ...splat] = Array.from(arguments);
|
|
71
|
+
const [meta] = splat;
|
|
72
|
+
if (typeof meta === 'object' && meta !== null) {
|
|
73
|
+
logInfo = {
|
|
74
|
+
...meta,
|
|
75
|
+
level,
|
|
76
|
+
splat: splat.slice(1),
|
|
77
|
+
message,
|
|
78
|
+
};
|
|
79
|
+
if (meta.message) {
|
|
80
|
+
logInfo.message = `${(0, utils_2.getLogMessage)(logInfo.message)} ${(0, utils_2.getLogMessage)(meta.message)}`;
|
|
81
|
+
}
|
|
82
|
+
if (meta.stack) {
|
|
83
|
+
logInfo.stack = meta.stack;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
else {
|
|
87
|
+
logInfo = {
|
|
88
|
+
level,
|
|
89
|
+
splat,
|
|
90
|
+
message,
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
// Check if we should log this message
|
|
95
|
+
const minLevel = exports.globalLogLevel !== null && exports.globalLogLevel !== void 0 ? exports.globalLogLevel : options.logLevel;
|
|
96
|
+
if (minLevel && !(0, utils_2.isAboveLevel)(logInfo.level, minLevel)) {
|
|
97
|
+
return logger;
|
|
98
|
+
}
|
|
99
|
+
// Add timestamp and root metadata.
|
|
100
|
+
// Ensure there is a message property.
|
|
101
|
+
logInfo = {
|
|
102
|
+
...exports.globalMetadata,
|
|
103
|
+
...rootMetadata,
|
|
104
|
+
timestamp: new Date().toISOString(),
|
|
105
|
+
...logInfo,
|
|
106
|
+
message: (_b = logInfo.message) !== null && _b !== void 0 ? _b : '',
|
|
107
|
+
};
|
|
108
|
+
for (const transport of exports.logTransports) {
|
|
109
|
+
if (transport.logLevel) {
|
|
110
|
+
if (!(0, utils_2.isAboveLevel)(logInfo.level, transport.logLevel)) {
|
|
111
|
+
continue;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
transport.log({
|
|
115
|
+
...(0, utils_1.filterReporterMetadata)(logInfo, transport),
|
|
116
|
+
// These would break the transport if excluded.
|
|
117
|
+
level: logInfo.level,
|
|
118
|
+
message: logInfo.message,
|
|
119
|
+
splat: logInfo.splat,
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
return logger;
|
|
123
|
+
},
|
|
124
|
+
child(metadata) {
|
|
125
|
+
return createLogger({ ...rootMetadata, metadata });
|
|
126
|
+
},
|
|
127
|
+
startTimer() {
|
|
128
|
+
const start = new Date();
|
|
129
|
+
return {
|
|
130
|
+
logger: this,
|
|
131
|
+
done: ({ message, level, metadata } = {}) => {
|
|
132
|
+
const duration = new Date().getTime() - start.getTime();
|
|
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 : {}) });
|
|
134
|
+
return true;
|
|
135
|
+
},
|
|
136
|
+
};
|
|
137
|
+
},
|
|
138
|
+
// Forward the log levels into the log function, essentially shortcuts.
|
|
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),
|
|
143
|
+
};
|
|
144
|
+
return logger;
|
|
145
|
+
}
|
|
146
|
+
exports.createLogger = createLogger;
|
|
147
|
+
exports.logger = createLogger();
|
|
148
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
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,mDAAmD;iBAC9C,IAAI,SAAS,CAAC,MAAM,KAAK,CAAC,EAAE;gBAC/B,MAAM,CAAC,KAAK,EAAE,OAAO,CAAC,GAAG,KAAK,CAAC,IAAI,CAAC,SAAS,CAA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E,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"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.test.d.ts","sourceRoot":"","sources":["../../src/logger/index.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,60 @@
|
|
|
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");
|
|
7
|
+
// This isn't actually asserting anything.
|
|
8
|
+
// But it ensures that the logs look correct, and no errors get thrown.
|
|
9
|
+
describe('logger', () => {
|
|
10
|
+
const logger = (0, _1.createLogger)({
|
|
11
|
+
metadata: {
|
|
12
|
+
service: 'test',
|
|
13
|
+
},
|
|
14
|
+
});
|
|
15
|
+
// Manually set the log transports.
|
|
16
|
+
_1.logTransports.length = 0;
|
|
17
|
+
const transport = (0, _1.consoleTransport)({
|
|
18
|
+
// So we can see everything.
|
|
19
|
+
ignoreMetadataPatterns: [],
|
|
20
|
+
});
|
|
21
|
+
_1.logTransports.push(transport);
|
|
22
|
+
it('should log', async () => {
|
|
23
|
+
logger.log({
|
|
24
|
+
level: 'info',
|
|
25
|
+
message: 'data',
|
|
26
|
+
});
|
|
27
|
+
logger.log({ data: 'obj' });
|
|
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)({
|
|
31
|
+
service: 'test-service',
|
|
32
|
+
environment: 'test',
|
|
33
|
+
version: 'no-version',
|
|
34
|
+
defaultMetadata: {
|
|
35
|
+
application: 'test',
|
|
36
|
+
},
|
|
37
|
+
});
|
|
38
|
+
const infoLogger = logger.child({ isLevel: 'info' });
|
|
39
|
+
infoLogger.info('info call, message');
|
|
40
|
+
infoLogger.info('info call, message, metadata', { data: 'metadataobj' });
|
|
41
|
+
const timer = logger.startTimer();
|
|
42
|
+
await new Promise((resolve) => setTimeout(resolve, 1000));
|
|
43
|
+
timer.done({ message: 'timer done', metadata: { id: 1 } });
|
|
44
|
+
(0, _1.setLogLevel)(types_1.LogLevel.Info);
|
|
45
|
+
(0, reporters_1.addMetadata)({ global: 'metadata' });
|
|
46
|
+
logger.debug('You should not see this');
|
|
47
|
+
logger.info('You should see this with { global: "metadata" }');
|
|
48
|
+
(0, _1.setLogLevel)(null);
|
|
49
|
+
(0, reporters_1.setUser)({
|
|
50
|
+
id: '123',
|
|
51
|
+
email: 'test@example.com',
|
|
52
|
+
});
|
|
53
|
+
logger.debug('debug call, user metadata');
|
|
54
|
+
transport.logLevel = types_1.LogLevel.Info;
|
|
55
|
+
logger.debug('You should not see this');
|
|
56
|
+
logger.info('You should see this');
|
|
57
|
+
transport.logLevel = undefined;
|
|
58
|
+
});
|
|
59
|
+
});
|
|
60
|
+
//# sourceMappingURL=index.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
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,gBAAQ,CAAC,KAAK,EAAE,gBAAgB,EAAE,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC,CAAC;QAC9D,MAAM,CAAC,GAAG,CAAC,gBAAQ,CAAC,KAAK,EAAE,0BAA0B,EAAE,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC,CAAC;QAExE,IAAA,QAAI,EAAC;YACH,OAAO,EAAE,cAAc;YACvB,WAAW,EAAE,MAAM;YACnB,OAAO,EAAE,YAAY;YACrB,eAA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AC"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { LogLevel } from '../types';
|
|
2
|
+
/**
|
|
3
|
+
* Checks if a log level is above the global log level.
|
|
4
|
+
* @param level The log level to check
|
|
5
|
+
* @param checkLevel THe log level to check against
|
|
6
|
+
* @returns Is above the checkLevel?
|
|
7
|
+
*/
|
|
8
|
+
export declare function isAboveLevel(level: LogLevel, checkLevel: LogLevel): boolean;
|
|
9
|
+
/**
|
|
10
|
+
* Ensures a message output is a string.
|
|
11
|
+
* @param message The message to assert is/convert to a string
|
|
12
|
+
* @returns The message as a string
|
|
13
|
+
*/
|
|
14
|
+
export declare function getLogMessage(message: any): string;
|
|
15
|
+
//# sourceMappingURL=utils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../src/logger/utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,UAAU,CAAC;AAEpC;;;;;GAKG;AACH,wBAAgB,YAAY,CAAC,KAAK,EAAE,QAAQ,EAAE,UAAU,EAAE,QAAQ,GAAG,OAAO,CAG3E;AAED;;;;GAIG;AACH,wBAAgB,aAAa,CAAC,OAAO,EAAE,GAAG,GAAG,MAAM,CASlD"}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getLogMessage = exports.isAboveLevel = void 0;
|
|
4
|
+
const types_1 = require("../types");
|
|
5
|
+
/**
|
|
6
|
+
* Checks if a log level is above the global log level.
|
|
7
|
+
* @param level The log level to check
|
|
8
|
+
* @param checkLevel THe log level to check against
|
|
9
|
+
* @returns Is above the checkLevel?
|
|
10
|
+
*/
|
|
11
|
+
function isAboveLevel(level, checkLevel) {
|
|
12
|
+
const levels = [types_1.LogLevel.Debug, types_1.LogLevel.Info, types_1.LogLevel.Warn, types_1.LogLevel.Error];
|
|
13
|
+
return levels.indexOf(level) >= levels.indexOf(checkLevel);
|
|
14
|
+
}
|
|
15
|
+
exports.isAboveLevel = isAboveLevel;
|
|
16
|
+
/**
|
|
17
|
+
* Ensures a message output is a string.
|
|
18
|
+
* @param message The message to assert is/convert to a string
|
|
19
|
+
* @returns The message as a string
|
|
20
|
+
*/
|
|
21
|
+
function getLogMessage(message) {
|
|
22
|
+
if (typeof message === 'string') {
|
|
23
|
+
return message;
|
|
24
|
+
}
|
|
25
|
+
if (typeof message === 'object' && message !== null) {
|
|
26
|
+
const data = { ...message };
|
|
27
|
+
return JSON.stringify(data);
|
|
28
|
+
}
|
|
29
|
+
return String(message);
|
|
30
|
+
}
|
|
31
|
+
exports.getLogMessage = getLogMessage;
|
|
32
|
+
//# sourceMappingURL=utils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
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"}
|
|
@@ -1,23 +1,53 @@
|
|
|
1
|
-
import { IReporter } from '
|
|
2
|
-
|
|
3
|
-
export
|
|
1
|
+
import { IReporter, ReporterConfigBase, ServiceInfo } from '../types';
|
|
2
|
+
declare type AttributeMap = Record<string, string[] | string | null>;
|
|
3
|
+
export interface AmplifyReporterConfig extends ReporterConfigBase {
|
|
4
|
+
/**
|
|
5
|
+
* AWS Region for Amplify.
|
|
6
|
+
*/
|
|
4
7
|
region: string;
|
|
5
8
|
/**
|
|
6
|
-
* The Identity Pool
|
|
9
|
+
* The Identity Pool Id to use for reporting, if set to false, Auth.configure is not called.
|
|
7
10
|
* This must be called manually for the reporter to work.
|
|
8
11
|
*/
|
|
9
12
|
identityPoolId: string | false;
|
|
13
|
+
/**
|
|
14
|
+
* The Pinpoint App Id to report to.
|
|
15
|
+
*/
|
|
10
16
|
analyticsAppId: string;
|
|
17
|
+
/**
|
|
18
|
+
* The Cognito User Pool to configure in Auth.configure.
|
|
19
|
+
* If you are using Cognito, it is better to set identityPoolId to false and configure Auth manually.
|
|
20
|
+
*/
|
|
21
|
+
userPoolId?: string;
|
|
22
|
+
/**
|
|
23
|
+
* The Cognito Web Client Id to configure in Auth.configure.
|
|
24
|
+
* If you are using Cognito, it is better to set identityPoolId to false and configure Auth manually.
|
|
25
|
+
*/
|
|
26
|
+
userPoolWebClientId?: string;
|
|
27
|
+
/**
|
|
28
|
+
* If you want to track which page/url in your webapp is the most frequently viewed one, you can use this feature.
|
|
29
|
+
* It will automatically send events containing url information when the page is visited.
|
|
30
|
+
*/
|
|
11
31
|
autoTrackPageViews?: boolean;
|
|
32
|
+
/**
|
|
33
|
+
* If you want to track user interactions with elements on the page, you can use this feature.
|
|
34
|
+
* All you need to do is attach the specified selectors to your dom element and turn on the auto tracking.
|
|
35
|
+
*/
|
|
12
36
|
autoTrackEvents?: boolean;
|
|
37
|
+
/**
|
|
38
|
+
* A web session can be defined in different ways.
|
|
39
|
+
* 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.
|
|
40
|
+
*/
|
|
13
41
|
autoTrackSessions?: boolean;
|
|
42
|
+
/**
|
|
43
|
+
* The data tag prefix to use for attributing HTML elements. Defaults to data-analytics-
|
|
44
|
+
*/
|
|
14
45
|
selectorPrefix?: string;
|
|
15
|
-
|
|
46
|
+
/**
|
|
47
|
+
* Modify how the reporter sends events to Amplify.
|
|
48
|
+
*/
|
|
16
49
|
buffering?: AmplifyReporterBufferingConfig;
|
|
17
|
-
|
|
18
|
-
userPoolWebClientId?: string;
|
|
19
|
-
ignoreMetadataPatterns?: RegExp[];
|
|
20
|
-
};
|
|
50
|
+
}
|
|
21
51
|
/**
|
|
22
52
|
* Configuration options for the buffering behavior of Pinpoint's event tracker.
|
|
23
53
|
*
|
|
@@ -34,19 +64,15 @@ declare type AmplifyReporterBufferingConfig = {
|
|
|
34
64
|
resendLimit?: number;
|
|
35
65
|
};
|
|
36
66
|
export declare function amplifyReporter(info: ServiceInfo, config: AmplifyReporterConfig): IReporter;
|
|
37
|
-
declare type AttributeMap = Record<string, string[] | string | null>;
|
|
38
67
|
/**
|
|
39
68
|
* Pinpoint has strict attribute name and value length limits
|
|
40
69
|
*/
|
|
41
|
-
export declare function asAttributeMap(values: Record<string,
|
|
70
|
+
export declare function asAttributeMap(values: Record<string, unknown>, groupValues?: boolean): AttributeMap;
|
|
42
71
|
/**
|
|
43
72
|
* Pinpoint expects `endpoint.attributes` and `endpoint.userAttributes` to have
|
|
44
73
|
* values which are string arrays. This function takes in an object and ensures
|
|
45
74
|
* all of its values are of type `string[]` to appease Pinpoint.
|
|
46
75
|
*/
|
|
47
76
|
export declare function buildAttributeMap(values: Record<string, any>, parentKey?: string | undefined, groupValues?: boolean): AttributeMap;
|
|
48
|
-
export declare function filterAttributeMap(attributes: AttributeMap | Record<string, string>, ignorePatterns: RegExp[]): {
|
|
49
|
-
[k: string]: string | string[] | null;
|
|
50
|
-
};
|
|
51
77
|
export {};
|
|
52
78
|
//# sourceMappingURL=amplifyReporter.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"amplifyReporter.d.ts","sourceRoot":"","sources":["../../src/reporters/amplifyReporter.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"amplifyReporter.d.ts","sourceRoot":"","sources":["../../src/reporters/amplifyReporter.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,SAAS,EAGT,kBAAkB,EAGlB,WAAW,EACZ,MAAM,UAAU,CAAC;AAMlB,aAAK,YAAY,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,EAAE,GAAG,MAAM,GAAG,IAAI,CAAC,CAAC;AAE7D,MAAM,WAAW,qBAAsB,SAAQ,kBAAkB;IAC/D;;OAEG;IACH,MAAM,EAAE,MAAM,CAAC;IACf;;;OAGG;IACH,cAAc,EAAE,MAAM,GAAG,KAAK,CAAC;IAC/B;;OAEG;IACH,cAAc,EAAE,MAAM,CAAC;IACvB;;;OAGG;IACH,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB;;;OAGG;IACH,mBAAmB,CAAC,EAAE,MAAM,CAAC;IAE7B;;;OAGG;IACH,kBAAkB,CAAC,EAAE,OAAO,CAAC;IAC7B;;;OAGG;IACH,eAAe,CAAC,EAAE,OAAO,CAAC;IAC1B;;;OAGG;IACH,iBAAiB,CAAC,EAAE,OAAO,CAAC;IAE5B;;OAEG;IACH,cAAc,CAAC,EAAE,MAAM,CAAC;IAExB;;OAEG;IACH,SAAS,CAAC,EAAE,8BAA8B,CAAC;CAC5C;AAED;;;;GAIG;AACH,aAAK,8BAA8B,GAAG;IACpC,6CAA6C;IAC7C,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,wDAAwD;IACxD,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,4EAA4E;IAC5E,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,wEAAwE;IACxE,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB,CAAC;AAEF,wBAAgB,eAAe,CAAC,IAAI,EAAE,WAAW,EAAE,MAAM,EAAE,qBAAqB,GAAG,SAAS,CAmG3F;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"}
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.buildAttributeMap = exports.asAttributeMap = exports.amplifyReporter = void 0;
|
|
4
4
|
const auth_1 = require("@aws-amplify/auth");
|
|
5
5
|
const analytics_1 = require("@aws-amplify/analytics");
|
|
6
|
+
const logger_1 = require("../logger");
|
|
6
7
|
function amplifyReporter(info, config) {
|
|
7
8
|
var _a;
|
|
8
9
|
if (config.identityPoolId !== false) {
|
|
@@ -17,7 +18,7 @@ function amplifyReporter(info, config) {
|
|
|
17
18
|
appName: info.service,
|
|
18
19
|
service: info.service,
|
|
19
20
|
domain: window.location.host,
|
|
20
|
-
environment: info.
|
|
21
|
+
environment: info.environment,
|
|
21
22
|
version: info.version,
|
|
22
23
|
});
|
|
23
24
|
analytics_1.Analytics.configure({
|
|
@@ -50,15 +51,11 @@ function amplifyReporter(info, config) {
|
|
|
50
51
|
attributes: asAttributeMap({
|
|
51
52
|
...event.metadata,
|
|
52
53
|
...event.tags,
|
|
53
|
-
}, false
|
|
54
|
+
}, false),
|
|
54
55
|
metrics: event.metrics,
|
|
55
56
|
});
|
|
56
57
|
},
|
|
57
58
|
addBreadcrumb: function (breadcrumb) {
|
|
58
|
-
var _a;
|
|
59
|
-
if (breadcrumb.category && ((_a = config.ignoreBreadcrumbCategories) === null || _a === void 0 ? void 0 : _a.includes(breadcrumb.category))) {
|
|
60
|
-
return;
|
|
61
|
-
}
|
|
62
59
|
reporter.trackEvent({
|
|
63
60
|
message: breadcrumb.message,
|
|
64
61
|
metadata: {
|
|
@@ -68,7 +65,7 @@ function amplifyReporter(info, config) {
|
|
|
68
65
|
});
|
|
69
66
|
},
|
|
70
67
|
addMetadata: function (metadata) {
|
|
71
|
-
Object.assign(allMetadata, asAttributeMap(metadata, true
|
|
68
|
+
Object.assign(allMetadata, asAttributeMap(metadata, true));
|
|
72
69
|
analytics_1.Analytics.updateEndpoint({
|
|
73
70
|
attributes: allMetadata,
|
|
74
71
|
}).catch(() => {
|
|
@@ -97,9 +94,6 @@ function amplifyReporter(info, config) {
|
|
|
97
94
|
setPageName: function (pageName) {
|
|
98
95
|
reporter.addMetadata({ pageName });
|
|
99
96
|
},
|
|
100
|
-
reportError: function () { },
|
|
101
|
-
recordSession: function () { },
|
|
102
|
-
recordSessionStop: function () { },
|
|
103
97
|
};
|
|
104
98
|
return reporter;
|
|
105
99
|
}
|
|
@@ -107,11 +101,9 @@ exports.amplifyReporter = amplifyReporter;
|
|
|
107
101
|
/**
|
|
108
102
|
* Pinpoint has strict attribute name and value length limits
|
|
109
103
|
*/
|
|
110
|
-
|
|
111
|
-
function asAttributeMap(values, groupValues = true, ignorePatterns = []) {
|
|
104
|
+
function asAttributeMap(values, groupValues = true) {
|
|
112
105
|
const attributeMap = buildAttributeMap(values, undefined, groupValues);
|
|
113
|
-
const
|
|
114
|
-
const checkedEntries = Object.entries(filteredAttributeMap).map(([key, value]) => {
|
|
106
|
+
const checkedEntries = Object.entries(attributeMap).map(([key, value]) => {
|
|
115
107
|
var _a, _b;
|
|
116
108
|
const truncatedKey = key.length > 50 ? `___${key.slice(-47)}` : key;
|
|
117
109
|
const truncatedValue = Array.isArray(value)
|
|
@@ -119,6 +111,13 @@ function asAttributeMap(values, groupValues = true, ignorePatterns = []) {
|
|
|
119
111
|
: (_b = value === null || value === void 0 ? void 0 : value.slice(0, 100)) !== null && _b !== void 0 ? _b : null;
|
|
120
112
|
return [truncatedKey, truncatedValue];
|
|
121
113
|
});
|
|
114
|
+
// Pinpoint only accepts 40 attributes
|
|
115
|
+
if (checkedEntries.length > 40) {
|
|
116
|
+
logger_1.logger.error(`Amplify only allows 40 attributes per event, truncating to 40 attributes`, {
|
|
117
|
+
attributes: checkedEntries,
|
|
118
|
+
});
|
|
119
|
+
checkedEntries.length = 40;
|
|
120
|
+
}
|
|
122
121
|
return Object.fromEntries(checkedEntries);
|
|
123
122
|
}
|
|
124
123
|
exports.asAttributeMap = asAttributeMap;
|
|
@@ -127,9 +126,7 @@ exports.asAttributeMap = asAttributeMap;
|
|
|
127
126
|
* values which are string arrays. This function takes in an object and ensures
|
|
128
127
|
* all of its values are of type `string[]` to appease Pinpoint.
|
|
129
128
|
*/
|
|
130
|
-
function buildAttributeMap(
|
|
131
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
132
|
-
values, parentKey = undefined, groupValues = true) {
|
|
129
|
+
function buildAttributeMap(values, parentKey = undefined, groupValues = true) {
|
|
133
130
|
const valuesWithStringArrays = {};
|
|
134
131
|
Object.entries(values).forEach(([key, value]) => {
|
|
135
132
|
const combinedKey = parentKey ? [parentKey, key].join('.') : key;
|
|
@@ -151,9 +148,4 @@ values, parentKey = undefined, groupValues = true) {
|
|
|
151
148
|
return valuesWithStringArrays;
|
|
152
149
|
}
|
|
153
150
|
exports.buildAttributeMap = buildAttributeMap;
|
|
154
|
-
function filterAttributeMap(attributes, ignorePatterns) {
|
|
155
|
-
const entries = Object.entries(attributes);
|
|
156
|
-
return Object.fromEntries(entries.filter(([key]) => !ignorePatterns.some((pattern) => pattern.test(key))));
|
|
157
|
-
}
|
|
158
|
-
exports.filterAttributeMap = filterAttributeMap;
|
|
159
151
|
//# sourceMappingURL=amplifyReporter.js.map
|