使用现有的Winston记录器

时间:2018-06-29 09:44:09

标签: javascript node.js logging import winston

我是Node.js和Java语言的新手,所以我仍然在学习各种技巧。我来自python背景。

我有一个可导入的函数,该函数创建一个记录器,并将记录对象移回到main脚本中。

我正在将其他模块导入main脚本中,希望它们使用相同的日志记录对象/设置。

在Python中,记录器对象是一个单例对象-从下标或导入的方法再次实例化时,它将获取所有现有传输。

是否可以在JS中完成此操作,而不必仅将logger对象作为函数的参数传入?

logger.js

const { createLogger, transports } = require("winston");
let   { format }                   = require("winston");
let logger;
let log_level;
let logfile; 
let screendump;

function formatParams(info) { (...snip...) }

//Set format default here
format = format.combine( ...snip ...);

module.exports = {
    set: function (argv) { // defaults
        /* Set logging. Can be set by command line or config file */
        var log_level  = ( process.env.log_level  != null ? process.env.log_level  : argv["log_level"] )  // Can end as undefined
        var logfile    = ( process.env.logfile    != null ? process.env.logfile    : argv["logfile"]   )  // Can end as undefined
        var screendump = ( process.env.screendump != null ? process.env.screendump : argv["screendump"])  // Can end as undefined

        // Put a check in here to ensure "formatting" is a format object
        if ( typeof formatting !== 'undefined') { format = formatting; }

        logger = createLogger({
                    level : log_level,
                    format: format,
                    transports: [
                    // Start with everything going to dev null...just to satisfy
                    // Winston's requirement for at least one transport.
                    // Add more later 
                    new transports.File({ filename: '/dev/null', level: 'error' }),
                  ]
                });

        // put a file check in here
        if ( typeof logfile !== 'undefined' ) { 
            logger.add(new transports.File({ filename: logfile, format: format }) )
        }

        if ( typeof screendump !== 'undefined' || screendump != false) { 
            logger.add(new transports.Console({ format: format }) );
        }
        return logger;
    },

};

main.js

var   log        = require('./logger');
require('./mySubFunction')();

logger = log.set(argv);
logger.log('info',"Logger successfully started!");

mySubFunction(param = 10);

mySubFunction.js

logger = <import existing logger object created by main?>

module.exports = function() { 
    this.mySubFunction = function (param) { // defaults
        var i;
        for (i = 0; i < param; i++) { 
            logger.log('info', "This is iteration" + String(i));
        }
        return;
    }
}

1 个答案:

答案 0 :(得分:2)

您可以在一个文件中创建一个记录器实例,然后将其导出,从而创建一个单例。

logger.js

$(function(){
    $(document).scroll(function(){
        var checkboxes = document.getElementsByClassName("wiztype");
        var hasChecked = false;
        for(var i = 0; i < checkboxes.length; ++i) {
            if(checkboxes.item(i).checked) {
                hasChecked = true;
                break;
            }
        }
        if($(this).scrollTop() >= $('#questrow1').offset().top + 100 && !hasChecked) {
            $("#questrow1").addClass('redback');
        }
    });
});

somethingElse.js

var createLogger = require('winston');
module.exports = createLogger();

var logger = require('./logger'); 是全局的,因此您不必从process.argv中传递。