UNPKG

better-logging

Version:

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

62 lines (61 loc) 3.59 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.expressMiddleware = void 0; const chalk_1 = __importDefault(require("chalk")); const typescript_helpers_1 = require("@olian/typescript-helpers"); const configCache_1 = require("../lib/configCache"); const expressMiddleware = (hostObjOrLogFunction, config = {}) => { const logFunction = typeof hostObjOrLogFunction === 'function' ? hostObjOrLogFunction : hostObjOrLogFunction === null || hostObjOrLogFunction === void 0 ? void 0 : hostObjOrLogFunction.info; const betterLoggingConfig = configCache_1.ConfigCache.getConfig(logFunction); if (logFunction === undefined || betterLoggingConfig === null) { throw new Error('BetterLogging.expressMiddleware requires its first argument to be either an object decorated by betterLogging, or a logging function that belongs to an object decorated by betterLogging.'); } const { color } = betterLoggingConfig; return (req, res, next) => { const method = { order: (0, typescript_helpers_1.useValueOrFallback)(config.method, 'order', 1), show: (0, typescript_helpers_1.useValueOrFallback)(config.method, 'show', true), color: (0, typescript_helpers_1.useValueOrFallback)(config.method, 'color', color.base), value: (0, typescript_helpers_1.useValueOrFallback)(req, 'method', ''), }; const ip = { order: (0, typescript_helpers_1.useValueOrFallback)(config.ip, 'order', 2), show: (0, typescript_helpers_1.useValueOrFallback)(config.ip, 'show', true), color: (0, typescript_helpers_1.useValueOrFallback)(config.ip, 'color', color.base), value: (0, typescript_helpers_1.useValueOrFallback)(req, 'ip', ''), }; const path = { order: (0, typescript_helpers_1.useValueOrFallback)(config.path, 'order', 3), show: (0, typescript_helpers_1.useValueOrFallback)(config.path, 'show', true), color: (0, typescript_helpers_1.useValueOrFallback)(config.path, 'color', chalk_1.default.reset), value: (0, typescript_helpers_1.useValueOrFallback)(req, 'path', ''), }; const body = { order: (0, typescript_helpers_1.useValueOrFallback)(config.body, 'order', 4), show: (0, typescript_helpers_1.useValueOrFallback)(config.body, 'show', false), color: (0, typescript_helpers_1.useValueOrFallback)(config.body, 'color', chalk_1.default.reset), value: (0, typescript_helpers_1.useValueOrFallback)(req, 'body', ''), }; const header = { order: (0, typescript_helpers_1.useValueOrFallback)(config.header, 'order', 5), show: (0, typescript_helpers_1.useValueOrFallback)(config.header, 'show', false), color: (0, typescript_helpers_1.useValueOrFallback)(config.header, 'color', chalk_1.default.reset), value: (0, typescript_helpers_1.useValueOrFallback)(req, 'headers', {}), }; logFunction([method, ip, path, body, header] .filter((a) => a.show) .sort((a, b) => a.order - b.order) .map((obj) => obj.color(`${typeof obj.value === 'object' ? JSON.stringify(obj.value) : obj.value}`)) .filter((v) => v.length > 0) .join(' ')); next(); }; }; exports.expressMiddleware = expressMiddleware;