diff --git a/server/utils/logger.js b/server/utils/logger.js index 6768b6821..46167cab4 100644 --- a/server/utils/logger.js +++ b/server/utils/logger.js @@ -24,17 +24,18 @@ const createLogger = () => { if (isLocal) { 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({ level: "silly", format: winston.format.combine( - winston.format.colorize(), // Colorize the output - winston.format.timestamp(), // Add timestamps + winston.format.colorize(), + winston.format.timestamp(), 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)}` : ""} ${ @@ -44,7 +45,8 @@ const createLogger = () => { ) }) ]; - const productionTransports = [ + + const getProductionTransports = () => [ new WinstonCloudWatch({ level: "error", logStreamName: "errors", @@ -65,9 +67,9 @@ const createLogger = () => { const winstonLogger = winston.createLogger({ format: winston.format.json(), transports: - process.env.NODE_ENV !== "production" - ? productionTransports - : [...developmentTransports, ...productionTransports] + process.env.NODE_ENV === "production" + ? getProductionTransports() + : [...getDevelopmentTransports(), ...getProductionTransports()] }); return (message, type, user, record, object) => { @@ -80,7 +82,8 @@ const createLogger = () => { }); }; } catch (e) { - return console.log; + console.error("Logger setup failed", e); + return () => {}; // Return a no-op function in case of failure } };