diff --git a/Server/utils/logger.js b/Server/utils/logger.js index 6c0bddcd8..60d509a6a 100644 --- a/Server/utils/logger.js +++ b/Server/utils/logger.js @@ -1,23 +1,91 @@ -import winston from "winston"; -/** - * @module - * @example - * logger.info("Registered a new user!") - * logger.warn("User not found!") - * logger.error("Cannot save") - * @example - * "Specify service and ID in the log if applicable." - * logger.error("Descriptive Message",{"service":"monitor","monitorId":"123456"}) - * logger.error("Incorrect Credentials",{"service":"Auth","userId":"654321"}) - * logger.error("User not found!",{"service":"Auth"}) - */ -const logger = winston.createLogger({ - level: "info", - format: winston.format.combine(winston.format.timestamp(), winston.format.json()), - transports: [ - new winston.transports.Console(), - new winston.transports.File({ filename: "app.log" }), - ], -}); +import { createLogger, format, transports } from "winston"; + +class Logger { + constructor() { + const consoleFormat = format.printf( + ({ level, message, service, method, details, timestamp }) => { + if (message instanceof Object) { + message = JSON.stringify(message, null, 2); + } + let msg = `${timestamp} ${level}:`; + service && (msg += ` [${service}]`); + method && (msg += `(${method})`); + message && (msg += ` ${message}`); + details && (msg += ` (details: ${details})`); + return msg; + } + ); + + this.logger = createLogger({ + level: "info", + format: format.combine(format.timestamp()), + transports: [ + new transports.Console({ + format: format.combine( + format.colorize(), + format.prettyPrint(), + format.json(), + consoleFormat + ), + }), + new transports.File({ + format: format.combine(format.json()), + filename: "app.log", + }), + ], + }); + } + /** + * Logs an informational message. + * @param {Object} config - The configuration object. + * @param {string} config.message - The message to log. + * @param {string} config.service - The service name. + * @param {string} config.method - The method name. + * @param {Object} config.details - Additional details. + */ + info(config) { + this.logger.info(config.message, { + service: config.service, + method: config.method, + details: config.details, + }); + } + + /** + * Logs a warning message. + * @param {Object} config - The configuration object. + * @param {string} config.message - The message to log. + * @param {string} config.service - The service name. + * @param {string} config.method - The method name. + * @param {Object} config.details - Additional details. + */ + warn(config) { + this.logger.warn(config.message, { + service: config.service, + method: config.method, + details: config.details, + }); + } + + /** + * Logs an error message. + * @param {Object} config - The configuration object. + * @param {string} config.message - The message to log. + * @param {string} config.service - The service name. + * @param {string} config.method - The method name. + * @param {Object} config.details - Additional details. + */ + error(config) { + this.logger.error(config.message, { + service: config.service, + method: config.method, + details: config.details, + stack: config.stack, + }); + } +} + +const logger = new Logger(); +export { Logger }; export default logger;