Sailsjs - Custom Logging with Winston

2019-05-31 06:57发布

I am currently trying to write a custom logger for sailsjs that will use winston to send files to either an s3 bucket or a mongodb database.

The documentation seems to be lacking but so far i have found this:

var customLogger = new winston.Logger({
transports: [
    new(winston.transports.File)({
        level: 'debug',
        filename: './logs/my_log_file.log'
    })
]
});

module.exports.log = {
    colors: false,  // To get clean logs without prefixes or color codings
    custom: customLogger
};

Which overall is not working for me.

Any ideas?

1条回答
老娘就宠你
2楼-- · 2019-05-31 07:18

After extending above MayBeColin's work, the working solution:

Create a new js file inside a config folder(code inside of this will be executed automatically by sails) and add mongodb transports as below,

var winston = require('winston');
var MongoDB = require('winston-mongodb').MongoDB;

var customLogger = new(winston.Logger)({
    transports: [
        new(winston.transports.MongoDB)({
            db: 'mongodb://localhost:27017/test',
            collection: 'logs',
            level: 'debug'
        })
    ]
});

module.exports.logging = {
    colors: false, // To get clean logs without prefixes or color codings
    custom: customLogger
};

And use it anywhere like

sails.config.logging.custom.debug("winston mongodb transport logging");
查看更多
登录 后发表回答