Winston with 类 - 为每个文件创建新实例

Winston with classes - creating new instance for each file

我不确定,但我觉得这段代码有问题。假设我有一个 winston class,每次使用记录器时都会创建一个新实例,即每个文件。

这个逻辑有问题吗?

// In logger.js
const winston = require('winston');

class Winston {
    constructor(errorPath) {
        const format = winston.format;
        const customFormatter = format((info) => {
            return Object.assign({
                timestamp: info.timestamp
            }, info);
        });

        let settings = {
            level: 'silly',
            format: winston.format.json(),
            transports: [
                new (winston.transports.File)({
                    filename: errorPath + '/error.log',
                    level: 'error',
                    handleExceptions: true,
                    stack: true,
                    format: format.combine(
                        format.timestamp(),
                        customFormatter(),
                        format.json()
                    )
                }),
                new (winston.transports.File)({
                    filename: errorPath + '/general.log',
                    format: format.combine(
                        format.timestamp(),
                        customFormatter(),
                        format.json()
                    )
                })
            ],
            exitOnError: false
        };
        settings.transports.push(new (winston.transports.Console)());
        return new winston.createLogger(settings);
    }
}

module.exports = Winston;

那我们假设还有其他文件。

a.js
const Loger = require('./logger');
const logger = new Loger('somepath');
logger.error('Some error');
logger.debug('Some debug');
b.js
const Loger = require('./logger');
const logger = new Loger('somepath');
logger.error('Some error');
logger.debug('Some debug');
c.js
const Loger = require('./logger');
const logger = new Loger('somepath');
logger.error('Some error');
logger.debug('Some debug');

这会产生不必要的开销吗?

Suppose I have a winston class

这是第一个问题。您不应在此处使用 class。唯一的 class Winston 应该在 winston 库本身内。而你的 class 实际上只是一个工厂函数,就像 createLogger 一样:它没有任何方法,而构造函数 returns 是一个对象。

a new instance is created every time the logger is used, which is every file

除非您每次记录一条消息时都创建一个新的记录器,否则可以为每个文件创建一个记录器,然后在文件中多次使用该记录器。

somepath is the same in each file

这实际上可能有问题。每个输出文件应该有一个传输,否则当两个记录器试图同时写入同一个文件时,您可能会出现竞争条件。

是的,如果它们都共享相同的配置,那么创建多个记录器实例只是一种浪费。为什么不直接在 logger.js 中创建记录器并共享这个实例?

// logger.js
const winston = require('winston');

const errorPath = 'somepath';
const format = winston.format;
const customFormatter = format((info) => {
    return Object.assign({
        timestamp: info.timestamp
    }, info);
});

let settings = {
    level: 'silly',
    format: winston.format.json(),
    transports: [
        new (winston.transports.File)({
            filename: errorPath + '/error.log',
            level: 'error',
            handleExceptions: true,
            stack: true,
            format: format.combine(
                format.timestamp(),
                customFormatter(),
                format.json()
            )
        }),
        new (winston.transports.File)({
            filename: errorPath + '/general.log',
            format: format.combine(
                format.timestamp(),
                customFormatter(),
                format.json()
            )
        })
    ],
    exitOnError: false
};
settings.transports.push(new (winston.transports.Console)());

module.exports = new winston.createLogger(settings);
// a-module.js
const logger = require('./logger');
logger.error('Some error');
logger.debug('Some debug');