You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

146 lines
4.1 KiB

  1. /**
  2. * angular-simple-logger
  3. *
  4. * @version: 0.1.7
  5. * @author: Nicholas McCready
  6. * @date: Tue Jan 26 2016 10:15:01 GMT-0500 (EST)
  7. * @license: MIT
  8. */
  9. var angular = require('angular');
  10. angular.module('nemLogging', []);
  11. angular.module('nemLogging').provider('nemDebug', function (){
  12. var ourDebug = null;
  13. ourDebug = require('debug');
  14. this.$get = function(){
  15. //avail as service
  16. return ourDebug;
  17. };
  18. //avail at provider, config time
  19. this.debug = ourDebug;
  20. return this;
  21. });
  22. var bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
  23. slice = [].slice;
  24. angular.module('nemLogging').provider('nemSimpleLogger', [
  25. 'nemDebugProvider', function(nemDebugProvider) {
  26. var LEVELS, Logger, _debugCache, _fns, _isValidLogObject, _maybeExecLevel, _wrapDebug, i, key, len, nemDebug, val;
  27. nemDebug = nemDebugProvider.debug;
  28. _debugCache = {};
  29. _fns = ['debug', 'info', 'warn', 'error', 'log'];
  30. LEVELS = {};
  31. for (key = i = 0, len = _fns.length; i < len; key = ++i) {
  32. val = _fns[key];
  33. LEVELS[val] = key;
  34. }
  35. _maybeExecLevel = function(level, current, fn) {
  36. if (level >= current) {
  37. return fn();
  38. }
  39. };
  40. _isValidLogObject = function(logObject) {
  41. var isValid, j, len1;
  42. isValid = false;
  43. if (!logObject) {
  44. return isValid;
  45. }
  46. for (j = 0, len1 = _fns.length; j < len1; j++) {
  47. val = _fns[j];
  48. isValid = (logObject[val] != null) && typeof logObject[val] === 'function';
  49. if (!isValid) {
  50. break;
  51. }
  52. }
  53. return isValid;
  54. };
  55. /*
  56. Overide logeObject.debug with a nemDebug instance
  57. see: https://github.com/visionmedia/debug/blob/master/Readme.md
  58. */
  59. _wrapDebug = function(namespace, logObject) {
  60. var debugInstance, j, len1, newLogger;
  61. if (_debugCache[namespace] == null) {
  62. _debugCache[namespace] = nemDebug(namespace);
  63. }
  64. debugInstance = _debugCache[namespace];
  65. newLogger = {};
  66. for (j = 0, len1 = _fns.length; j < len1; j++) {
  67. val = _fns[j];
  68. newLogger[val] = val === 'debug' ? debugInstance : logObject[val];
  69. }
  70. return newLogger;
  71. };
  72. Logger = (function() {
  73. function Logger($log1) {
  74. var fn1, j, len1, level, logFns;
  75. this.$log = $log1;
  76. this.spawn = bind(this.spawn, this);
  77. if (!this.$log) {
  78. throw 'internalLogger undefined';
  79. }
  80. if (!_isValidLogObject(this.$log)) {
  81. throw '@$log is invalid';
  82. }
  83. this.doLog = true;
  84. logFns = {};
  85. fn1 = (function(_this) {
  86. return function(level) {
  87. logFns[level] = function() {
  88. var args;
  89. args = 1 <= arguments.length ? slice.call(arguments, 0) : [];
  90. if (_this.doLog) {
  91. return _maybeExecLevel(LEVELS[level], _this.currentLevel, function() {
  92. var ref;
  93. return (ref = _this.$log)[level].apply(ref, args);
  94. });
  95. }
  96. };
  97. return _this[level] = logFns[level];
  98. };
  99. })(this);
  100. for (j = 0, len1 = _fns.length; j < len1; j++) {
  101. level = _fns[j];
  102. fn1(level);
  103. }
  104. this.LEVELS = LEVELS;
  105. this.currentLevel = LEVELS.error;
  106. }
  107. Logger.prototype.spawn = function(newInternalLogger) {
  108. if (typeof newInternalLogger === 'string') {
  109. if (!_isValidLogObject(this.$log)) {
  110. throw '@$log is invalid';
  111. }
  112. if (!nemDebug) {
  113. throw 'nemDebug is undefined this is probably the light version of this library sep debug logggers is not supported!';
  114. }
  115. return _wrapDebug(newInternalLogger, this.$log);
  116. }
  117. return new Logger(newInternalLogger || this.$log);
  118. };
  119. return Logger;
  120. })();
  121. this.decorator = [
  122. '$log', function($delegate) {
  123. var log;
  124. log = new Logger($delegate);
  125. log.currentLevel = LEVELS.debug;
  126. return log;
  127. }
  128. ];
  129. this.$get = [
  130. '$log', function($log) {
  131. return new Logger($log);
  132. }
  133. ];
  134. return this;
  135. }
  136. ]);