logger.js 501 B

12345678910111213141516171819202122
  1. const levels = ['error', 'warn', 'log', 'info'];
  2. let level = 'warn';
  3. function debug(method, ...args) {
  4. if (levels.indexOf(method) <= levels.indexOf(level)) {
  5. console[method](...args); // eslint-disable-line no-console
  6. }
  7. }
  8. function namespace(ns) {
  9. return levels.reduce((logger, method) => {
  10. logger[method] = debug.bind(console, method, ns);
  11. return logger;
  12. }, {});
  13. }
  14. namespace.level = newLevel => {
  15. level = newLevel;
  16. };
  17. debug.level = namespace.level;
  18. export default namespace;