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,20 +1,33 @@
const InstanceManager = require("../utils/instanceMgr").default;
const winston = require("winston");
const WinstonCloudWatch = require("winston-cloudwatch");
const { isString, isEmpty } = require("lodash");
const region = InstanceManager({
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"
? [
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(
@@ -30,36 +43,34 @@ const winstonLogger = winston.createLogger({
})
)
})
]
: [
];
const productionTransports = [
new WinstonCloudWatch({
level: "error",
logGroupName: logGroupName,
logStreamName: "errors",
awsRegion: region,
jsonMessage: true
...winstonCloudwatchTransportDefaults
}),
new WinstonCloudWatch({
level: "warn",
logGroupName: logGroupName,
logStreamName: "warn",
awsRegion: region,
jsonMessage: true
...winstonCloudwatchTransportDefaults
}),
new WinstonCloudWatch({
level: "debug",
logGroupName: logGroupName,
logStreamName: "debug",
awsRegion: region,
jsonMessage: true
...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]
});
return (message, type, user, record, object) => {
winstonLogger.log({
level: type.toLowerCase(),
message: message,
@@ -67,7 +78,10 @@ function log(message, type, user, record, object) {
record: record,
meta: object
});
};
} catch (e) {
return console.log;
}
};
}
module.exports = { log };
module.exports = { log: createLogger() };