Skip to content

Commit

Permalink
chore: add structured logger to transformer logger & use it for gaec …
Browse files Browse the repository at this point in the history
…response logging

Signed-off-by: Sai Sankeerth <[email protected]>
  • Loading branch information
Sai Sankeerth committed May 27, 2024
1 parent 5bbb38e commit 0b7bfec
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 25 deletions.
37 changes: 33 additions & 4 deletions src/logger.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,66 @@
/* istanbul ignore file */
const { structuredLogger: logger } = require('@rudderstack/integrations-lib');
const { getLoggableData } = require('./v0/util');

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;

const setLogLevel = (level) => {
const logger = getLogger();
logLevel = level || logLevel;
logger?.setLogLevel(`${loglevel}`);
};

const getLogger = () => {
return loggerImpl === 'winston' ? logger : console;
};

const debug = (...args) => {
const logger = getLogger();
if (levelDebug >= logLevel) {
console.debug(...args);
logger.debug(...args);
}
};

const info = (...args) => {
const logger = getLogger();
if (levelInfo >= logLevel) {
console.info(...args);
logger.info(...args);
}
};

const warn = (...args) => {
const logger = getLogger();
if (levelWarn >= logLevel) {
console.warn(...args);
logger.warn(...args);
}
};

const error = (...args) => {
const logger = getLogger();
if (levelError >= logLevel) {
logger.error(...args);
}
};

const responseLog = (
identifierMsg,
{ metadata, responseDetails: { response: responseBody, status, headers: responseHeaders } },
) => {
const logger = getLogger();
if (levelError >= logLevel) {
console.error(...args);
logger.debug(identifierMsg, {
...getLoggableData(metadata),
...(responseBody ? { responseBody } : {}),
...(responseHeaders ? { responseHeaders } : {}),
status,
});
}
};

Expand All @@ -46,4 +74,5 @@ module.exports = {
levelInfo,
levelWarn,
levelError,
responseLog,
};
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
const { get, set } = require('lodash');
const sha256 = require('sha256');
const {
NetworkError,
NetworkInstrumentationError,
structuredLogger: logger,
} = require('@rudderstack/integrations-lib');
const { NetworkError, NetworkInstrumentationError } = require('@rudderstack/integrations-lib');
const SqlString = require('sqlstring');
const { prepareProxyRequest, handleHttpRequest } = require('../../../adapters/network');
const {
isHttpStatusSuccess,
getAuthErrCategoryFromStCode,
getLoggableData,
} = require('../../util/index');
const { isHttpStatusSuccess, getAuthErrCategoryFromStCode } = require('../../util/index');
const { CONVERSION_ACTION_ID_CACHE_TTL, destType } = require('./config');
const Cache = require('../../util/cache');
const logger = require('../../../logger');

const conversionActionIdCache = new Cache(CONVERSION_ACTION_ID_CACHE_TTL);

Expand Down Expand Up @@ -63,11 +56,13 @@ const getConversionActionId = async ({ method, headers, params, metadata }) => {
},
);
const { status, response, headers: responseHeaders } = gaecConversionActionIdResponse;
logger.debug(`[${destType.toUpperCase()}] get conversion action id response`, {
...getLoggableData(metadata),
...(responseHeaders ? { responseHeaders } : {}),
...(response ? { response } : {}),
status,
logger.responseLog(`[${destType.toUpperCase()}] get conversion action id response`, {
metadata,
responseDetails: {
response,
status,
headers: responseHeaders,
},
});
if (!isHttpStatusSuccess(status)) {
throw new NetworkError(
Expand Down Expand Up @@ -127,12 +122,14 @@ const ProxyRequest = async (request) => {
module: 'dataDelivery',
},
);
const { response: resp, status, headers: responseHeaders } = processedResponse;
logger.debug(`[${destType.toUpperCase()}] get conversion action id response`, {
...getLoggableData(metadata),
...(responseHeaders ? { responseHeaders } : {}),
...(resp ? { response: resp } : {}),
status,
const { response: processedResp, status, headers: responseHeaders } = processedResponse;
logger.responseLog(`[${destType.toUpperCase()}] get conversion action id response`, {
metadata,
responseDetails: {
response: processedResp,
status,
headers: responseHeaders,
},
});
return response;
};
Expand Down

0 comments on commit 0b7bfec

Please sign in to comment.