Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: cannot extend winston.logger #2170

Closed
tomaswarynyca opened this issue Jul 29, 2022 · 7 comments · Fixed by #2181
Closed

[Bug]: cannot extend winston.logger #2170

tomaswarynyca opened this issue Jul 29, 2022 · 7 comments · Fixed by #2181

Comments

@tomaswarynyca
Copy link

tomaswarynyca commented Jul 29, 2022

🔎 Search Terms

class extend, extend logger

The problem

TypeError: Class extends value undefined is not a constructor or null

What version of Winston presents the issue?

v3.8.1

What version of Node are you using?

v16.16.0

If this worked in a previous version of Winston, which was it?

No response

Minimum Working Example

import * as winston from 'winston'

class LogService extends winston.Logger {
  private static instance: LogService

  constructor () {
    super({
      level: process.env.LOG_LEVEL ?? 'info',
      exitOnError: false,
      format: winston.format.combine(winston.format.timestamp(), winston.format.json()),
    })
  }

  static getInstance = (): LogService => {
    if (LogService.instance == null) { LogService.instance = new LogService() }
    return LogService.instance
  }
}

export default LogService
@jianzhou520
Copy link

same issue

@Faithfinder
Copy link
Contributor

So, Logger is a class because of the latest version update - #2157
However, it's simply not exported from https://github.com/winstonjs/winston/blob/master/lib/winston.js

There's even a warning that it's not allowed:

winston/lib/winston.js

Lines 175 to 176 in acaa95b

// Throw a useful error when users attempt to run `new winston.Logger`.
warn.moved(exports, 'createLogger', 'Logger');

Personally, I would like to extend the Logger as well. My purpose is providing default messages for common situations, for example:

    logEntityDeleted = (entity: EntityLike, additionalMetadata?: Record<string, unknown>): void => {
        this.log({
            level: 'audit',
            message: `${entityToString(entity)}, was deleted`,
            eventType: 'deleteEntity',
            entity: entity.constructor.name,
            entityId: entity.id,
            entityName: entity?.name,
            ...additionalMetadata,
        });
    };

I think the fix is as simple as exporting the Logger class, though that warning shows that it's not exported intentionally.

@ludgerey
Copy link

ludgerey commented Aug 30, 2022

Having a similar issue with Transport. Is this related?

class WinstonEventEmitterTransport extends Transport {
                                           ^
TypeError: Class extends value undefined is not a constructor or null

Update: Fixed it by setting "esModuleInterop": true" in tsconfig.json.

@mikebarr24
Copy link
Contributor

Having a similar issue with Transport. Is this related?

class WinstonEventEmitterTransport extends Transport {
                                           ^
TypeError: Class extends value undefined is not a constructor or null

Not sure if I've understood correctly but I am able to extend from Transport class using the 'winston-transport' module.

@ludgerey
Copy link

ludgerey commented Sep 8, 2022

Having a similar issue with Transport. Is this related?

class WinstonEventEmitterTransport extends Transport {
                                           ^
TypeError: Class extends value undefined is not a constructor or null

Not sure if I've understood correctly but I am able to extend from Transport class using the 'winston-transport' module.

I got my specific problem fixed by by setting "esModuleInterop": true" in tsconfig.json. Sorry for the late update.

@marc-wilson
Copy link

+1

@DABH
Copy link
Contributor

DABH commented Jul 10, 2023

This should be solved via #2181 and will go out in the next release

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants