unjs / consola

🐨 Elegant Console Logger for Node.js and Browser
Other
6k stars 175 forks source link

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

Open trevor-vaughan opened 1 year ago

trevor-vaughan commented 1 year ago

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

NozomuIkuta commented 1 year ago

@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.

trevor-vaughan commented 1 year ago

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

NozomuIkuta commented 1 year ago

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. 🤔

trevor-vaughan commented 1 year ago

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

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