unjs/consola

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

trevor-vaughan opened this issue · 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

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.

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

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

export const LogLevels: Record<LogType, number> = {
silent: Number.NEGATIVE_INFINITY,
fatal: 0,
error: 0,
warn: 1,
log: 2,
info: 3,
success: 3,
fail: 3,
ready: 3,
start: 3,
box: 3,
debug: 4,
trace: 5,
verbose: Number.POSITIVE_INFINITY,
};

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

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

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