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 = []; let interactionFunctions: (() => {})[] = [];
export const addFunction = (func) => { export const addFunction = (func: () => {}) => {
interactionFunctions.push(func); interactionFunctions.push(func);
}; };
export const attachFunctions = () => { export const attachFunctions = () => {

View File

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