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: volumes:
- /var/run/docker.sock:/var/run/docker.sock - /var/run/docker.sock:/var/run/docker.sock
environment: environment:
- SERVICES=ses,secretsmanager - SERVICES=ses,secretsmanager,cloudwatch,logs
- DEBUG=0 - DEBUG=0
- AWS_ACCESS_KEY_ID=test - AWS_ACCESS_KEY_ID=test
- AWS_SECRET_ACCESS_KEY=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-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 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 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: The Main IMEX API
node-app: node-app:

View File

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