UNPKG

better-logging

Version:

better-logging is a drop in replacement for the default logging methods of node.js

48 lines (47 loc) 1.92 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.decorateObject = void 0; const path_1 = __importDefault(require("path")); const formatMessage_1 = require("./formatMessage"); const writeToFile_1 = require("./writeToFile"); const decorateObject = (target, implementation, fs, config) => { const targetObject = target; targetObject.logLevel = 3; if (config.saveToFile !== null) { const dirPath = path_1.default.parse(config.saveToFile).dir; if (dirPath !== '') { fs.mkdirSync(dirPath, { recursive: true }); } } const funcFactory = (type) => (msg, ...args) => { const [message, remainingArgs] = (0, formatMessage_1.formatMessage)(type, config, [ msg, ...args, ]); if (config.saveToFile !== null) { (0, writeToFile_1.writeLogToFile)(fs, config.saveToFile, message, remainingArgs); } if (targetObject.logLevel < config.logLevels[type]) { // Don't emit a log if the logLevel is lower than the configured logLevel for the current type return; } implementation[type](message, ...remainingArgs); }; targetObject.log = funcFactory('log'); targetObject.debug = funcFactory('debug'); targetObject.error = funcFactory('error'); targetObject.info = funcFactory('info'); targetObject.warn = funcFactory('warn'); targetObject.line = (msg, ...args) => { if (targetObject.logLevel < config.logLevels.line) { // Don't emit a log if the logLevel is lower than the configured logLevel for "line" return; } implementation.log(msg, ...args); }; return targetObject; }; exports.decorateObject = decorateObject;