Created
December 2, 2016 13:09
-
-
Save domjtalbot/e3faddc72abdb90ba57f4ca96ceacf02 to your computer and use it in GitHub Desktop.
Custom debug class - better browser support
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
/* global:window */ | |
import npmdebug from 'debug'; | |
import './env.es6'; | |
npmdebug.enable(process.env.DEBUG); | |
const heNamespace = 'dom:'; | |
export const clientDebug = npmdebug; | |
/** | |
* Class representing a configured instance of [debug](https://www.npmjs.com/package/debug) | |
*/ | |
export class Debug { | |
static namespace; | |
/** | |
* Create instance of [debug](https://www.npmjs.com/package/debug) | |
* @param {string} namespace - Name to appear in debugging. | |
* @returns {Debug} Namespaced instance of [debug](https://www.npmjs.com/package/debug) | |
*/ | |
constructor(namespace) { | |
this.namespace = `${heNamespace}${namespace}`; | |
const debug = npmdebug(this.namespace); | |
debug(`${this.namespace} initialized`); | |
return debug; | |
} | |
static error = () => { | |
} | |
} | |
/** | |
* Class representing a configured instance of [debug](https://www.npmjs.com/package/debug) warning | |
*/ | |
export class Warning { | |
static namespace; | |
/** | |
* Create instance of [debug](https://www.npmjs.com/package/debug) warning | |
* @param {string} namespace - Name to appear in debugging warning. | |
* @returns {Debug} Namespaced instance of [debug](https://www.npmjs.com/package/debug) warning | |
*/ | |
constructor(namespace) { | |
this.namespace = `${heNamespace}${namespace}`; | |
const warn = npmdebug(this.namespace); | |
warn.log = (...args) => { | |
const consoletype = typeof console === 'object'; | |
/* eslint-disable no-console */ | |
const consoleError = console.warn; | |
const consoleBind = Function.prototype.apply.call(console.warn, console, args); | |
/* eslint-enable no-console */ | |
return consoletype && consoleError && consoleBind; | |
}; | |
return warn; | |
} | |
} | |
/** | |
* Class representing a configured instance of [debug](https://www.npmjs.com/package/debug) error | |
*/ | |
export class Error { | |
static namespace; | |
/** | |
* Create instance of [debug](https://www.npmjs.com/package/debug) error | |
* @param {string} namespace - Name to appear in debugging error. | |
* @returns {Debug} Namespaced instance of [debug](https://www.npmjs.com/package/debug) error | |
*/ | |
constructor(namespace) { | |
this.namespace = `${heNamespace}${namespace}`; | |
const error = npmdebug(this.namespace); | |
error.log = (...args) => { | |
const consoletype = typeof console === 'object'; | |
/* eslint-disable no-console */ | |
const consoleError = console.error; | |
const consoleBind = Function.prototype.apply.call(console.error, console, args); | |
/* eslint-enable no-console */ | |
return consoletype && consoleError && consoleBind; | |
}; | |
return error; | |
} | |
} | |
export { Debug as default }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment