Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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;
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