Skip to content

Commit 679b119

Browse files
committed
feat: add getLogLevelName
1 parent ece71aa commit 679b119

5 files changed

Lines changed: 57 additions & 0 deletions

File tree

.README/README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,26 @@ Produces output:
426426
427427
```
428428

429+
## Utilities
430+
431+
### `getLogLevelName`
432+
433+
Provides log level name (trace, debug, ...) for a numeric log level (10, 20, ...).
434+
435+
If numeric log level is between two ranges, then resolves to the one with greater severity (e.g. 5 => trace).
436+
437+
If numeric log level is greater than the maximum supported, then falls back to the greatest severity (fatal).
438+
439+
```js
440+
import {
441+
getLogLevelName,
442+
} from 'roarr';
443+
import type {
444+
LogLevelName,
445+
} from 'roarr';
446+
447+
getLogLevelName(numericLogLevel: number): LogLevelName;
448+
```
429449
## Middlewares
430450

431451
Roarr logger supports middlewares implemented as [`child`](#child) message translate functions, e.g.

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ JSON logger for Node.js and browser.
2626
* [`warn`](#roarr-api-warn)
2727
* [`error`](#roarr-api-error)
2828
* [`fatal`](#roarr-api-fatal)
29+
* [Utilities](#roarr-utilities)
30+
* [`getLogLevelName`](#roarr-utilities-getloglevelname)
2931
* [Middlewares](#roarr-middlewares)
3032
* [CLI program](#roarr-cli-program)
3133
* [Transports](#roarr-transports)
@@ -485,6 +487,28 @@ Produces output:
485487
486488
```
487489

490+
<a name="roarr-utilities"></a>
491+
## Utilities
492+
493+
<a name="roarr-utilities-getloglevelname"></a>
494+
### <code>getLogLevelName</code>
495+
496+
Provides log level name (trace, debug, ...) for a numeric log level (10, 20, ...).
497+
498+
If numeric log level is between two ranges, then resolves to the one with greater severity (e.g. 5 => trace).
499+
500+
If numeric log level is greater than the maximum supported, then falls back to the greatest severity (fatal).
501+
502+
```js
503+
import {
504+
getLogLevelName,
505+
} from 'roarr';
506+
import type {
507+
LogLevelName,
508+
} from 'roarr';
509+
510+
getLogLevelName(numericLogLevel: number): LogLevelName;
511+
```
488512
<a name="roarr-middlewares"></a>
489513
## Middlewares
490514

src/Roarr.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ import {
1515
import {
1616
createRoarrInitialGlobalState,
1717
} from './factories/createRoarrInitialGlobalState';
18+
import {
19+
getLogLevelName,
20+
} from './getLogLevelName';
1821
import type {
1922
RoarrGlobalState,
2023
} from './types';
@@ -66,6 +69,7 @@ const Roarr = logFactory((message) => {
6669

6770
export type {
6871
Logger,
72+
LogLevelName,
6973
LogWriter,
7074
Message,
7175
MessageEventHandler,
@@ -74,6 +78,7 @@ export type {
7478
} from './types';
7579

7680
export {
81+
getLogLevelName,
7782
logLevels,
7883
Roarr,
7984
ROARR,

src/browser.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ import {
88
import {
99
createRoarrInitialGlobalStateBrowser,
1010
} from './factories/createRoarrInitialGlobalStateBrowser';
11+
import {
12+
getLogLevelName,
13+
} from './getLogLevelName';
1114
import type {
1215
RoarrGlobalState,
1316
} from './types';
@@ -28,13 +31,15 @@ const Roarr = createLogger((message) => {
2831

2932
export type {
3033
Logger,
34+
LogLevelName,
3135
Message,
3236
MessageEventHandler,
3337
RoarrGlobalState,
3438
TranslateMessageFunction,
3539
} from './types';
3640

3741
export {
42+
getLogLevelName,
3843
logLevels,
3944
Roarr,
4045
ROARR,

src/types.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,6 @@ export type Logger = LogMethod & {
6969
};
7070

7171
export type MessageEventHandler = (message: Message) => void;
72+
73+
// eslint-disable-next-line @typescript-eslint/sort-type-union-intersection-members
74+
export type LogLevelName = 'trace' | 'debug' | 'info' | 'error' | 'fatal' | 'warn';

0 commit comments

Comments
 (0)