UNPKG

@minimaltech/node-infra

Version:

Minimal Technology NodeJS Infrastructure - Loopback 4 Framework

93 lines 3.52 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; var _a, _b; Object.defineProperty(exports, "__esModule", { value: true }); exports.ApplicationLogger = exports.Logger = void 0; const isEmpty_1 = __importDefault(require("lodash/isEmpty")); const default_logger_1 = require("./default-logger"); const utilities_1 = require("../../utilities"); const extraLogEnvs = (_b = ((_a = process.env.APP_ENV_EXTRA_LOG_ENVS) !== null && _a !== void 0 ? _a : '').split(',').map(el => el.trim())) !== null && _b !== void 0 ? _b : []; const LOG_ENVIRONMENTS = new Set([ 'local', 'development', 'alpha', 'beta', 'staging', ...extraLogEnvs, ]); class Logger { constructor(opts) { this.environment = process.env.NODE_ENV; this.scopes = []; this.customLogger = opts === null || opts === void 0 ? void 0 : opts.customLogger; } // --------------------------------------------------------------------- getLogger() { var _a; return (_a = this.customLogger) !== null && _a !== void 0 ? _a : default_logger_1.applicationLogger; } // --------------------------------------------------------------------- _enhanceMessage(parts, message) { const enhanced = parts === null || parts === void 0 ? void 0 : parts.reduce((prevState = '', current) => { if ((0, isEmpty_1.default)(prevState)) { return current; } return prevState.concat(`-${current}`); }, ''); return `[${enhanced}]${message}`; } // --------------------------------------------------------------------- withScope(scope) { if (this.scopes.length < 2) { this.scopes.push(scope); return this; } while (this.scopes.length > 2) { this.scopes.pop(); } this.scopes[1] = scope; return this; } // --------------------------------------------------------------------- log(level, message, ...args) { const logger = this.getLogger(); if (!logger) { throw (0, utilities_1.getError)({ message: `[doLog] Level: ${level} | Invalid logger instance!` }); } const enhanced = this._enhanceMessage(this.scopes, message); logger.log(level, enhanced, ...args); } // --------------------------------------------------------------------- debug(message, ...args) { if (this.environment && !LOG_ENVIRONMENTS.has(this.environment)) { return; } if (!process.env.DEBUG) { return; } this.log('debug', message, ...args); } // --------------------------------------------------------------------- info(message, ...args) { this.log('info', message, ...args); } // --------------------------------------------------------------------- warn(message, ...args) { this.log('warn', message, ...args); } // --------------------------------------------------------------------- error(message, ...args) { this.log('error', message, ...args); } // --------------------------------------------------------------------- emerg(message, ...args) { this.log('emerg', message, ...args); } } exports.Logger = Logger; class ApplicationLogger extends Logger { } exports.ApplicationLogger = ApplicationLogger; //# sourceMappingURL=application-logger.js.map