-
Notifications
You must be signed in to change notification settings - Fork 4
#8 Winston Logger ECS Deployment Fix #9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
LOG_DIR=logs | ||
|
||
# leave unset (defaults to false in production) | ||
ENABLE_FILE_LOGGING=true |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider setting ENABLE_FILE_LOGGING
to false
by default in the example environment file to prevent accidental file logging in production environments.
DEFAULT_NODE_ENV: 'development', | ||
DEFAULT_LOG_LEVEL: 'info', | ||
DEFAULT_LOG_DIR: 'logs', | ||
DEFAULT_ENABLE_FILE_LOGGING: false, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider making DEFAULT_ENABLE_FILE_LOGGING
configurable through environment variables or configuration files to allow flexibility in different deployment environments.
} | ||
|
||
private createWinstonLogger(): Logger { | ||
const baseTransports: any[] = [ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Avoid using any[]
for baseTransports
. Consider specifying a more precise type for the array elements, such as TransportStream[]
, to improve type safety and maintainability.
level: process.env.LOG_LEVEL || CONFIG.APP.DEFAULT_LOG_LEVEL, | ||
format: format.combine( | ||
format.colorize(), | ||
format.printf(({ timestamp, level, message, context, ...meta }) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The format.printf
function is used to format log messages. Ensure that all possible types of message
and context
are handled to avoid unexpected runtime errors. Consider adding validation or default handling for unexpected types.
]; | ||
|
||
// Add file transports conditionally based on environment | ||
const shouldEnableFileLogging = this.shouldEnableFileLogging(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The method shouldEnableFileLogging
is called to determine if file logging should be enabled. Ensure this method is implemented correctly and returns a boolean value. Consider adding error handling or logging if the method fails or returns an unexpected value.
try { | ||
baseTransports.push( | ||
new transports.File({ | ||
filename: `${process.env.LOG_DIR || CONFIG.APP.DEFAULT_LOG_DIR}/error.log`, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The file path for the log files is constructed using environment variables or default configuration. Ensure that process.env.LOG_DIR
and CONFIG.APP.DEFAULT_LOG_DIR
are correctly set and accessible. Consider validating these paths to prevent potential runtime errors.
}), | ||
); | ||
} catch (error) { | ||
// If file transport creation fails (e.g., read-only filesystem), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The error handling for file transport creation logs a warning to the console. Consider using the existing logger to log this warning instead of console.warn
to maintain consistency in logging practices.
*/ | ||
private shouldEnableFileLogging(): boolean { | ||
const nodeEnv = process.env.NODE_ENV; | ||
const enableFileLogging = process.env.ENABLE_FILE_LOGGING; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider adding validation for the ENABLE_FILE_LOGGING
environment variable to ensure it only accepts 'true' or 'false' values. This can prevent unexpected behavior if the variable is set to an invalid value.
|
||
// In production, only enable file logging if explicitly requested | ||
if (nodeEnv === 'production') { | ||
return enableFileLogging === 'true'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The condition enableFileLogging === 'true'
is a string comparison. Ensure that ENABLE_FILE_LOGGING
is always set as a string in the environment variables to avoid potential issues.
logging: { | ||
level: process.env.LOG_LEVEL || 'info', | ||
directory: process.env.LOG_DIR || 'logs', | ||
enableFileLogging: process.env.ENABLE_FILE_LOGGING === 'true', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider using a more explicit check for the environment variable, such as process.env.ENABLE_FILE_LOGGING?.toLowerCase() === 'true'
, to ensure that the comparison is case-insensitive and handles undefined values gracefully.
Please set NODE_ENV=production or ENABLE_FILE_LOGGING=false to avoid the Winston logger writting logs to File system.