UNPKG

@aspecto/logger

Version:

_How to install_

109 lines 3.77 kB
"use strict"; var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; result["default"] = mod; return result; }; var _a, _b, _c, _d, _e; Object.defineProperty(exports, "__esModule", { value: true }); const os = __importStar(require("os")); const serialize_error_1 = require("serialize-error"); const types_1 = require("./types"); const aspectoProdLoggerProcessDebug = !!((_a = process.env) === null || _a === void 0 ? void 0 : _a.ASPECTO_PROD_LOGGER_PROCESS_DEBUG); const aspectoProdLoggerProcessTrace = !!((_b = process.env) === null || _b === void 0 ? void 0 : _b.ASPECTO_PROD_LOGGER_PROCESS_TRACE); let getContext; try { getContext = (_c = require('@aspecto/opentelemetry')) === null || _c === void 0 ? void 0 : _c.getContext; } catch (_f) { } const metadata = { env: (_e = (_d = process) === null || _d === void 0 ? void 0 : _d.env) === null || _e === void 0 ? void 0 : _e.NODE_ENV, hostname: os.hostname(), }; try { const packageJson = require(`${process.cwd()}/package.json`); if (packageJson.version) { metadata['appVersion'] = packageJson.version; } if (packageJson.name) { metadata['appName'] = packageJson.name; } } catch (_g) { } const trySerialize = (val) => { try { if (val && val.isAxiosError) { val = convertAxiosError(val); } return serialize_error_1.serializeError(val); } catch (_a) { return val; } }; const convertAxiosError = (val) => { var _a, _b, _c, _d, _e, _f; const { request, response, config, message, name, stack, isAxiosError } = val; return { isAxiosError, name, stack, message, config: { method: (_a = config) === null || _a === void 0 ? void 0 : _a.method, url: (_b = config) === null || _b === void 0 ? void 0 : _b.url, }, request: { host: (_c = request) === null || _c === void 0 ? void 0 : _c.host, path: (_d = request) === null || _d === void 0 ? void 0 : _d.path, }, response: { status: (_e = response) === null || _e === void 0 ? void 0 : _e.status, statusText: (_f = response) === null || _f === void 0 ? void 0 : _f.statusText, }, }; }; const productionLogger = (level, outputAs, message, extra) => { var _a, _b, _c, _d; if (level === types_1.LogLevel.Debug && !aspectoProdLoggerProcessDebug) { return; } if (level === types_1.LogLevel.Trace && !aspectoProdLoggerProcessTrace) { return; } let contextTraceId; let contextSpanId; let sampled; try { const context = (_a = getContext) === null || _a === void 0 ? void 0 : _a(); contextTraceId = (_b = context) === null || _b === void 0 ? void 0 : _b.traceId; contextSpanId = (_c = context) === null || _c === void 0 ? void 0 : _c.spanId; sampled = (_d = context) === null || _d === void 0 ? void 0 : _d.sampled; } catch (_e) { } const log = { level, message, metadata, contextTraceId, contextSpanId, sampled, }; if (extra instanceof Error) { log.error = trySerialize(extra); } else if (extra) { Object.entries(extra).forEach(([key, value]) => { if (value instanceof Error) { log.error = trySerialize(value); delete extra[key]; } }); log.extra = extra; } console[outputAs](JSON.stringify(log)); }; exports.default = productionLogger; //# sourceMappingURL=prod-logger.js.map