有人可以举例说明如何配置sails.js来登录文件吗?
看起来应该很简单,但我无法在线查找示例。
我正在查看config / log.js或config / sockets.js文件中的更改。
答案 0 :(得分:32)
根据the source code, for v0.9.x ,您只需在filePath
中设置config/log.js
:
module.exports = {
log: {
level: 'info',
filePath: 'application.log'
}
};
答案 1 :(得分: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
};
答案 2 :(得分: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