Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
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.

loglevel-plugin-prefix.js 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. (function (root, factory) {
  2. if (typeof define === 'function' && define.amd) {
  3. define(factory);
  4. } else if (typeof module === 'object' && module.exports) {
  5. module.exports = factory();
  6. } else {
  7. root.prefix = factory(root);
  8. }
  9. }(this, function (root) {
  10. 'use strict';
  11. var merge = function (target) {
  12. var i = 1;
  13. var length = arguments.length;
  14. var key;
  15. for (; i < length; i++) {
  16. for (key in arguments[i]) {
  17. if (Object.prototype.hasOwnProperty.call(arguments[i], key)) {
  18. target[key] = arguments[i][key];
  19. }
  20. }
  21. }
  22. return target;
  23. };
  24. var defaults = {
  25. template: '[%t] %l:',
  26. levelFormatter: function (level) {
  27. return level.toUpperCase();
  28. },
  29. nameFormatter: function (name) {
  30. return name || 'root';
  31. },
  32. timestampFormatter: function (date) {
  33. return date.toTimeString().replace(/.*(\d{2}:\d{2}:\d{2}).*/, '$1');
  34. },
  35. format: undefined
  36. };
  37. var loglevel;
  38. var configs = {};
  39. var reg = function (rootLogger) {
  40. if (!rootLogger || !rootLogger.getLogger) {
  41. throw new TypeError('Argument is not a root logger');
  42. }
  43. loglevel = rootLogger;
  44. };
  45. var apply = function (logger, config) {
  46. if (!logger || !logger.setLevel) {
  47. throw new TypeError('Argument is not a logger');
  48. }
  49. /* eslint-disable vars-on-top */
  50. var originalFactory = logger.methodFactory;
  51. var name = logger.name || '';
  52. var parent = configs[name] || configs[''] || defaults;
  53. /* eslint-enable vars-on-top */
  54. function methodFactory(methodName, logLevel, loggerName) {
  55. var originalMethod = originalFactory(methodName, logLevel, loggerName);
  56. var options = configs[loggerName] || configs[''];
  57. var hasTimestamp = options.template.indexOf('%t') !== -1;
  58. var hasLevel = options.template.indexOf('%l') !== -1;
  59. var hasName = options.template.indexOf('%n') !== -1;
  60. return function () {
  61. var content = '';
  62. var length = arguments.length;
  63. var args = Array(length);
  64. var key = 0;
  65. for (; key < length; key++) {
  66. args[key] = arguments[key];
  67. }
  68. // skip the root method for child loggers to prevent duplicate logic
  69. if (name || !configs[loggerName]) {
  70. /* eslint-disable vars-on-top */
  71. var timestamp = options.timestampFormatter(new Date());
  72. var level = options.levelFormatter(methodName);
  73. var lname = options.nameFormatter(loggerName);
  74. /* eslint-enable vars-on-top */
  75. if (options.format) {
  76. content += options.format(level, lname, timestamp);
  77. } else {
  78. content += options.template;
  79. if (hasTimestamp) {
  80. content = content.replace(/%t/, timestamp);
  81. }
  82. if (hasLevel) content = content.replace(/%l/, level);
  83. if (hasName) content = content.replace(/%n/, lname);
  84. }
  85. if (args.length && typeof args[0] === 'string') {
  86. // concat prefix with first argument to support string substitutions
  87. args[0] = content + ' ' + args[0];
  88. } else {
  89. args.unshift(content);
  90. }
  91. }
  92. originalMethod.apply(undefined, args);
  93. };
  94. }
  95. if (!configs[name]) {
  96. logger.methodFactory = methodFactory;
  97. }
  98. // for remove inherited format option if template option preset
  99. config = config || {};
  100. if (config.template) config.format = undefined;
  101. configs[name] = merge({}, parent, config);
  102. logger.setLevel(logger.getLevel());
  103. if (!loglevel) {
  104. logger.warn(
  105. 'It is necessary to call the function reg() of loglevel-plugin-prefix before calling apply. From the next release, it will throw an error. See more: https://github.com/kutuluk/loglevel-plugin-prefix/blob/master/README.md'
  106. );
  107. }
  108. return logger;
  109. };
  110. var api = {
  111. reg: reg,
  112. apply: apply
  113. };
  114. var save;
  115. if (root) {
  116. save = root.prefix;
  117. api.noConflict = function () {
  118. if (root.prefix === api) {
  119. root.prefix = save;
  120. }
  121. return api;
  122. };
  123. }
  124. return api;
  125. }));