@grafana/faro-web-sdk
Version:
Faro instrumentations, metas, transports for web.
58 lines • 3.78 kB
JavaScript
import { allLogLevels, BaseInstrumentation, defaultErrorArgsSerializer, defaultLogArgsSerializer, LogLevel, VERSION, } from '@grafana/faro-core';
import { getDetailsFromConsoleErrorArgs } from '../errors/getErrorDetails';
export class ConsoleInstrumentation extends BaseInstrumentation {
constructor() {
super(...arguments);
this.name = '@grafana/faro-web-sdk:instrumentation-console';
this.version = VERSION;
this.errorSerializer = defaultLogArgsSerializer;
}
initialize() {
var _a;
const instrumentationOptions = this.config.consoleInstrumentation;
const serializeErrors = (instrumentationOptions === null || instrumentationOptions === void 0 ? void 0 : instrumentationOptions.serializeErrors) || !!(instrumentationOptions === null || instrumentationOptions === void 0 ? void 0 : instrumentationOptions.errorSerializer);
this.errorSerializer = serializeErrors
? ((_a = instrumentationOptions === null || instrumentationOptions === void 0 ? void 0 : instrumentationOptions.errorSerializer) !== null && _a !== void 0 ? _a : defaultErrorArgsSerializer)
: defaultLogArgsSerializer;
allLogLevels
.filter((level) => { var _a; return !((_a = instrumentationOptions === null || instrumentationOptions === void 0 ? void 0 : instrumentationOptions.disabledLevels) !== null && _a !== void 0 ? _a : ConsoleInstrumentation.defaultDisabledLevels).includes(level); })
.forEach((level) => {
/* eslint-disable-next-line no-console */
console[level] = (...args) => {
try {
if (level === LogLevel.ERROR && !(instrumentationOptions === null || instrumentationOptions === void 0 ? void 0 : instrumentationOptions.consoleErrorAsLog)) {
const { value, type, stackFrames } = getDetailsFromConsoleErrorArgs(args, this.errorSerializer);
if (value && !type && !stackFrames) {
this.api.pushError(new Error(ConsoleInstrumentation.consoleErrorPrefix + value));
return;
}
this.api.pushError(new Error(ConsoleInstrumentation.consoleErrorPrefix + value), { type, stackFrames });
}
else if (level === LogLevel.ERROR && (instrumentationOptions === null || instrumentationOptions === void 0 ? void 0 : instrumentationOptions.consoleErrorAsLog)) {
const { value, type, stackFrames } = getDetailsFromConsoleErrorArgs(args, this.errorSerializer);
this.api.pushLog(value ? [ConsoleInstrumentation.consoleErrorPrefix + value] : args, {
level,
context: {
value: value !== null && value !== void 0 ? value : '',
type: type !== null && type !== void 0 ? type : '',
stackFrames: (stackFrames === null || stackFrames === void 0 ? void 0 : stackFrames.length) ? defaultErrorArgsSerializer(stackFrames) : '',
},
});
}
else {
this.api.pushLog(args, { level });
}
}
catch (err) {
this.logError(err);
}
finally {
this.unpatchedConsole[level](...args);
}
};
});
}
}
ConsoleInstrumentation.defaultDisabledLevels = [LogLevel.DEBUG, LogLevel.TRACE, LogLevel.LOG];
ConsoleInstrumentation.consoleErrorPrefix = 'console.error: ';
//# sourceMappingURL=instrumentation.js.map