sails logging to file

前端 未结 3 796
失恋的感觉
失恋的感觉 2020-12-25 13:27

Can someone provide an example of how to configure sails.js to log to a file?

It seems like it should be straightforward, but I\'m having trouble finding examples on

相关标签:
3条回答
  • 2020-12-25 14:05

    For winston 3.x.x versions

    @djsadinoff's answer not works.

    Instead do:

     $ npm install winston
    

    Replace your config/log.js file with the following code in Sails.js

        var winston = require('winston');
        const logger = winston.createLogger({
          level: 'silly',
          format: winston.format.json(),
          transports: [
            //
            // - Write to all logs with level `info` and below to `combined.log`
            // - Write all logs error (and below) to `error.log`.
            //
            new winston.transports.File({ filename: 'error.log', level: 'error' }),
            new winston.transports.File({ filename: 'sails.log' })
          ]
        });
    
        //
        // If we're not in production then log to the `console` with the format:
        // `${info.level}: ${info.message} JSON.stringify({ ...rest }) `
        //
        if (process.env.NODE_ENV !== 'production') {
          logger.add(new winston.transports.Console({
            format: winston.format.simple()
          }));
        }
    
        module.exports.log = {
          /***************************************************************************
          *                                                                          *
          * Valid `level` configs: i.e. the minimum log level to capture with        *
          * sails.log.*()                                                            *
          *                                                                          *
          * The order of precedence for log levels from lowest to highest is:        *
          * silly, verbose, info, debug, warn, error                                 *
          *                                                                          *
          * You may also set the level to "silent" to suppress all logs.             *
          *                                                                          *
          ***************************************************************************/
    
          // Pass in our custom logger, and pass all log levels through.
          custom: logger,
          level: 'silly',
    
          // Disable captain's log so it doesn't prefix or stringify our meta data.
          inspect: false
        };
    

    Then do

    $ sails lift
    
    0 讨论(0)
  • 2020-12-25 14:19

    According to the source code, for v0.9.x, you just have to set the filePath in your config/log.js:

    module.exports = {
      log: {
        level: 'info',
        filePath: 'application.log'
      }
    };
    
    0 讨论(0)
  • 2020-12-25 14:29

    Logging to a file doesn't work out of the box. You need to invoke functionality in libraries two levels down. See the documentation for winston.

    first install winston like so:

    $ npm install winston
    

    Then adjust config/log.js to look as follows

    var winston = require('winston');
    
    /*see the documentation for Winston:  https://github.com/flatiron/winston */
    var logger = new(winston.Logger)({
      transports: [
        new (winston.transports.Console)({}),
        new (winston.transports.File)({
          filename: 'logfile.log',
          level: 'verbose',
          json: false,
          colorize: false
        })
      ]
    });
    
    module.exports.log = {
      /***************************************************************************
       *                                                                          *
       * Valid `level` configs: i.e. the minimum log level to capture with        *
       * sails.log.*()                                                            *
       *                                                                          *
       * The order of precedence for log levels from lowest to highest is:        *
       * silly, verbose, info, debug, warn, error                                 *
       *                                                                          *
       * You may also set the level to "silent" to suppress all logs.             *
       *                                                                          *
       ***************************************************************************/
    
      level: 'silly',
      colorize: false,
      custom: logger
    };
    
    0 讨论(0)
提交回复
热议问题