Node.js Logging

前端 未结 9 1839
一个人的身影
一个人的身影 2021-01-29 17:28

Is there any library which will help me to handle logging in my Node.Js application? All I want to do is, I want to write all logs into a File and also I need an options like ro

9条回答
  •  孤独总比滥情好
    2021-01-29 18:07

    Log4js is one of the most popular logging library for nodejs application.

    It supports many cool features:

    1. Coloured console logging
    2. Replacement of node's console.log functions (optional)
    3. File appender, with log rolling based on file size
    4. SMTP, GELF, hook.io, Loggly appender
    5. Multiprocess appender (useful when you've got worker processes)
    6. A logger for connect/express servers
    7. Configurable log message layout/patterns
    8. Different log levels for different log categories (make some parts of your app log as DEBUG, others only ERRORS, etc.)

    Example:

    1. Installation: npm install log4js

    2. Configuration (./config/log4js.json):

      {"appenders": [
          {
              "type": "console",
              "layout": {
                  "type": "pattern",
                  "pattern": "%m"
              },
              "category": "app"
          },{
              "category": "test-file-appender",
              "type": "file",
              "filename": "log_file.log",
              "maxLogSize": 10240,
              "backups": 3,
              "layout": {
                  "type": "pattern",
                  "pattern": "%d{dd/MM hh:mm} %-5p %m"
              }
          }
      ],
      "replaceConsole": true }
      
    3. Usage:

      var log4js = require( "log4js" );
      log4js.configure( "./config/log4js.json" );
      var logger = log4js.getLogger( "test-file-appender" );
      // log4js.getLogger("app") will return logger that prints log to the console
      logger.debug("Hello log4js");// store log in file
      

提交回复
热议问题