Last active
November 6, 2020 11:30
-
-
Save Nitinrajyadav/7e195b6a855f67603f871a701713ca0b to your computer and use it in GitHub Desktop.
Simple TypeScript Logging Wrapper over console
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Logger { | |
private static __ENABLED__ == true; // use something like webpack-provider-plugin to inject __ENABLED__ | |
private static doLog = (fun: string, ...args: any[]) => { | |
if (!Logger.__ENABLED__) { return; } | |
console[fun]?.bind(console)(...args); | |
} | |
public static info = (...args: any[]) => Logger.doLog('info', '[INFO]', ...args ); | |
public static log = (...args: any[]) => Logger.doLog('log', '[LOG]', ...args ); | |
public static debug = (...args: any[]) => Logger.doLog('debug', '[DEBUG]', ...args ); | |
public static warn = (...args: any[]) => Logger.doLog('warn', '[WARN]', ...args ); | |
public static error = (...args: any[]) => Logger.doLog('error', '[ERROR]', ...args ); | |
public static assert = (...args: any[]) => Logger.doLog('assert', ...args); | |
public static trace = (...args: any[]) => Logger.doLog('trace', ...args); | |
public static table = (...args: any[]) => Logger.doLog('table', ...args); | |
public static count = (...args: any[]) => Logger.doLog('count', ...args); | |
public static time = (...args: any[]) => Logger.doLog('time', ...args); | |
public static timeEnd = (...args: any[]) => Logger.doLog('timeEnd', ...args); | |
public static group = (...args: any[]) => Logger.doLog('group', ...args); | |
public static groupEnd = (...args: any[]) => Logger.doLog('groupEnd', ...args); | |
} | |
// test | |
Logger.info(2134); | |
Logger.log(2134); | |
Logger.warn(2134); | |
Logger.error(2134); | |
Logger.assert(1==2); | |
Logger.trace(2134); | |
Logger.table({ 'a': 123 }); | |
Logger.count(2134); | |
Logger.time(2134); | |
Logger.timeEnd(2134); | |
Logger.group(2134); | |
Logger.groupEnd(2134); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment