This is a logger for a Node.js express application that uses the winston library for logging. It includes the following features:
- Support for different log levels: trace, debug, info, warn, error, fatal, and emergency
- Support for different formats: JSON, XML, or CSV
- Support for remote logging using the
winston-loggly
andwinston-papertrail
transports - Support for rotating log files using the
winston-daily-rotate-file
transport - Support for correlation ID
- Support for structured logging
- Support for different environments, in production mode it will only log error level messages
- Support for logging to a database like MongoDB, MySQL, or PostgreSQL
- Support for logging to a cloud service like AWS CloudWatch, Google Cloud Logging, or Azure Log Analytics
To use this logger in your express application, you need to install the following dependencies:
- winston
- winston-loggly
- winston-papertrail
- winston-daily-rotate-file
- shortid
You also need to import your database connection and config file, which are required for the MongoDB and remote logging transports.
The logger uses a custom format that includes a timestamp, log level, and message. It also includes a label and colorize for console transport. The log files rotate daily and keep for 14 days, and also archive as a zip file. The remote logging options are only enabled in production mode.
By default, the log level is set to 'debug' in development mode and 'error' in production mode. The logging to a database can be enabled by config.logToDB
.
You can use the log()
function to log messages, and provide log level, message, and context as arguments. The context object can include any additional information you want to include in the log message, such as the request ID
, user ID
, or other metadata.
You can also use the predefined functions info
, debug
, warn
, and error
to log messages at specific log levels.
The code generates a unique correlation ID for each log message, this can be overridden by passing correlationId
in the context object.
You can configure the logger by editing the config file and db connection file.
Please make sure to replace the placeholder fields such as
config.loggly.token
, config.loggly.subdomain
, config.papertrail.host
and config.papertrail.port
with the actual values for your application.