UNPKG

ton-logger

Version:

Logger set and configurations for datadog

214 lines (213 loc) 6.74 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const object_path_immutable_1 = __importDefault(require("object-path-immutable")); const pino_1 = __importDefault(require("pino")); const get_correlation_id_1 = __importDefault(require("./get-correlation-id")); const get_device_1 = __importDefault(require("./get-device")); const get_http_1 = __importDefault(require("./get-http")); const get_ip_1 = __importDefault(require("./get-ip")); const get_request_id_1 = __importDefault(require("./get-request-id")); const get_user_1 = __importDefault(require("./get-user")); const remove_empty_keys_1 = __importDefault(require("./remove-empty-keys")); const LEVELS = ['trace', 'debug', 'info', 'warn', 'error', 'fatal', 'silent']; class Logger { constructor() { this.config = {}; this.masks = []; this.setLevel = this.setLevel.bind(this); this.resetConfig = this.resetConfig.bind(this); this.setService = this.setService.bind(this); this.setRequestId = this.setRequestId.bind(this); this.setNetwork = this.setNetwork.bind(this); this.setHttp = this.setHttp.bind(this); this.setUser = this.setUser.bind(this); this.setDevice = this.setDevice.bind(this); this.setCorrelationId = this.setCorrelationId.bind(this); this.setEvent = this.setEvent.bind(this); this.getConfig = this.getConfig.bind(this); this.trace = this.trace.bind(this); this.debug = this.debug.bind(this); this.info = this.info.bind(this); this.warn = this.warn.bind(this); this.fatal = this.fatal.bind(this); this.error = this.error.bind(this); this.setCustomArgs = this.setCustomArgs.bind(this); this.pino = (0, pino_1.default)({ base: null, timestamp: false, messageKey: 'message', formatters: { level: (label) => ({ level: label.toUpperCase() }), }, }); this.setLevel(process.env.LOGGER_LEVEL || LEVELS[0]); } setLevel(level) { if (level && LEVELS.includes(level)) { this.pino.level = level; } } resetConfig() { this.config = {}; } setService(service) { this.config.service = service; } setRequestId(request_id) { this.config.request_id = request_id; } setNetwork(ip) { const network = { client: { ip, }, }; this.config.network = network; } setHttp({ useragent, method, url, host, }) { const http = { useragent, method, url, }; this.config.http = http; this.config.host = host; } setUser({ id, email, groups, scope, type, companyId, status, }) { const usr = { id, email, groups, scope, type, companyId, status, }; this.config.usr = usr; } setDevice({ id, installation, jti, }) { const device = { id, installation, jti, }; this.config.device = device; } setCorrelationId(correlation_id) { this.config.correlation_id = correlation_id; } setMask(masks) { this.masks = masks; } setEvent(service, event) { this.resetConfig(); this.setService(service); if (!event) { return; } const ip = (0, get_ip_1.default)(event); const http = (0, get_http_1.default)(event); const user = (0, get_user_1.default)(event); const device = (0, get_device_1.default)(event); const request_id = (0, get_request_id_1.default)(event); const correlation_id = (0, get_correlation_id_1.default)(event); if (ip) { this.setNetwork(ip); } if (http) { this.setHttp(http); } if (user) { this.setUser(user); } if (device) { this.setDevice(device); } if (request_id) { this.setRequestId(request_id); } if (correlation_id) { this.setCorrelationId(correlation_id); } this.config = (0, remove_empty_keys_1.default)(this.config); } maskLog(log) { if (this.masks.length === 0) { return log; } const result = this.masks.reduce((prev, curr) => { var _a; const value = object_path_immutable_1.default.get(prev, curr.path); if (!value) { return prev; } try { const defaultMask = () => '*'; const updateValue = (_a = curr.maskFunction) !== null && _a !== void 0 ? _a : defaultMask; return object_path_immutable_1.default .update(prev, curr.path, updateValue); } catch (_) { return prev; } }, log); return result; } getConfig() { return { created_at: new Date().toISOString(), type: 'log', ...this.config, }; } trace(log) { const config = this.getConfig(); this.pino .child(config) .trace(this.maskLog(log)); } debug(log) { const config = this.getConfig(); this.pino .child(config) .debug(this.maskLog(log)); } info(log) { const config = this.getConfig(); this.pino .child(config) .info(this.maskLog(log)); } warn(log) { const config = this.getConfig(); this.pino .child(config) .warn(this.maskLog(log)); } error(log) { const config = this.getConfig(); this.pino .child(config) .error(this.maskLog(log)); } fatal(log) { const config = this.getConfig(); this.pino .child(config) .fatal(this.maskLog(log)); } setCustomArgs(root_args, custom_args) { const root = (0, remove_empty_keys_1.default)(root_args); const custom = (0, remove_empty_keys_1.default)(custom_args); if (typeof root === 'object') { this.config = Object.assign(this.config, root); } if (typeof custom === 'object' && !!Object.keys(custom).length) { this.config.custom_args = custom; } } } exports.default = Logger;