winstonjs / winston

A logger for just about everything.
http://github.com/winstonjs/winston
MIT License
22.65k stars 1.8k forks source link

winston logger issue: Cannot read property 'length' of undefined #1932

Open hira-00 opened 3 years ago

hira-00 commented 3 years ago

Please tell us about your environment:

What is the problem?

My logger initialisation code with Winston (inside my MERN app) exists over here on stack overflow.

What do you expect to happen instead?

the error occurs when the terminal is in idle mode, but the app is running, so the error shouldn't be appearing at all. I think this is an internal Winston bug based on this previous issue.

Other information

This is my stack trace

  error: uncaughtException: Cannot read property 'length' of undefined
    TypeError: Cannot read property 'length' of undefined
    at DerivedLogger.add (/Users/user-2/bulk-code/platform-v2/node_modules/winston/lib/winston/logger.js:344:45)
    at DerivedLogger.transportEvent (/Users/user-2/bulk-code/platform-v2/node_modules/winston/lib/winston/logger.js:628:14)
    at LegacyTransportStream.emit (events.js:327:22)
    at LegacyTransportStream.EventEmitter.emit (domain.js:467:12)
    at LegacyTransportStream.transportError (/Users/user-2/bulk-code/platform-v2/node_modules/winston-transport/legacy.js:32:10)
    at exports.Papertrail.emit (events.js:315:20)
    at exports.Papertrail.EventEmitter.emit (domain.js:467:12)
    at exports.Papertrail.Papertrail._silentErrorEmitter (/Users/user-2/bulk-code/platform-v2/node_modules/winston-papertrail/lib/winston-papertrail.js:479:8)
    at TLSSocket.onErrored (/Users/user-2/bulk-code/platform-v2/node_modules/winston-papertrail/lib/winston-papertrail.js:236:8)
    at Object.onceWrapper (events.js:422:26)
    at TLSSocket.emit (events.js:315:20)
    at TLSSocket.EventEmitter.emit (domain.js:467:12)
    at emitErrorNT (internal/streams/destroy.js:106:8)
    at emitErrorCloseNT (internal/streams/destroy.js:74:3)
    at processTicksAndRejections (internal/process/task_queues.js:80:21)

Also, the issue occurs at line 345 of logger.js in Winston inside the add function w/ transport.log.length > 2

  add(transport) {
    // Support backwards compatibility with all existing `winston < 3.x.x`
    // transports which meet one of two criteria:
    // 1. They inherit from winston.Transport in  < 3.x.x which is NOT a stream.
    // 2. They expose a log method which has a length greater than 2 (i.e. more then
    //    just `log(info, callback)`.
    const target =
      !isStream(transport) || transport.log.length > 2
        ? new LegacyTransportStream({ transport })
        : transport;

    if (!target._writableState || !target._writableState.objectMode) {
      throw new Error(
        'Transports must WritableStreams in objectMode. Set { objectMode: true }.'
      );
    }

    // Listen for the `error` event and the `warn` event on the new Transport.
    this._onEvent('error', target);
    this._onEvent('warn', target);
    this.pipe(target);

    if (transport.handleExceptions) {
      this.exceptions.handle();
    }

    if (transport.handleRejections) {
      this.rejections.handle();
    }

    return this;
  }

Does anyone over here have a solution to this?

saumyasrivastava226 commented 11 months ago

The error message "Cannot read property 'length' of undefined" suggests that the transport.log object is undefined, and you're trying to access its 'length' property. Implementing the null safety in your code will solve this problem.

const target = !isStream(transport) || transport?.log?.length > 2 ? new LegacyTransportStream({ transport }) : transport;

"?."--- using this while trying to access the length of the log property will resolve this issue. In JavaScript, the ?. (optional chaining) is an operator introduced in ECMAScript 2020 (ES11) that allows you to safely access properties or call methods on an object without having to explicitly check if the object and its properties are defined. It helps to avoid "Cannot read property 'x' of undefined" or "Cannot read property 'y' of null" errors.

Hope this helps. Good luck!