UNPKG

@foal/core

Version:

Full-featured Node.js framework, with no complexity

62 lines (61 loc) 2.01 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Logger = void 0; // std const node_async_hooks_1 = require("node:async_hooks"); // FoalTS const config_1 = require("../config"); const logger_utils_1 = require("./logger.utils"); class Logger { asyncLocalStorage = new node_async_hooks_1.AsyncLocalStorage(); transports = [ (level, log) => console.log(log), ]; addTransport(transport) { this.transports.push(transport); } initLogContext(callback) { this.asyncLocalStorage.run({}, callback); } addLogContext(context) { const store = this.asyncLocalStorage.getStore(); if (!store) { this.log('warn', 'Impossible to add log context information. The logger context has not been initialized.'); return; } Object.assign(store, context); } log(level, message, params = {}) { const format = config_1.Config.get('settings.logger.format', 'string', 'raw'); if (format === 'none') { return; } const configLogLevel = config_1.Config.get('settings.logger.logLevel', 'string', 'info'); if (!(0, logger_utils_1.shouldLog)(level, configLogLevel)) { return; } ; const now = new Date(); const contextParams = this.asyncLocalStorage.getStore(); const formattedMessage = (0, logger_utils_1.formatMessage)(level, message, { ...contextParams, ...params, }, format, now); for (const transport of this.transports) { transport(level, formattedMessage); } } debug(message, params = {}) { this.log('debug', message, params); } info(message, params = {}) { this.log('info', message, params); } warn(message, params = {}) { this.log('warn', message, params); } error(message, params = {}) { this.log('error', message, params); } } exports.Logger = Logger;