Created
July 26, 2018 21:17
-
-
Save ZeroDragon/6468f18935dbce61c22b28f770ae9c71 to your computer and use it in GitHub Desktop.
Replace console.log
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
/* globals ENV */ | |
let environment = 'dev' | |
if (typeof ENV === 'undefined') environment = 'prod' | |
const logger = {} | |
const sty = color => `padding:4px;color:#fff;background-color:${color};` | |
const loggerTypes = { | |
debug: sty('green'), | |
info: sty('blue'), | |
warn: sty('orange'), | |
error: sty('red') | |
} | |
let innerLogger = () => {} | |
if (console && console.log) { | |
innerLogger = console.log | |
} | |
Object.keys(loggerTypes).forEach(type => { | |
const style = loggerTypes[type] | |
logger[type] = (...params) => { | |
if (environment === 'dev') { | |
innerLogger(`%c${type.toUpperCase()}`, style, ...params) | |
} | |
} | |
}) | |
if (console && console.log) { | |
console.log = logger.info | |
} | |
window.logger = logger |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Este es un mejor logger para frontend, expone en global el objeto
logger
con los métodosdebug
,info
,warn
yerror
. Alternativamente redirige las llamadas de console.log a logger.info.Mientras tu ENV variable (global, pasada por webpack u otro lado) sea 'dev', los logs van a salir en la consola. Cuando se hace el build a prod todos los logs desaparecen para quitar basura de la consola