sails记录到文件

问题描述 投票:19回答:3

有人可以提供一个如何配置sails.js以记录到文件的示例吗?

它似乎应该是直截了当的,但我无法在线查找示例。

我正在查看config / log.js或config / sockets.js文件中的更改。

file logging configuration sails.js
3个回答
32
投票

根据the source code,对于v0.9.x,你只需要在你的filePath中设置config/log.js

module.exports = {
  log: {
    level: 'info',
    filePath: 'application.log'
  }
};

19
投票

记录到文件不是开箱即用的。您需要在库中调用两个级别的功能。请参阅winston的文档。

首先安装winston就像这样:

$ npm install winston

然后将config/log.js调整为如下所示

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
投票

适用于winston 3.x.x版本

@ djsadinoff的答案不起作用。

相反:

 $ npm install winston

使用Sails.js中的以下代码替换config / log.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
    };

然后做

$ sails lift
© www.soinside.com 2019 - 2024. All rights reserved.