2024-01-01 21:11:32 +00:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2022-03-01 04:25:50 +00:00
|
|
|
// This module is browser compatible.
|
|
|
|
|
2022-11-25 11:40:23 +00:00
|
|
|
/**
|
2023-11-27 19:26:21 +00:00
|
|
|
* Use this to retrieve the numeric log level by it's associated name.
|
2022-11-25 11:40:23 +00:00
|
|
|
* Defaults to INFO.
|
|
|
|
*/
|
2023-11-27 19:26:21 +00:00
|
|
|
export const LogLevels = {
|
|
|
|
NOTSET: 0,
|
|
|
|
DEBUG: 10,
|
|
|
|
INFO: 20,
|
2024-02-02 05:57:14 +00:00
|
|
|
WARN: 30,
|
2023-11-27 19:26:21 +00:00
|
|
|
ERROR: 40,
|
|
|
|
CRITICAL: 50,
|
|
|
|
} as const;
|
|
|
|
|
|
|
|
/** Union of valid log levels */
|
|
|
|
export type LogLevel = typeof LogLevels[LevelName];
|
|
|
|
|
|
|
|
/** Union of valid log level names */
|
|
|
|
export type LevelName = Exclude<keyof typeof LogLevels, number>;
|
2020-04-25 09:13:26 +00:00
|
|
|
|
|
|
|
/** Permitted log level names */
|
2023-12-19 00:26:13 +00:00
|
|
|
export const LogLevelNames: LevelName[] = Object.keys(LogLevels).filter((key) =>
|
2020-04-25 09:13:26 +00:00
|
|
|
isNaN(Number(key))
|
2023-11-27 19:26:21 +00:00
|
|
|
) as LevelName[];
|
2018-12-19 18:16:45 +00:00
|
|
|
|
2023-11-27 19:26:21 +00:00
|
|
|
const byLevel: Record<LogLevel, LevelName> = {
|
|
|
|
[LogLevels.NOTSET]: "NOTSET",
|
|
|
|
[LogLevels.DEBUG]: "DEBUG",
|
|
|
|
[LogLevels.INFO]: "INFO",
|
2024-02-02 05:57:14 +00:00
|
|
|
[LogLevels.WARN]: "WARN",
|
2023-11-27 19:26:21 +00:00
|
|
|
[LogLevels.ERROR]: "ERROR",
|
|
|
|
[LogLevels.CRITICAL]: "CRITICAL",
|
2018-12-19 18:16:45 +00:00
|
|
|
};
|
|
|
|
|
2023-11-27 19:26:21 +00:00
|
|
|
/**
|
|
|
|
* Returns the numeric log level associated with the passed,
|
2020-04-25 09:13:26 +00:00
|
|
|
* stringy log level name.
|
|
|
|
*/
|
2024-01-09 01:43:19 +00:00
|
|
|
export function getLevelByName(name: LevelName): LogLevel {
|
2023-11-27 19:26:21 +00:00
|
|
|
const level = LogLevels[name];
|
|
|
|
if (level !== undefined) {
|
|
|
|
return level;
|
2020-04-25 09:13:26 +00:00
|
|
|
}
|
2024-08-24 14:55:45 +00:00
|
|
|
throw new Error(`Cannot get log level: no level named ${name}`);
|
2018-12-19 18:16:45 +00:00
|
|
|
}
|
|
|
|
|
2024-01-09 01:43:19 +00:00
|
|
|
/** Returns the stringy log level name provided the numeric log level. */
|
|
|
|
export function getLevelName(level: LogLevel): LevelName {
|
2023-11-27 19:26:21 +00:00
|
|
|
const levelName = byLevel[level as LogLevel];
|
2020-04-25 09:13:26 +00:00
|
|
|
if (levelName) {
|
|
|
|
return levelName;
|
|
|
|
}
|
2024-08-24 14:55:45 +00:00
|
|
|
throw new Error(`Cannot get log level: no name for level: ${level}`);
|
2018-12-19 18:16:45 +00:00
|
|
|
}
|