Last active
January 24, 2018 13:58
-
-
Save dreamyguy/370bf99077e7afc8e711 to your computer and use it in GitHub Desktop.
A way to see console.log() messages without breaking old browsers AND only on dev servers
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
import {ENV} from '../../env'; // (ENV = 'dev' || 'prod') | |
export default function logMessage(message, styles) { | |
// 'ENV' will establish if this should be rendered or not. | |
// Since it's set to 'dev' by default, 'console.log' messages | |
// will not output in production. | |
if (ENV === 'dev' && typeof parent.window.console === 'object') { | |
parent.window.console.log(message, styles); | |
} | |
} | |
// example// Use: | |
const helloWorld = 'hello world'; | |
logMessage(`%c [helloWorld]: ${helloWorld}`, 'padding: 5px 5px 5px 2px; background: #bada55; color: black'); | |
logMessage(`%c [helloWorld]: ${helloWorld}`, 'padding: 5px 5px 5px 2px; background: #de1e7e; color: white'); | |
logMessage(`%c [helloWorld]: ${helloWorld}`, 'padding: 5px 5px 5px 2px; background: orange; color: white'); | |
logMessage(`%c [helloWorld]: ${helloWorld}`, 'padding: 5px 5px 5px 2px; background: #551a8b; color: white'); | |
logMessage(`%c [helloWorld]: ${helloWorld}`, 'padding: 5px 5px 5px 2px; background: black; color: white'); |
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
const logMessage = message => { | |
// set this to false if you don't want to print console log messages through this function | |
const debug = true; | |
if (debug && typeof parent.window.console === 'object') { | |
parent.window.console.log(message); | |
} | |
}; | |
// example | |
logMessage('hello world'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment