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.

custom level not work in typescript

See original GitHub issue

Please tell us about your environment:

  • winston version?
    • winston@2
    • winston@3
  • _node -v outputs:_v8.12.0
  • Operating System? macOS
  • Language? | TypeScript 3.1.3

What is the problem?

custom level not work in typescript 3.1.3

code

import winston from 'winston';

const myCustomLevels: winston.config.AbstractConfigSetLevels = {
  foo: 0,
  bar: 1,
  baz: 2,
  foobar: 3
};

const customLevelLogger = winston.createLogger({
  levels: myCustomLevels,
  transports: [
    new winston.transports.Console({
      level: 'foobar'
    })
  ]
});
//  [ts] Property 'foobar' does not exist on type 'Logger'.
customLevelLogger.foobar('some foobar level-ed message');

Issue Analytics

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

github_iconTop GitHub Comments

8reactions
jensbodalcommented, Nov 6, 2018

Another alternative is to just extend the winston.Logger interface and then cast the result of createLogger to your interface.

import winston from 'winston';

const myCustomLevels: winston.config.AbstractConfigSetLevels = {
  foo: 0,
  bar: 1,
  baz: 2,
  foobar: 3
};

interface CustomLevels extends winston.Logger {
  foo: winston.LeveledLogMethod;
  bar: winston.LeveledLogMethod;
  baz: winston.LeveledLogMethod;
  foobar: winston.LeveledLogMethod;
}

const customLevelLogger: CustomLevels = <CustomLevels>winston.createLogger({
  levels: myCustomLevels,
  transports: [
    new winston.transports.Console({
      level: 'foobar'
    })
  ]
});

customLevelLogger.foobar('some foobar level-ed message');
3reactions
evankennedycommented, Oct 13, 2020

It would be even better if each logger could infer levels from the createLogger call. That means if we exclude certain common levels from the levels config, we will get the proper compile-time error if we try calling that.

For example, we could make it infer the levels in this example:

const logger = winston.createLogger({
  levels: { custom: 0 },
});

// works:
logger.custom('stuff');
logger.log('custom', 'stuff');

// compile error:
logger.info('stuff');
logger.log('info', 'stuff');

I’m happy to work on a PR when I get some free time.


Edit: I realize maybe this can’t be done since Winston can be augmented on the fly with .configure() 😫

Read more comments on GitHub >

github_iconTop Results From Across the Web

Winston custom log levels typescript definitions - Stack Overflow
The definitions as currently written do not unfortunately allow for custom log levels. The simplest solution is to cast the returned logger ......
Read more >
Advanced integration with custom typescript component ...
Advanced integration with custom typescript component (Implementation) In this tutorial, you'll inspect each individual TypeScript file in the tutorial project ...
Read more >
CUSTOM ERRORS in TypeScript? - Advanced ... - YouTube
Become a TypeScript Wizard with Matt's upcoming TypeScript Course:https://www.mattpocock.com/Follow Matt on ...
Read more >
Tasks in Visual Studio Code
You can create user level tasks that are not tied to a specific workspace or ... A tasks.json that configures the npm: run...
Read more >
Documentation - Module Resolution - TypeScript
If that didn't work and if the module name is non-relative (and in the case of ... A node_modules folder can be on...
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