Sailsjs - Custom Logging with Winston

爷,独闯天下 提交于 2019-12-23 01:04:40

问题


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:


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");


来源:https://stackoverflow.com/questions/26258774/sailsjs-custom-logging-with-winston

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!