forked from questdb/nodejs-questdb-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogging.ts
28 lines (25 loc) · 888 Bytes
/
logging.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
const LOG_LEVELS = {
error: { log: console.error, criticality: 3 },
warn: { log: console.warn, criticality: 2 },
info: { log: console.info, criticality: 1 },
debug: { log: console.debug, criticality: 0 },
};
const DEFAULT_CRITICALITY = LOG_LEVELS.info.criticality;
/**
* Simple logger to write log messages to the console. <br>
* Supported logging levels are `error`, `warn`, `info` and `debug`. <br>
* Throws an error if logging level is invalid.
*
* @param {'error'|'warn'|'info'|'debug'} level - The log level of the message.
* @param {string} message - The log message.
*/
function log(level: "error" | "warn" | "info" | "debug", message: string) {
const logLevel = LOG_LEVELS[level];
if (!logLevel) {
throw new Error(`Invalid log level: '${level}'`);
}
if (logLevel.criticality >= DEFAULT_CRITICALITY) {
logLevel.log(message);
}
}
export { log };