Sail.js - 如何创建干净的日志文件?

时间:2014-07-02 12:11:12

标签: node.js sails.js

我目前正在使用Sails v0.10.0-rc7并尝试将控制台日志转发到文件中。 一些较旧的线程通过向config/log.js添加以下行来描述一种方法:

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

但这似乎不再起作用了。 Sails使用以前封装winston的子项目captains-log。根据README.md,我将config/log.js更改为:

var winston = require('winston');
module.exports = {
    'log': {
        'custom': new (winston.Logger)({
            'transports': [
                new (winston.transports.Console)({
                    'level': 'info',
                    'colorize': true,
                    'timestamp': false,
                    'json': false
                }),
                new winston.transports.File({
                    'level': 'debug',
                    'colorize': false,
                    'timestamp': true,
                    'json': true,
                    'filename': './logs/test.log',
                    'maxsize': 5120000,
                    'maxFiles': 3
                })
            ]
        })
    }
};

嗯,这样可行,但记录的消息在消息文本中始终有一个彩色前缀。所以控制台中的输出看起来像

info: info:
info: info:
info: info:    Sails              <|
info: info:    v0.10.0-rc7         |\
info: info:                       /|.\
info: info:                      / || \
info: info:                    ,'  |'  \
info: info:                 .-'.-==|/_--'
info: info:                 `--'-------'
info: info:    __---___--___---___--___---___--___
info: info:  ____---___--___---___--___---___--___-__
info: info:

(两个信息标签都以绿色显示) 并且日志文件消息看起来像

{"level":"info","message":"\u001b[32minfo: \u001b[39m","timestamp":"2014-07-02T10:51:13.517Z"}
{"level":"info","message":"\u001b[32minfo: \u001b[39m","timestamp":"2014-07-02T10:51:13.517Z"}
{"level":"info","message":"\u001b[32minfo: \u001b[39m   Sails              <|","timestamp":"2014-07-02T10:51:13.517Z"}
{"level":"info","message":"\u001b[32minfo: \u001b[39m   v0.10.0-rc7         |\\","timestamp":"2014-07-02T10:51:13.517Z"}
{"level":"info","message":"\u001b[32minfo: \u001b[39m                      /|.\\","timestamp":"2014-07-02T10:51:13.517Z"}
{"level":"info","message":"\u001b[32minfo: \u001b[39m                     / || \\","timestamp":"2014-07-02T10:51:13.517Z"}
{"level":"info","message":"\u001b[32minfo: \u001b[39m                   ,'  |'  \\","timestamp":"2014-07-02T10:51:13.527Z"}
{"level":"info","message":"\u001b[32minfo: \u001b[39m                .-'.-==|/_--'","timestamp":"2014-07-02T10:51:13.527Z"}
{"level":"info","message":"\u001b[32minfo: \u001b[39m                `--'-------' ","timestamp":"2014-07-02T10:51:13.527Z"}
{"level":"info","message":"\u001b[32minfo: \u001b[39m   __---___--___---___--___---___--___","timestamp":"2014-07-02T10:51:13.527Z"}
{"level":"info","message":"\u001b[32minfo: \u001b[39m ____---___--___---___--___---___--___-__","timestamp":"2014-07-02T10:51:13.527Z"}
{"level":"info","message":"\u001b[32minfo: \u001b[39m","timestamp":"2014-07-02T10:51:13.527Z"}

在生产模式下工作会抑制前缀,但我仍然在日志消息中有一些颜色编码。

那么,有没有人知道如何摆脱日志消息中的前缀和颜色编码?

1 个答案:

答案 0 :(得分:13)

我可以通过添加colors: false

来实现我的目标
var winston = require('winston');
module.exports = {
    'log': {
        'colors': false,
        'custom': new (winston.Logger)({
            'transports': [
                new (winston.transports.Console)({
                    'level': 'info',
                    'colorize': true,
                    'timestamp': false,
                    'json': false
                }),
                new winston.transports.File({
                    'level': 'debug',
                    'colorize': false,
                    'timestamp': true,
                    'json': true,
                    'filename': './logs/test.log',
                    'maxsize': 5120000,
                    'maxFiles': 3
                })
            ]
        })
    }
};