From 2d19625ab71c3f37acda33b73f7f91fe5cdc55e3 Mon Sep 17 00:00:00 2001 From: Sai Sankeerth Date: Mon, 10 Jun 2024 19:13:06 +0530 Subject: [PATCH] chore: refactor with different convention of log levels Signed-off-by: Sai Sankeerth --- benchmark/metaLogger.js | 8 +-- src/logger.js | 123 ++++++++++++++++++++++++---------------- 2 files changed, 78 insertions(+), 53 deletions(-) diff --git a/benchmark/metaLogger.js b/benchmark/metaLogger.js index 2af1f599c9..757e7039e0 100644 --- a/benchmark/metaLogger.js +++ b/benchmark/metaLogger.js @@ -5,25 +5,25 @@ const logger = require('../src/logger'); logger.setLogLevel(Number.POSITIVE_INFINITY); const debug = (...args) => { - logger.setLogLevel(logger.levelDebug); + logger.setLogLevel('debug'); logger.debug(...args); logger.setLogLevel(Number.POSITIVE_INFINITY); }; const info = (...args) => { - logger.setLogLevel(logger.levelInfo); + logger.setLogLevel('info'); logger.info(...args); logger.setLogLevel(Number.POSITIVE_INFINITY); }; const warn = (...args) => { - logger.setLogLevel(logger.levelWarn); + logger.setLogLevel('warn'); logger.warn(...args); logger.setLogLevel(Number.POSITIVE_INFINITY); }; const error = (...args) => { - logger.setLogLevel(logger.levelError); + logger.setLogLevel('error'); logger.error(...args); logger.setLogLevel(Number.POSITIVE_INFINITY); }; diff --git a/src/logger.js b/src/logger.js index 97c717d11d..640a5ae05b 100644 --- a/src/logger.js +++ b/src/logger.js @@ -1,14 +1,17 @@ /* istanbul ignore file */ -const { structuredLogger: logger } = require('@rudderstack/integrations-lib'); +const { structuredLogger: logger /* LOGLEVELS */ } = require('@rudderstack/integrations-lib'); + +const LOGLEVELS = { + debug: 0, // Most verbose logging level + info: 1, // Logs about state of the application + warn: 2, // Logs about warnings which dont immediately halt the application + error: 3, // Logs about errors which dont immediately halt the application +}; -const levelDebug = 0; // Most verbose logging level -const levelInfo = 1; // Logs about state of the application -const levelWarn = 2; // Logs about warnings which dont immediately halt the application -const levelError = 3; // Logs about errors which dont immediately halt the application // any value greater than levelError will work as levelNone const loggerImpl = process.env.LOGGER_IMPL ?? 'winston'; -let logLevel = process.env.LOG_LEVEL ? parseInt(process.env.LOG_LEVEL, 10) : levelInfo; +let logLevel = process.env.LOG_LEVEL ?? 'error'; const setLogLevel = (level) => { const logger = getLogger(); @@ -16,78 +19,100 @@ const setLogLevel = (level) => { logger?.setLogLevel(`${loglevel}`); }; +const getLogMetadata = (metadata) => { + let reqMeta = metadata; + if (Array.isArray(metadata)) { + [reqMeta] = metadata; + } + return { + ...(reqMeta?.destinationId && { destinationId: reqMeta.destinationId }), + ...(reqMeta?.sourceId && { sourceId: reqMeta.sourceId }), + ...(reqMeta?.workspaceId && { workspaceId: reqMeta.workspaceId }), + ...(reqMeta?.destType && { destType: reqMeta.destType }), + ...(reqMeta?.module && { module: reqMeta.module }), + ...(reqMeta?.implementation && { implementation: reqMeta.implementation }), + ...(reqMeta?.feature && { feature: reqMeta.feature }), + }; +}; + +const log = (logMethod, args) => { + const [message, logInfo, ...otherArgs] = args; + if (logInfo) { + const { metadata, ...otherLogInfoArgs } = logInfo; + if (Array.isArray(metadata)) { + metadata.forEach((m) => { + logMethod( + message, + { + ...getLogMetadata(m), + ...otherLogInfoArgs, + }, + ...otherArgs, + ); + }); + return; + } + logMethod( + message, + { + ...getLogMetadata(metadata), + ...otherLogInfoArgs, + }, + ...otherArgs, + ); + return; + } + logMethod(message); +}; + const getLogger = () => { return loggerImpl === 'winston' ? logger : console; }; const debug = (...args) => { const logger = getLogger(); - if (levelDebug >= logLevel) { - logger.debug(...args); + if (logLevel >= LOGLEVELS.debug) { + log(logger.debug, args); } }; const info = (...args) => { const logger = getLogger(); - if (levelInfo >= logLevel) { - logger.info(...args); + if (logLevel >= LOGLEVELS.info) { + log(logger.info, args); } }; const warn = (...args) => { const logger = getLogger(); - if (levelWarn >= logLevel) { - logger.warn(...args); + if (logLevel >= LOGLEVELS.warn) { + log(logger.warn, args); } }; const error = (...args) => { const logger = getLogger(); - if (levelError >= logLevel) { - logger.error(...args); - } -}; - -const getLogMetadata = (metadata) => { - let reqMeta = metadata; - if (Array.isArray(metadata)) { - [reqMeta] = metadata; + if (logLevel >= LOGLEVELS.error) { + log(logger.error, args); } - return { - ...(reqMeta?.destinationId && { destinationId: reqMeta.destinationId }), - ...(reqMeta?.sourceId && { sourceId: reqMeta.sourceId }), - ...(reqMeta?.workspaceId && { workspaceId: reqMeta.workspaceId }), - ...(reqMeta?.destType && { destType: reqMeta.destType }), - ...(reqMeta?.module && { module: reqMeta.module }), - ...(reqMeta?.implementation && { implementation: reqMeta.implementation }), - ...(reqMeta?.feature && { feature: reqMeta.feature }), - }; }; const requestLog = (identifierMsg, { metadata, requestDetails: { url, body, method } }) => { const logger = getLogger(); - if (logLevel === levelWarn) { - logger.warn(identifierMsg, { - ...getLogMetadata(metadata), - url, - body, - method, - }); + if (logLevel === LOGLEVELS.warn) { + const reqLogArgs = [identifierMsg, { metadata, url, body, method }]; + log(logger.warn, reqLogArgs); } }; const responseLog = ( identifierMsg, - { metadata, responseDetails: { response: responseBody, status, headers: responseHeaders } }, + { metadata, responseDetails: { response: body, status, headers } }, ) => { const logger = getLogger(); - if (logLevel === levelWarn) { - logger.warn(identifierMsg, { - ...getLogMetadata(metadata), - ...(responseBody ? { responseBody } : {}), - ...(responseHeaders ? { responseHeaders } : {}), - status, - }); + if (logLevel === LOGLEVELS.warn) { + const resLogArgs = [identifierMsg, { metadata, body, status, headers }]; + log(logger.warn, resLogArgs); } }; @@ -97,10 +122,10 @@ module.exports = { warn, error, setLogLevel, - levelDebug, - levelInfo, - levelWarn, - levelError, + // levelDebug, + // levelInfo, + // levelWarn, + // levelError, responseLog, getLogMetadata, requestLog,