GithubHelp home page GithubHelp logo

stevekinyori / nodejs-logger Goto Github PK

View Code? Open in Web Editor NEW
1.0 2.0 0.0 13 KB

Logger for Node.js Express Application

License: MIT License

TypeScript 100.00%
database express express-js expressjs logger node nodejs winston winston-logger winston-transport

nodejs-logger's Introduction

Logger for Node.js Express Application

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 and winston-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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.