@minimaltech/node-infra
Version:
Minimal Technology NodeJS Infrastructure - Loopback 4 Framework
116 lines • 5.35 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
var _a, _b, _c;
Object.defineProperty(exports, "__esModule", { value: true });
exports.applicationLogger = exports.defineCustomLogger = exports.applicationLogFormatter = exports.defineCustomLoggerFormatter = void 0;
const common_1 = require("../../common");
const node_path_1 = __importDefault(require("node:path"));
const winston_1 = __importDefault(require("winston"));
require("winston-daily-rotate-file");
const transports_1 = require("./transports");
const utilities_1 = require("../../utilities");
const LOGGER_FOLDER_PATH = (_a = process.env.APP_ENV_LOGGER_FOLDER_PATH) !== null && _a !== void 0 ? _a : './';
const LOGGER_PREFIX = common_1.App.APPLICATION_NAME;
// -------------------------------------------------------------------------------------------
const defineCustomLoggerFormatter = (opts) => {
return winston_1.default.format.combine(winston_1.default.format.label({ label: opts.label }), winston_1.default.format.splat(), winston_1.default.format.align(), winston_1.default.format.timestamp(), winston_1.default.format.simple(), winston_1.default.format.colorize(), winston_1.default.format.printf(({ level, message, label, timestamp }) => `${timestamp} [${label}] ${level}: ${message}`), winston_1.default.format.errors({ stack: true }));
};
exports.defineCustomLoggerFormatter = defineCustomLoggerFormatter;
// -------------------------------------------------------------------------------------------
exports.applicationLogFormatter = (0, exports.defineCustomLoggerFormatter)({ label: LOGGER_PREFIX });
// -------------------------------------------------------------------------------------------
const defineCustomLogger = (opts) => {
const { logLevels = {
error: 1,
alert: 1,
emerg: 1,
warn: 2,
info: 3,
http: 4,
verbose: 5,
debug: 6,
silly: 7,
}, logColors = {
error: 'red',
alert: 'red',
emerg: 'red',
warn: 'yellow',
info: 'green',
http: 'magenta',
verbose: 'gray',
debug: 'blue',
silly: 'gray',
}, loggerFormatter = exports.applicationLogFormatter, transports: { info: infoTransportOptions, error: errorTransportOptions }, } = opts;
const consoleLogTransport = new winston_1.default.transports.Console({ level: 'debug' });
const transports = {
general: [consoleLogTransport],
exception: [consoleLogTransport],
};
// File configure
if (infoTransportOptions.file) {
const transport = new winston_1.default.transports.DailyRotateFile({
frequency: '1h',
maxSize: '100m',
maxFiles: '5d',
datePattern: 'YYYYMMDD_HH',
filename: node_path_1.default.join(infoTransportOptions.file.folder, `/${infoTransportOptions.file.prefix}-info-%DATE%.log`),
level: 'info',
});
transports.general.push(transport);
}
if (errorTransportOptions.file) {
const transport = new winston_1.default.transports.DailyRotateFile({
frequency: '1h',
maxSize: '100m',
maxFiles: '5d',
datePattern: 'YYYYMMDD_HH',
filename: node_path_1.default.join(errorTransportOptions.file.folder, `/${errorTransportOptions.file.prefix}-error-%DATE%.log`),
level: 'error',
});
transports.general.push(transport);
transports.exception.push(transport);
}
// Stream configure
if (infoTransportOptions.dgram) {
const transport = transports_1.DgramTransport.fromPartial(infoTransportOptions.dgram);
if (transport) {
transports.general.push(transport);
}
}
if (errorTransportOptions.dgram) {
const transport = transports_1.DgramTransport.fromPartial(errorTransportOptions.dgram);
if (transport) {
transports.exception.push(transport);
}
}
// Color configure
winston_1.default.addColors(logColors);
// Logger
return winston_1.default.createLogger({
levels: logLevels,
format: loggerFormatter,
exitOnError: false,
transports: transports.general,
exceptionHandlers: transports.exception,
});
};
exports.defineCustomLogger = defineCustomLogger;
// -------------------------------------------------------------------------------------------
const fileOptions = { folder: LOGGER_FOLDER_PATH, prefix: LOGGER_PREFIX };
const dgramOptions = {
socketOptions: { type: 'udp4' },
host: process.env.APP_ENV_LOGGER_DGRAM_HOST,
port: (0, utilities_1.int)(process.env.APP_ENV_LOGGER_DGRAM_PORT),
label: process.env.APP_ENV_LOGGER_DGRAM_LABEL,
levels: (_c = (_b = process.env.APP_ENV_LOGGER_DGRAM_LEVELS) === null || _b === void 0 ? void 0 : _b.split(',').map(el => el.trim())) !== null && _c !== void 0 ? _c : [],
};
// -------------------------------------------------------------------------------------------
exports.applicationLogger = (0, exports.defineCustomLogger)({
transports: {
info: { file: fileOptions, dgram: dgramOptions },
error: { file: fileOptions, dgram: dgramOptions },
},
});
//# sourceMappingURL=default-logger.js.map