question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Bug]: 'level' parameter of winston.createLogger() no longer accepts type 'string', breaks ability to set log level via environment variable

See original GitHub issue

The problem

When using environment variables to set ‘level’ property of winston.createLogger({ level: ... }), VSCode displays the following error: Type 'string' is not assignable to type 'level | undefined'.

It appears that in v3.5.0 the type for the level parameter was changes from 'string | undefined' (as in v3.4.0 and prior) to 'level | undefined' where ‘level’ is a type defined by winston as type level = "debug" | "error" | "warn" | "info" | "http" | "verbose" | "silly"

Even when defining the type for the environment variable to winston.level this type error still occurs:

import { level } from 'winston'
declare namespace NodeJS {
  interface ProcessEnv {
    LOG_LEVEL: level
  }
}

then

import { createLogger } from 'winston'
const logger = createLogger({
  level: process.env.LOG_LEVEL  // error occurs here
})

Current workaround

import level and assert the environment variable as the level type.

import { createLogger, type level } from 'winston'

const logger = createLogger({
  level: process.env.LOG_LEVEL as level
})

What version of Winston presents the issue?

3.5.0

What version of Node are you using?

14.17.3

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

3.4.0

Minimum Working Example

No response

Additional information

No response

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:3
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

5reactions
carboneatercommented, Jan 28, 2022

So I opened #2048 to apply generics most everywhere to solve the overly strict types.

However, for all the flexibility & ease of use I’ve worked into that one, it is still not globally without breaking changes. (Typescript users using custom loglevels will need to tweak their type annotations.)

Going forward, I would strongly advise reverting #1896 and publishing it as 3.5.1.

#1896 & #2048 should be released as part of a new major version, if only because they can break TypeScript code using Winston.

1reaction
wbtcommented, Jan 31, 2022

Going forward, I would strongly advise reverting #1896 and publishing it as 3.5.1.

This has been done. Apologies for missing the impact of adding those types on custom levels! Also tests involving custom levels which would’ve caught that earlier are welcome.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Complete Winston Logger Guide With Hands-on Examples
We will create a custom instance of Winston–which gives us the ability to customize its properties (e.g., the colors used, the logging levels, ......
Read more >
How to set log level in Winston/Node.js - Stack Overflow
If you are using the default logger, you can adjust the log levels like this: const winston = require('winston'); // ... winston.level =...
Read more >
Changing log level at runtime using Winston logger
I am going to accomplish this using following techniques. Updates to the environment variables needs to be picked by Node Server; Changing the ......
Read more >
express-winston - npm
express.js middleware for winstonjs/winston. Latest version: 4.2.0, last published: a year ago. Start using express-winston in your project ...
Read more >
A Complete Guide to Winston Logging in Node.js - Better Stack
With this change in place, the application will log at the info level if the LOG_LEVEL variable is not set in the environment....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found