consola icon indicating copy to clipboard operation
consola copied to clipboard

I would like to be able to determine the 'friendly' name of the log level from the numeric level

Open trevor-vaughan opened this issue 2 years ago • 4 comments

Describe the feature

The default level is 3 but that isn't very user-friendly when trying to pass it back to users.

I would like to be able to lookup the 'friendly' name of the level from the current level setting.

Additional information

  • [ ] Would you be willing to help implement this feature?

trevor-vaughan avatar Jun 29 '23 16:06 trevor-vaughan

@trevor-vaughan

At the moment, you could look up string name of log level by searching LogLevel constants:

import { LogLevels } from 'consola'

const [levelName, _] = Object.entries(LogLevels).find(([levelName, levelNum]) => levelNum === <certain_num>)

levelName // 'debug', for example

Ref: https://github.com/unjs/consola/blob/c2ff2b6d610b2442768ed01cd9aede21fff7fdb9/src/constants.ts


Exporting such a utility function from consola would be enhancement in terms of DX.

nozomuikuta avatar Jun 29 '23 17:06 nozomuikuta

Thanks @NozomuIkuta. That's what I'm currently doing, it would just be nice to have a built-in, hence the feature request.

trevor-vaughan avatar Jun 29 '23 17:06 trevor-vaughan

I noticed that we have to take it into consideration that more than one keys (i.e. LogType) are associated with a number.

https://github.com/unjs/consola/blob/c2ff2b6d610b2442768ed01cd9aede21fff7fdb9/src/constants.ts#L6-L28

So, there seems to be no (easy) way a function can determine which LogType is actually meant by a number. 🤔

nozomuikuta avatar Jun 30 '23 16:06 nozomuikuta

@NozomuIkuta Hmm...maybe return the 'most common' one?

For example, for 3 I would expect info with the others as aliases.

trevor-vaughan avatar Jun 30 '23 17:06 trevor-vaughan