Node.js Logging

前端 未结 9 1838
一个人的身影
一个人的身影 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:04

    Winston is strong choice for most of the developers. I have been using winston for long. Recently I used winston with with papertrail which takes the application logging to next level.

    Here is a nice screenshot from their site.

    How its useful

    • you can manage logs from different systems at one place. this can be very useful when you have two backend communicating and can see logs from both at on place.

    • Logs are live. you can see realtime logs of your production server.

    • Powerful search and filter

    • you can create alerts to send you email if it encounters specific text in log.

    and you can find more http://help.papertrailapp.com/kb/how-it-works/event-viewer/

    A simple configuration using winston,winston-express and winston-papertrail node modules.

    import winston from 'winston';
    import expressWinston from 'express-winston';
    //
    // Requiring `winston-papertrail` will expose
    // `winston.transports.Papertrail`
    //
    require('winston-papertrail').Papertrail;
    // create winston transport for Papertrail
    var winstonPapertrail = new winston.transports.Papertrail({
      host: 'logsX.papertrailapp.com',
      port: XXXXX
    });
    app.use(expressWinston.logger({
      transports: [winstonPapertrail],
      meta: true, // optional: control whether you want to log the meta data about the request (default to true)
      msg: "HTTP {{req.method}} {{req.url}}", // optional: customize the default logging message. E.g. "{{res.statusCode}} {{req.method}} {{res.responseTime}}ms {{req.url}}"
      expressFormat: true, // Use the default Express/morgan request formatting. Enabling this will override any msg if true. Will only output colors with colorize set to true
      colorize: true, // Color the text and status code, using the Express/morgan color palette (text: gray, status: default green, 3XX cyan, 4XX yellow, 5XX red).
      ignoreRoute: function (req, res) { return false; } // optional: allows to skip some log messages based on request and/or response
    }));
    
    0 讨论(0)
  • 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
      
    0 讨论(0)
  • 2021-01-29 18:10

    Scribe.JS Lightweight Logger

    I have looked through many loggers, and I wasn't able to find a lightweight solution - so I decided to make a simple solution that is posted on github.

    • Saves the file which are organized by user, date, and level
    • Gives you a pretty output (we all love that)
    • Easy-to-use HTML interface

    I hope this helps you out.

    Online Demo

    http://bluejamesbond.github.io/Scribe.js/

    Secure Web Access to Logs

    A

    Prints Pretty Text to Console Too!

    A

    Web Access

    A

    Github

    https://github.com/bluejamesbond/Scribe.js

    0 讨论(0)
提交回复
热议问题