Created
January 22, 2020 16:18
-
-
Save aercolino/b7987c91bd16e32f61a2967b90134aa6 to your computer and use it in GitHub Desktop.
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
import log from 'loglevel'; | |
function logger(name, prefix) { | |
const result = log.getLogger(name); | |
if (!prefix) return result; | |
if (prefix === result.prefix) return result; | |
result.prefix = prefix; | |
const original = { | |
trace: result.trace.bind(result), | |
debug: result.debug.bind(result), | |
info: result.info.bind(result), | |
warn: result.warn.bind(result), | |
error: result.error.bind(result), | |
}; | |
result.trace = (...args) => original.trace((typeof prefix === 'function' ? prefix() : prefix), ...args); | |
result.debug = (...args) => original.debug((typeof prefix === 'function' ? prefix() : prefix), ...args); | |
result.info = (...args) => original.info((typeof prefix === 'function' ? prefix() : prefix), ...args); | |
result.warn = (...args) => original.warn((typeof prefix === 'function' ? prefix() : prefix), ...args); | |
result.error = (...args) => original.error((typeof prefix === 'function' ? prefix() : prefix), ...args); | |
return result; | |
} | |
export default logger; |
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
import logger from './loglevel-with-prefix'; | |
const log = logger('my-logger', () => `${new Date().toISOString()} my-logger`); | |
log('> hey', 'you'); // 2020-01-22T16:08:11.361Z my-logger > hey you |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment