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