tsConversion: logger

This commit is contained in:
Sidharth Vinod 2022-08-21 00:59:54 +05:30
parent c9cd56914f
commit 896154d89f
No known key found for this signature in database
GPG Key ID: FB5CCD378D3907CD
2 changed files with 22 additions and 23 deletions

View File

@ -1,5 +1,5 @@
let interactionFunctions = [];
export const addFunction = (func) => {
let interactionFunctions: (() => {})[] = [];
export const addFunction = (func: () => {}) => {
interactionFunctions.push(func);
};
export const attachFunctions = () => {

View File

@ -1,9 +1,8 @@
import moment from 'moment-mini';
/** @typedef {'debug' | 'info' | 'warn' | 'error' | 'fatal'} LogLevel A log level */
export type LogLevel = 'debug' | 'info' | 'warn' | 'error' | 'fatal';
/** @type {Object<LogLevel, number>} */
export const LEVELS = {
export const LEVELS: Record<LogLevel, number> = {
debug: 1,
info: 2,
warn: 3,
@ -11,12 +10,12 @@ export const LEVELS = {
fatal: 5,
};
export const log = {
debug: (...args) => {},
info: (...args) => {},
warn: (...args) => {},
error: (...args) => {},
fatal: (...args) => {},
export const log: Record<keyof typeof LEVELS, typeof console.log> = {
debug: (..._args: any[]) => {},
info: (..._args: any[]) => {},
warn: (..._args: any[]) => {},
error: (..._args: any[]) => {},
fatal: (..._args: any[]) => {},
};
/**
@ -24,40 +23,40 @@ export const log = {
*
* @param {LogLevel} [level="fatal"] The level to set the logging to. Default is `"fatal"`
*/
export const setLogLevel = function (level = 'fatal') {
if (isNaN(level)) {
export const setLogLevel = function (level: keyof typeof LEVELS | number | string = 'fatal') {
let numericLevel: number = LEVELS.fatal;
if (typeof level === 'string') {
level = level.toLowerCase();
if (LEVELS[level] !== undefined) {
level = LEVELS[level];
if (level in LEVELS) {
numericLevel = LEVELS[level as keyof typeof LEVELS];
}
}
log.trace = () => {};
log.debug = () => {};
log.info = () => {};
log.warn = () => {};
log.error = () => {};
log.fatal = () => {};
if (level <= LEVELS.fatal) {
if (numericLevel <= LEVELS.fatal) {
log.fatal = console.error
? console.error.bind(console, format('FATAL'), 'color: orange')
: console.log.bind(console, '\x1b[35m', format('FATAL'));
}
if (level <= LEVELS.error) {
if (numericLevel <= LEVELS.error) {
log.error = console.error
? console.error.bind(console, format('ERROR'), 'color: orange')
: console.log.bind(console, '\x1b[31m', format('ERROR'));
}
if (level <= LEVELS.warn) {
if (numericLevel <= LEVELS.warn) {
log.warn = console.warn
? console.warn.bind(console, format('WARN'), 'color: orange')
: console.log.bind(console, `\x1b[33m`, format('WARN'));
}
if (level <= LEVELS.info) {
log.info = console.info // ? console.info.bind(console, '\x1b[34m', format('INFO'), 'color: blue')
if (numericLevel <= LEVELS.info) {
log.info = console.info
? console.info.bind(console, format('INFO'), 'color: lightblue')
: console.log.bind(console, '\x1b[34m', format('INFO'));
}
if (level <= LEVELS.debug) {
if (numericLevel <= LEVELS.debug) {
log.debug = console.debug
? console.debug.bind(console, format('DEBUG'), 'color: lightgreen')
: console.log.bind(console, '\x1b[32m', format('DEBUG'));
@ -70,7 +69,7 @@ export const setLogLevel = function (level = 'fatal') {
* @param {LogLevel} level The level for the log format
* @returns {string} The format with the timestamp and log level
*/
const format = (level) => {
const format = (level: string): string => {
const time = moment().format('ss.SSS');
return `%c${time} : ${level} : `;
};