【发布时间】:2013-12-27 03:18:30
【问题描述】:
有人可以提供一个如何配置sails.js 以记录到文件的示例吗?
看起来应该很简单,但我在网上找不到示例。
我正在查看 config/log.js 或 config/sockets.js 文件中的更改。
【问题讨论】:
标签: file logging configuration sails.js
有人可以提供一个如何配置sails.js 以记录到文件的示例吗?
看起来应该很简单,但我在网上找不到示例。
我正在查看 config/log.js 或 config/sockets.js 文件中的更改。
【问题讨论】:
标签: file logging configuration sails.js
根据the source code,对于v0.9.x,您只需在config/log.js 中设置filePath:
module.exports = {
log: {
level: 'info',
filePath: 'application.log'
}
};
【讨论】:
记录到文件不能开箱即用。您需要在两层以下调用库中的功能。请参阅 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
};
【讨论】:
config/local.js,设置日志级别?
对于winston 3.x.x 版本
@djsadinoff 的回答无效。
改为:
$ npm install winston
将您的 config/log.js 文件替换为 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
};
那就做吧
$ sails lift
【讨论】: