feature/IO-2998-enhanced-api-logging - Finish

Signed-off-by: Dave Richer <dave@imexsystems.ca>
This commit is contained in:
Dave Richer
2024-10-24 14:21:35 -07:00
parent ed16156957
commit cd2a7cad7f
2 changed files with 79 additions and 64 deletions

View File

@@ -74,7 +74,7 @@ services:
volumes:
- /var/run/docker.sock:/var/run/docker.sock
environment:
- SERVICES=ses,secretsmanager
- SERVICES=ses,secretsmanager,cloudwatch,logs
- DEBUG=0
- AWS_ACCESS_KEY_ID=test
- AWS_SECRET_ACCESS_KEY=test
@@ -115,6 +115,7 @@ services:
aws --endpoint-url=http://localstack:4566 ses verify-domain-identity --domain imex.online --region ca-central-1
aws --endpoint-url=http://localstack:4566 ses verify-email-identity --email-address noreply@imex.online --region ca-central-1
aws --endpoint-url=http://localstack:4566 secretsmanager create-secret --name CHATTER_PRIVATE_KEY --secret-string file:///tmp/certs/id_rsa
aws --endpoint-url=http://localstack:4566 logs create-log-group --log-group-name development --region ca-central-1
"
# Node App: The Main IMEX API
node-app:

View File

@@ -1,73 +1,87 @@
const InstanceManager = require("../utils/instanceMgr").default;
const winston = require("winston");
const WinstonCloudWatch = require("winston-cloudwatch");
const { isString, isEmpty } = require("lodash");
const region = InstanceManager({
imex: "ca-central-1",
rome: "us-east-2"
});
const createLogger = () => {
try {
const region = InstanceManager({
imex: "ca-central-1",
rome: "us-east-2"
});
const logGroupName = process.env.CLOUDWATCH_LOG_GROUP;
const isLocal = isString(process.env?.LOCALSTACK_HOSTNAME) && !isEmpty(process.env?.LOCALSTACK_HOSTNAME);
const winstonLogger = winston.createLogger({
//level: "debug",
format: winston.format.json(),
transports: [
...(process.env.NODE_ENV !== "production"
? [
new winston.transports.Console({
level: "silly",
format: winston.format.combine(
winston.format.colorize(), // Colorize the output
winston.format.timestamp(), // Add timestamps
winston.format.printf(({ level, message, timestamp, user, record, object }) => {
// Format the log message for pretty printing
return `${timestamp} [${level}]: ${message} ${
user ? `| user: ${JSON.stringify(user)}` : ""
} ${record ? `| record: ${JSON.stringify(record)}` : ""} ${
object ? `| object: ${JSON.stringify(object, null, 2)}` : ""
}`;
})
)
const logGroupName = isLocal ? "development" : process.env.CLOUDWATCH_LOG_GROUP;
const winstonCloudwatchTransportDefaults = {
logGroupName: logGroupName,
awsOptions: {
region
},
jsonMessage: true
};
if (isLocal) {
winstonCloudwatchTransportDefaults.awsOptions.endpoint = `http://${process.env.LOCALSTACK_HOSTNAME}:4566`;
console.log(`Winston Transports set to LocalStack end point: ${winstonCloudwatchTransportDefaults.endpoint}`);
}
const developmentTransports = [
new winston.transports.Console({
level: "silly",
format: winston.format.combine(
winston.format.colorize(), // Colorize the output
winston.format.timestamp(), // Add timestamps
winston.format.printf(({ level, message, timestamp, user, record, object }) => {
// Format the log message for pretty printing
return `${timestamp} [${level}]: ${message} ${
user ? `| user: ${JSON.stringify(user)}` : ""
} ${record ? `| record: ${JSON.stringify(record)}` : ""} ${
object ? `| object: ${JSON.stringify(object, null, 2)}` : ""
}`;
})
]
: [
new WinstonCloudWatch({
level: "error",
logGroupName: logGroupName,
logStreamName: "errors",
awsRegion: region,
jsonMessage: true
}),
new WinstonCloudWatch({
level: "warn",
logGroupName: logGroupName,
logStreamName: "warn",
awsRegion: region,
jsonMessage: true
}),
new WinstonCloudWatch({
level: "debug",
logGroupName: logGroupName,
logStreamName: "debug",
awsRegion: region,
jsonMessage: true
})
])
]
});
)
})
];
const productionTransports = [
new WinstonCloudWatch({
level: "error",
logStreamName: "errors",
...winstonCloudwatchTransportDefaults
}),
new WinstonCloudWatch({
level: "warn",
logStreamName: "warn",
...winstonCloudwatchTransportDefaults
}),
new WinstonCloudWatch({
level: "debug",
logStreamName: "debug",
...winstonCloudwatchTransportDefaults
})
];
function log(message, type, user, record, object) {
// winstonLogger.debug(message, user, record, object);
const winstonLogger = winston.createLogger({
format: winston.format.json(),
transports:
process.env.NODE_ENV !== "production"
? productionTransports
: [...developmentTransports, ...productionTransports]
});
winstonLogger.log({
level: type.toLowerCase(),
message: message,
user: user,
record: record,
meta: object
});
return (message, type, user, record, object) => {
winstonLogger.log({
level: type.toLowerCase(),
message: message,
user: user,
record: record,
meta: object
});
};
} catch (e) {
return console.log;
}
};
}
module.exports = { log };
module.exports = { log: createLogger() };