Created
May 19, 2019 00:12
-
-
Save IgorHalfeld/1b2229230d512dadc2909cbf18c828ee to your computer and use it in GitHub Desktop.
Simple and functional logger written in pure TypeScript
This file contains hidden or 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
const info: Function = console.info; | |
const warn: Function = console.warn; | |
const error: Function = console.error; | |
const table: Function = console.table; | |
/** | |
* Add date before de args | |
* @param {Function} fn | |
* @param {Array} args | |
*/ | |
const withDateMessage = (fn: Function) => (args: Array<string>) => { | |
if (args.length) fn(`${new Date().toISOString()} -`, ...args); | |
}; | |
type Methods = { | |
[warn: string]: Function, | |
info: Function, | |
error: Function, | |
table: Function, | |
} | |
/** | |
* Simple logger | |
* @param {isProduction} Boolean | |
* @param {logLevel} String | |
* @returns {Object} | |
*/ | |
export const Logger = (isProduction: Boolean): Function => (logLevel: string): Function => { | |
const methods: Methods = { | |
warn: (...args: Array<string>): void => { | |
return withDateMessage(warn)(isProduction ? [] : args); | |
}, | |
error: (...args: Array<string>): void => { | |
return withDateMessage(error)(isProduction ? [] : args); | |
}, | |
info: (...args: Array<string>): void => { | |
return withDateMessage(info)(isProduction ? [] : args); | |
}, | |
table: (...args: Array<string>): void => { | |
return withDateMessage(table)(isProduction ? [] : args); | |
}, | |
}; | |
return methods[logLevel]; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment