feature/IO-2998-enhanced-api-logging - Finish
Signed-off-by: Dave Richer <dave@imexsystems.ca>
This commit is contained in:
@@ -174,7 +174,7 @@ const connectToRedisCluster = async () => {
|
|||||||
Math.min(CLUSTER_RETRY_BASE_DELAY + times * 50, CLUSTER_RETRY_MAX_DELAY) + Math.random() * CLUSTER_RETRY_JITTER;
|
Math.min(CLUSTER_RETRY_BASE_DELAY + times * 50, CLUSTER_RETRY_MAX_DELAY) + Math.random() * CLUSTER_RETRY_JITTER;
|
||||||
logger.log(
|
logger.log(
|
||||||
`[${process.env.NODE_ENV}] Redis cluster not yet ready. Retrying in ${delay.toFixed(2)}ms`,
|
`[${process.env.NODE_ENV}] Redis cluster not yet ready. Retrying in ${delay.toFixed(2)}ms`,
|
||||||
"ERROR",
|
"WARN",
|
||||||
"redis",
|
"redis",
|
||||||
"api"
|
"api"
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -5,19 +5,16 @@ const { isString, isEmpty } = require("lodash");
|
|||||||
|
|
||||||
const createLogger = () => {
|
const createLogger = () => {
|
||||||
try {
|
try {
|
||||||
const region = InstanceManager({
|
|
||||||
imex: "ca-central-1",
|
|
||||||
rome: "us-east-2"
|
|
||||||
});
|
|
||||||
|
|
||||||
const isLocal = isString(process.env?.LOCALSTACK_HOSTNAME) && !isEmpty(process.env?.LOCALSTACK_HOSTNAME);
|
const isLocal = isString(process.env?.LOCALSTACK_HOSTNAME) && !isEmpty(process.env?.LOCALSTACK_HOSTNAME);
|
||||||
|
|
||||||
const logGroupName = isLocal ? "development" : process.env.CLOUDWATCH_LOG_GROUP;
|
const logGroupName = isLocal ? "development" : process.env.CLOUDWATCH_LOG_GROUP;
|
||||||
|
|
||||||
const winstonCloudwatchTransportDefaults = {
|
const winstonCloudwatchTransportDefaults = {
|
||||||
logGroupName: logGroupName,
|
logGroupName: logGroupName,
|
||||||
awsOptions: {
|
awsOptions: {
|
||||||
region
|
region: InstanceManager({
|
||||||
|
imex: "ca-central-1",
|
||||||
|
rome: "us-east-2"
|
||||||
|
})
|
||||||
},
|
},
|
||||||
jsonMessage: true
|
jsonMessage: true
|
||||||
};
|
};
|
||||||
@@ -29,6 +26,25 @@ const createLogger = () => {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const levelFilter = (levels) => {
|
||||||
|
return winston.format((info) => {
|
||||||
|
if (Array.isArray(levels)) {
|
||||||
|
return levels.includes(info.level) ? info : false;
|
||||||
|
} else {
|
||||||
|
return info.level === levels ? info : false;
|
||||||
|
}
|
||||||
|
})();
|
||||||
|
};
|
||||||
|
|
||||||
|
const createProductionTransport = (level, logStreamName, filters) => {
|
||||||
|
return new WinstonCloudWatch({
|
||||||
|
level,
|
||||||
|
logStreamName: logStreamName || level,
|
||||||
|
format: levelFilter(filters || level),
|
||||||
|
...winstonCloudwatchTransportDefaults
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
const getDevelopmentTransports = () => [
|
const getDevelopmentTransports = () => [
|
||||||
new winston.transports.Console({
|
new winston.transports.Console({
|
||||||
level: "silly",
|
level: "silly",
|
||||||
@@ -47,21 +63,10 @@ const createLogger = () => {
|
|||||||
];
|
];
|
||||||
|
|
||||||
const getProductionTransports = () => [
|
const getProductionTransports = () => [
|
||||||
new WinstonCloudWatch({
|
createProductionTransport("error"),
|
||||||
level: "error",
|
createProductionTransport("warn"),
|
||||||
logStreamName: "errors",
|
createProductionTransport("info"),
|
||||||
...winstonCloudwatchTransportDefaults
|
createProductionTransport("silly", "debug", ["http", "verbose", "debug", "silly"])
|
||||||
}),
|
|
||||||
new WinstonCloudWatch({
|
|
||||||
level: "warn",
|
|
||||||
logStreamName: "warn",
|
|
||||||
...winstonCloudwatchTransportDefaults
|
|
||||||
}),
|
|
||||||
new WinstonCloudWatch({
|
|
||||||
level: "debug",
|
|
||||||
logStreamName: "debug",
|
|
||||||
...winstonCloudwatchTransportDefaults
|
|
||||||
})
|
|
||||||
];
|
];
|
||||||
|
|
||||||
const winstonLogger = winston.createLogger({
|
const winstonLogger = winston.createLogger({
|
||||||
@@ -72,19 +77,27 @@ const createLogger = () => {
|
|||||||
: [...getDevelopmentTransports(), ...getProductionTransports()]
|
: [...getDevelopmentTransports(), ...getProductionTransports()]
|
||||||
});
|
});
|
||||||
|
|
||||||
return (message, type, user, record, object) => {
|
const log = (message, type, user, record, meta) => {
|
||||||
winstonLogger.log({
|
winstonLogger.log({
|
||||||
level: type.toLowerCase(),
|
level: type.toLowerCase(),
|
||||||
message: message,
|
message,
|
||||||
user: user,
|
user,
|
||||||
record: record,
|
record,
|
||||||
meta: object
|
meta
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
return {
|
||||||
|
log,
|
||||||
|
logger: winstonLogger
|
||||||
|
};
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error("Logger setup failed", e);
|
console.error("Error setting up enhanced Logger, defaulting to console.: " + e?.message || "");
|
||||||
return () => {}; // Return a no-op function in case of failure
|
return {
|
||||||
|
log: console.log,
|
||||||
|
logger: console.log
|
||||||
|
};
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = { log: createLogger() };
|
module.exports = createLogger();
|
||||||
|
|||||||
Reference in New Issue
Block a user