index.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. 'use strict';
  2. require('object.assign/shim')();
  3. require('es6-symbol/implement');
  4. /* global window: true */
  5. const LogLevel = require('./lib/LogLevel');
  6. const MethodFactory = require('./lib/MethodFactory');
  7. const PrefixFactory = require('./factory/PrefixFactory');
  8. const defaultLogger = new LogLevel({ name: 'default' });
  9. const cache = { default: defaultLogger };
  10. // Grab the current global log variable in case of overwrite
  11. const existing = (typeof window !== 'undefined') ? window.log : null;
  12. module.exports = Object.assign(defaultLogger, {
  13. get factories() {
  14. return {
  15. MethodFactory,
  16. PrefixFactory
  17. };
  18. },
  19. get loggers() {
  20. return cache;
  21. },
  22. getLogger(options) {
  23. if (typeof options === 'string') {
  24. options = { name: options };
  25. }
  26. if (!options.id) {
  27. options.id = options.name;
  28. }
  29. const { name, id } = options;
  30. const defaults = { level: defaultLogger.level };
  31. if (typeof name !== 'string' || !name || !name.length) {
  32. throw new TypeError('You must supply a name when creating a logger.');
  33. }
  34. let logger = cache[id];
  35. if (!logger) {
  36. logger = new LogLevel(Object.assign({}, defaults, options));
  37. cache[id] = logger;
  38. }
  39. return logger;
  40. },
  41. noConflict() {
  42. if (typeof window !== 'undefined' && window.log === defaultLogger) {
  43. window.log = existing;
  44. }
  45. return defaultLogger;
  46. }
  47. });