Winston: handler.get() was called multiple times while using winston with the Proxy object

Created on 4 Sep 2019  路  2Comments  路  Source: winstonjs/winston

Please tell us about your environment:

  • _winston version?_ 3.2.1
  • _node -v outputs:_ v10.4.1
  • _Operating System?_ macOS 10.14.6
  • _Language?_ ES6

What is the problem?

while using the Proxy object, the handler.get() will be called multiple times.

e.g.

const winston = require('winston');
const logger = winston.createLogger();

const proxyLogger = new Proxy(logger, {
    get(target, prop) {
        console.log('handler.get() is called');
        return target[prop];
    }
});

proxyLogger.info('12312323');

the output result:

handler.get() is called
handler.get() is called
handler.get() is called
handler.get() is called
handler.get() is called
handler.get() is called
handler.get() is called
handler.get() is called
handler.get() is called
handler.get() is called
handler.get() is called
handler.get() is called
handler.get() is called
handler.get() is called
[winston] Attempt to write logs with no transports {"message":"12312323","level":"info"}
handler.get() is called
handler.get() is called
handler.get() is called
handler.get() is called
handler.get() is called
handler.get() is called
handler.get() is called
handler.get() is called
handler.get() is called
handler.get() is called
handler.get() is called
handler.get() is called
handler.get() is called
handler.get() is called
handler.get() is called

What do you expect to happen instead?

handler.get() should be called only once.

Most helpful comment

Modify your function to be called only for the info method you call

const proxyLogger = new Proxy(logger, {
    get: (target, prop) => {
        return (...args) => {
            console.log('handler.get() is called', prop);
            return target[prop](...args);
        }
    }
});

All 2 comments

if you edit the console.log and print the prop variable you will see its getting called for all the methods inside winston

handler.get() is called info
handler.get() is called _addDefaultMeta
handler.get() is called defaultMeta
handler.get() is called write
handler.get() is called _writableState
handler.get() is called _write
handler.get() is called _transformState
handler.get() is called _readableState
handler.get() is called _read
handler.get() is called _transformState
handler.get() is called _transform
handler.get() is called silent
handler.get() is called levels
handler.get() is called _readableState
[winston] Attempt to write logs with no transports {"message":"12312323","level":"info"}
handler.get() is called push
handler.get() is called format
handler.get() is called format
handler.get() is called _transformState
handler.get() is called _readableState
handler.get() is called _readableState
handler.get() is called _readableState
handler.get() is called _readableState
handler.get() is called emit
handler.get() is called _events
handler.get() is called domain
handler.get() is called _readableState
handler.get() is called read
handler.get() is called _readableState
handler.get() is called _read
handler.get() is called _transformState

Modify your function to be called only for the info method you call

const proxyLogger = new Proxy(logger, {
    get: (target, prop) => {
        return (...args) => {
            console.log('handler.get() is called', prop);
            return target[prop](...args);
        }
    }
});
Was this page helpful?
0 / 5 - 0 ratings

Related issues

Tonacatecuhtli picture Tonacatecuhtli  路  4Comments

anks333 picture anks333  路  3Comments

jlank picture jlank  路  4Comments

JaehyunLee-B2LiNK picture JaehyunLee-B2LiNK  路  3Comments

sinai-doron picture sinai-doron  路  3Comments