release/2024-11-01 - Fix some log things

Signed-off-by: Dave Richer <dave@imexsystems.ca>
This commit is contained in:
Dave Richer
2024-10-28 09:31:37 -07:00
parent e90cda07e4
commit 1f2786ddec
3 changed files with 49 additions and 36 deletions

View File

@@ -8,6 +8,7 @@ const InstanceManager = require("../utils/instanceMgr").default;
const winston = require("winston");
const WinstonCloudWatch = require("winston-cloudwatch");
const { isString, isEmpty } = require("lodash");
const { networkInterfaces } = require("node:os");
const createLogger = () => {
try {
@@ -42,6 +43,19 @@ const createLogger = () => {
})();
};
const getPrivateIP = () => {
const interfaces = networkInterfaces();
for (const name of Object.keys(interfaces)) {
for (const iface of interfaces[name]) {
// Find an IPv4 address that's not internal (like localhost)
if (iface.family === "IPv4" && !iface.internal) {
return iface.address;
}
}
}
return "127.0.0.1";
};
const createProductionTransport = (level, logStreamName, filters) => {
return new WinstonCloudWatch({
level,
@@ -51,17 +65,26 @@ const createLogger = () => {
});
};
const hostname = process.env?.HOSTNAME || getPrivateIP();
const getDevelopmentTransports = () => [
new winston.transports.Console({
level: "silly",
format: winston.format.combine(
winston.format.colorize(),
winston.format.timestamp(),
winston.format.printf(({ level, message, timestamp, user, record, object }) => {
return `${timestamp} [${level}]: ${message} ${
user ? `| user: ${JSON.stringify(user)}` : ""
} ${record ? `| record: ${JSON.stringify(record)}` : ""} ${
object ? `| object: ${JSON.stringify(object, null, 2)}` : ""
winston.format.printf(({ level, message, timestamp, user, record, meta }) => {
const hostnameColor = `\x1b[34m${hostname}\x1b[0m`; // Blue
const timestampColor = `\x1b[36m${timestamp}\x1b[0m`; // Cyan
const labelColor = "\x1b[33m"; // Yellow
const separatorColor = "\x1b[35m|\x1b[0m"; // Magenta for separators
return `${timestampColor} [${hostnameColor}] [${level}]: ${message} ${
user ? `${separatorColor} ${labelColor}user:\x1b[0m ${JSON.stringify(user)}` : ""
} ${record ? `${separatorColor} ${labelColor}record:\x1b[0m ${JSON.stringify(record)}` : ""}${
meta
? `\n${separatorColor} ${labelColor}meta:\x1b[0m ${JSON.stringify(meta, null, 2)} ${separatorColor}`
: ""
}`;
})
)
@@ -89,6 +112,7 @@ const createLogger = () => {
message,
user,
record,
hostname,
meta
});
};