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

Add Typescript support for custom log levels #2349

Open
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

eschablowski
Copy link

@eschablowski eschablowski commented Sep 15, 2023

This is a small change in the typescript types that allows for the automatic detection of log levels (including custom log levels).

Breaking Changes

  • This uses Template Literal Types and other string manipulation which is only available from TypeScript 4.1 onwards
  • This actually checks which log levels are there, and does not assume NPM, Syslog, etc. are there, so it may break some builds at transpile time (which would have broken in runtime)

To Do

  • Create a bigger test suite for TypeScript types (many other, dependent types are incompatible already)
  • Test on more TS versions (Not required, as per the previous todo makes this obsolete)

Fixes

eschablowski and others added 7 commits September 14, 2023 14:52
This update removes the "standard" utility method definitions to log to the defined log levels, and check whether they are activated and replaces them with mapped types.
This change allows for better intellisense, as well as more rigorous type checking in typescript
This is a fix to get the actual `is<Loglevel>Enabled`, which was a bug in the previous commit
@DABH
Copy link
Contributor

DABH commented Dec 12, 2023

This looks cool! I'm wondering about semver though. If this forces users to use a newer TS version, should this go into a major release for winston? Or what is the minimum TS version that winston is requiring right now? I recall there being some earlier discussion around us forcing users to migrate to a newer TS version, but I don't remember where those discussions ended up.

http: LeveledLogMethod;
verbose: LeveledLogMethod;
input: LeveledLogMethod;
silly: LeveledLogMethod;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  [key in T]: LeveledLogMethod;

should declare the respective methods for immediate use.

Might make sense to have the levels with their respective numbers as Type parameter:

interface LoggerBase<Levels extends AbstractConfigSetLevels> {
  [key in keyof Levels]: LeveledLogMethod;
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mind that custom log levels still break application-insights instrumentation translating them to the correct appinsights severities.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@eschablowski thoughts?

jayden-chan added a commit to saasquatch/program-tools that referenced this pull request Mar 5, 2024
Winston 3.12 introduced a breaking change which makes using syslog log
levels with TypeScript impossible:

winstonjs/winston@c3c3911

A fix is currently in progress but it has not been merged yet:

winstonjs/winston#2349

In the meantime, the peer dependency for Winston will be updated to
prevent consumers from installing the incompatible version.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants