Skip to content

Commit

Permalink
chore: refactor with different convention of log levels
Browse files Browse the repository at this point in the history
Signed-off-by: Sai Sankeerth <[email protected]>
  • Loading branch information
Sai Sankeerth committed Jun 10, 2024
1 parent 8ad1a5f commit 2d19625
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 53 deletions.
8 changes: 4 additions & 4 deletions benchmark/metaLogger.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
Expand Down
123 changes: 74 additions & 49 deletions src/logger.js
Original file line number Diff line number Diff line change
@@ -1,93 +1,118 @@
/* 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();
logLevel = level || logLevel;
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);
}
};

Expand All @@ -97,10 +122,10 @@ module.exports = {
warn,
error,
setLogLevel,
levelDebug,
levelInfo,
levelWarn,
levelError,
// levelDebug,
// levelInfo,
// levelWarn,
// levelError,
responseLog,
getLogMetadata,
requestLog,
Expand Down

0 comments on commit 2d19625

Please sign in to comment.