feature/IO-2998-enhanced-api-logging - Finish
Signed-off-by: Dave Richer <dave@imexsystems.ca>
This commit is contained in:
@@ -24,17 +24,18 @@ const createLogger = () => {
|
|||||||
|
|
||||||
if (isLocal) {
|
if (isLocal) {
|
||||||
winstonCloudwatchTransportDefaults.awsOptions.endpoint = `http://${process.env.LOCALSTACK_HOSTNAME}:4566`;
|
winstonCloudwatchTransportDefaults.awsOptions.endpoint = `http://${process.env.LOCALSTACK_HOSTNAME}:4566`;
|
||||||
console.log(`Winston Transports set to LocalStack end point: ${winstonCloudwatchTransportDefaults.endpoint}`);
|
console.log(
|
||||||
|
`Winston Transports set to LocalStack end point: ${winstonCloudwatchTransportDefaults.awsOptions.endpoint}`
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const developmentTransports = [
|
const getDevelopmentTransports = () => [
|
||||||
new winston.transports.Console({
|
new winston.transports.Console({
|
||||||
level: "silly",
|
level: "silly",
|
||||||
format: winston.format.combine(
|
format: winston.format.combine(
|
||||||
winston.format.colorize(), // Colorize the output
|
winston.format.colorize(),
|
||||||
winston.format.timestamp(), // Add timestamps
|
winston.format.timestamp(),
|
||||||
winston.format.printf(({ level, message, timestamp, user, record, object }) => {
|
winston.format.printf(({ level, message, timestamp, user, record, object }) => {
|
||||||
// Format the log message for pretty printing
|
|
||||||
return `${timestamp} [${level}]: ${message} ${
|
return `${timestamp} [${level}]: ${message} ${
|
||||||
user ? `| user: ${JSON.stringify(user)}` : ""
|
user ? `| user: ${JSON.stringify(user)}` : ""
|
||||||
} ${record ? `| record: ${JSON.stringify(record)}` : ""} ${
|
} ${record ? `| record: ${JSON.stringify(record)}` : ""} ${
|
||||||
@@ -44,7 +45,8 @@ const createLogger = () => {
|
|||||||
)
|
)
|
||||||
})
|
})
|
||||||
];
|
];
|
||||||
const productionTransports = [
|
|
||||||
|
const getProductionTransports = () => [
|
||||||
new WinstonCloudWatch({
|
new WinstonCloudWatch({
|
||||||
level: "error",
|
level: "error",
|
||||||
logStreamName: "errors",
|
logStreamName: "errors",
|
||||||
@@ -65,9 +67,9 @@ const createLogger = () => {
|
|||||||
const winstonLogger = winston.createLogger({
|
const winstonLogger = winston.createLogger({
|
||||||
format: winston.format.json(),
|
format: winston.format.json(),
|
||||||
transports:
|
transports:
|
||||||
process.env.NODE_ENV !== "production"
|
process.env.NODE_ENV === "production"
|
||||||
? productionTransports
|
? getProductionTransports()
|
||||||
: [...developmentTransports, ...productionTransports]
|
: [...getDevelopmentTransports(), ...getProductionTransports()]
|
||||||
});
|
});
|
||||||
|
|
||||||
return (message, type, user, record, object) => {
|
return (message, type, user, record, object) => {
|
||||||
@@ -80,7 +82,8 @@ const createLogger = () => {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
return console.log;
|
console.error("Logger setup failed", e);
|
||||||
|
return () => {}; // Return a no-op function in case of failure
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user