Skip to content

Instantly share code, notes, and snippets.

@acodesmith
Created December 22, 2016 16:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save acodesmith/95108edf05f9aad9059b74814a61d3a0 to your computer and use it in GitHub Desktop.
Save acodesmith/95108edf05f9aad9059b74814a61d3a0 to your computer and use it in GitHub Desktop.
Example of console
/**
* Custom Logger which only console logs to certain environment
*/
export default class Logger {
/**
* window.env var is set in the main yii2 layout
* If it's not set we default to prod
*/
constructor(options = {})
{
this._console = console
if( options ) {
var { env, ignore } = options
}
this.env = typeof env !== 'undefined' ? env : typeof window.env !== 'undefined' ? window.env : Logger.endProd
if( typeof ignore !== 'undefined' ) {
this.ignore = ignore
}else{
this.ignore = Logger.defaultEnvToIgnoreLog
}
if( this.ignore.indexOf( this.env ) === -1 ) {
console.log(`==== Logger: ${this.env}`)
console.log(`==== Logger Ignore: ${this.ignore.join()}`)
}
}
//Prod is the only set env, by deafult we always ignore prod
static get envProd() {
return 'prod'
}
static get defaultEnvToIgnoreLog() {
return [
Logger.envProd
]
}
/**
* Console log only if we aren't flagged as an ignored environment
*/
log() {
this.func('log',arguments);
}
/**
* Error message. Add custom logic as needed.
* Maybe GA error event?
*/
error() {
this.func('error',arguments);
}
/**
* Trigger any console function
* @param functionName
* @param data
*/
func(functionName, data) {
if( this.ignore.indexOf( this.env ) === -1 ){
this._console[ functionName ](...data)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment