Skip to content

Instantly share code, notes, and snippets.

@embiem
Created May 5, 2017 12:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save embiem/fd8684a1cb6b101cdca7a1a4b45266d7 to your computer and use it in GitHub Desktop.
Save embiem/fd8684a1cb6b101cdca7a1a4b45266d7 to your computer and use it in GitHub Desktop.
Custom logging via ES6 class. Extendable.
class Logger {
constructor() {
if (!Logger.instance) {
Logger.instance = this;
}
return Logger.instance;
}
log(...args) {
console.log(...args);
}
warn(...args) {
console.warn(...args);
}
error(...args) {
console.error(...args);
}
}
// ensure Singleton
const instance = new Logger();
Object.freeze(instance);
export default instance;
Copy link

ghost commented Mar 6, 2020

You could minimize all this robust code with this implementation:

const logger = {
  log: (...args) => console.log(...args),
  warn: (...args) => console.warn(...args),
  error: (...args) => console.error(...args),
  debug: (...args) => console.debug(...args),
  info: (...args) => console.info(...args),
  table: (...args) => console.table(...args),
};

export default logger;

@baumannalexj
Copy link

You could minimize all this robust code with this implementation:

const logger = {
  log: (...args) => console.log(...args),
  warn: (...args) => console.warn(...args),
  error: (...args) => console.error(...args),
  debug: (...args) => console.debug(...args),
  info: (...args) => console.info(...args),
  table: (...args) => console.table(...args),
};

export default logger;

@sayeko nice, does yours export a singleton?

Copy link

ghost commented May 27, 2020

yes this is a singleton.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment