Node.js Logging

前端 未结 9 1841
一个人的身影
一个人的身影 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
    }));
    

提交回复
热议问题