UNPKG

kuzzle-logger

Version:

Logger package for Kuzzle backend, JS SDK, and any related module (like gateways).

136 lines 4.7 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.KuzzleLogger = void 0; const pino_1 = require("pino"); const Presets_1 = require("./Presets"); /** * The Logger class provides logging functionality for Kuzzle. */ class KuzzleLogger { _pino; get level() { return this._pino.level; } set level(level) { this._pino.level = level; } get pino() { return this._pino; } set pino(pinoInstance) { this._pino = pinoInstance; } getMergingObject = () => ({}); constructor(config) { const { transport, getMergingObject, skipPinoInstance, level, ...globalSettings } = config; if (getMergingObject) { this.getMergingObject = getMergingObject; } const transportConfig = Presets_1.Presets.expandPresets(transport ?? { preset: 'stdout' }, globalSettings); if (!skipPinoInstance) { this._pino = (0, pino_1.pino)({ level: level ?? 'info' }, pino_1.pino.transport(transportConfig)); } } trace(objOrMsg, ...args) { if (typeof objOrMsg === 'object') { const additionalData = { ...objOrMsg, ...(this.isErrorLike(objOrMsg) ? {} : this.getMergingObject()), }; const message = args.shift(); this._pino.trace(additionalData, message, ...args); return; } this._pino.trace(this.getMergingObject(), objOrMsg, args); } debug(objOrMsg, ...args) { if (typeof objOrMsg === 'object') { const additionalData = { ...objOrMsg, ...(this.isErrorLike(objOrMsg) ? {} : this.getMergingObject()), }; const message = args.shift(); this._pino.debug(additionalData, message, ...args); return; } this._pino.debug(this.getMergingObject(), objOrMsg, ...args); } info(objOrMsg, ...args) { if (typeof objOrMsg === 'object') { const additionalData = { ...objOrMsg, ...(this.isErrorLike(objOrMsg) ? {} : this.getMergingObject()), }; const message = args.shift(); this._pino.info(additionalData, message, ...args); return; } this._pino.info(this.getMergingObject(), objOrMsg, ...args); } warn(objOrMsg, ...args) { if (typeof objOrMsg === 'object') { const additionalData = { ...objOrMsg, ...(this.isErrorLike(objOrMsg) ? {} : this.getMergingObject()), }; const message = args.shift(); this._pino.warn(additionalData, message, ...args); return; } this._pino.warn(this.getMergingObject(), objOrMsg, ...args); } error(objOrMsg, ...args) { if (typeof objOrMsg === 'object') { const additionalData = { ...objOrMsg, ...(this.isErrorLike(objOrMsg) ? {} : this.getMergingObject()), }; const message = args.shift(); this._pino.error(additionalData, message, ...args); return; } this._pino.error(this.getMergingObject(), objOrMsg, ...args); } fatal(objOrMsg, ...args) { if (typeof objOrMsg === 'object') { const additionalData = { ...objOrMsg, ...(this.isErrorLike(objOrMsg) ? {} : this.getMergingObject()), }; const message = args.shift(); this._pino.fatal(additionalData, message, ...args); return; } this._pino.fatal(this.getMergingObject(), objOrMsg, ...args); } async flush() { return new Promise((resolve, reject) => { this._pino.flush((err) => { if (err) { reject(err); } resolve(); }); }); } child(namespace) { const currentMergingObject = this.getMergingObject(); const newNamespace = currentMergingObject?.namespace ? `${currentMergingObject.namespace}:${namespace}` : namespace; const childLogger = new KuzzleLogger({ getMergingObject: () => ({ ...currentMergingObject, namespace: newNamespace, }), skipPinoInstance: true, }); childLogger.pino = this.pino.child({}); return childLogger; } isErrorLike(err) { return err && typeof err.message === 'string'; } } exports.KuzzleLogger = KuzzleLogger; //# sourceMappingURL=KuzzleLogger.js.map