log.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. describe('LogService', function() {
  2. const LOG_TYPES = ['debug', 'trace', 'info', 'warn', 'error'];
  3. let backup = {};
  4. let records = [];
  5. let $service;
  6. // Mock configuration
  7. const config = Object.assign({}, window.config);
  8. config.LOG_TAG_PADDING = 20;
  9. config.CONSOLE_LOG_LEVEL = 'info';
  10. config.REPORT_LOG_LEVEL = 'debug';
  11. config.REPORT_LOG_LIMIT = 10;
  12. // Ignoring page reload request
  13. beforeAll(() => window.onbeforeunload = () => null);
  14. beforeEach(function() {
  15. records = [];
  16. // Store each log level type method of console that we will override
  17. for (const type of LOG_TYPES) {
  18. backup[type] = console[type];
  19. }
  20. // Overwrite each log level type method of console
  21. for (const type of LOG_TYPES) {
  22. console[type] = (...args) => records.push(args);
  23. }
  24. // Angular magic
  25. module(($provide) => {
  26. $provide.constant('CONFIG', config);
  27. });
  28. module('3ema.services');
  29. // Inject the service
  30. inject(function(LogService) {
  31. $service = LogService;
  32. });
  33. });
  34. afterEach(() => {
  35. // Restore each log level type method of console that we have
  36. // previously overridden.
  37. for (const type of LOG_TYPES) {
  38. console[type] = backup[type];
  39. }
  40. });
  41. it('has correct root logger chain', () => {
  42. // With the above configuration:
  43. //
  44. // TeeLogger
  45. // v v
  46. // LevelLogger MemoryLogger
  47. // v
  48. // UnveilLogger
  49. // v
  50. // ConsoleLogger
  51. // Root logger
  52. expect($service.root.constructor.name).toBe('TeeLogger');
  53. let [left, right] = $service.root.loggers;
  54. // Console logger branch
  55. expect(left.constructor.name).toBe('LevelLogger');
  56. expect(left.level).toBe(config.CONSOLE_LOG_LEVEL);
  57. expect(left.logger.constructor.name).toBe('UnveilLogger');
  58. expect(left.logger.logger.constructor.name).toBe('ConsoleLogger');
  59. // Memory (report) logger branch
  60. expect(right.constructor.name).toBe('MemoryLogger');
  61. expect(right.limit).toBe(config.REPORT_LOG_LIMIT);
  62. });
  63. describe('getLogger', () => {
  64. it('log messages propagate to the root logger', () => {
  65. const logger = $service.getLogger('test');
  66. // Log
  67. logger.debug('debug');
  68. logger.trace('trace');
  69. logger.info('info');
  70. logger.warn('warn');
  71. logger.error('error');
  72. // Expect the console logger to have been called for 'info' and above
  73. expect(records.map((record) => record.slice(1))).toEqual([
  74. ['info'],
  75. ['warn'],
  76. ['error'],
  77. ]);
  78. // Expect the memory logger to have been called for 'debug' and above
  79. // (i.e. all log levels).
  80. expect(JSON
  81. .parse(JSON.stringify($service.memory.getRecords(), MemoryLogger.replacer))
  82. .map((record) => record.slice(1))
  83. ).toEqual([
  84. ['debug', '[test]', 'debug'],
  85. ['trace', '[test]', 'trace'],
  86. ['info', '[test]', 'info'],
  87. ['warn', '[test]', 'warn'],
  88. ['error', '[test]', 'error'],
  89. ]);
  90. });
  91. it('applies a tag (without style)', () => {
  92. const logger = $service.getLogger('test');
  93. logger.info('test');
  94. // Expect the console logger tag to be padded
  95. expect(records).toEqual([
  96. [' [test]', 'test']
  97. ]);
  98. // Expect the memory logger tag to be unpadded
  99. expect(JSON
  100. .parse(JSON.stringify($service.memory.getRecords(), MemoryLogger.replacer))
  101. .map((record) => record.slice(1))
  102. ).toEqual([
  103. ['info', '[test]', 'test']
  104. ]);
  105. });
  106. it('applies a tag (with style)', () => {
  107. const style = 'color: #fff';
  108. const logger = $service.getLogger('test', style);
  109. logger.info('test');
  110. // Expect the console logger tag to be padded and styled
  111. expect(records).toEqual([
  112. [' %c[test]', style, 'test']
  113. ]);
  114. // Expect the memory logger tag to be unpadded and unstyled
  115. expect(JSON
  116. .parse(JSON.stringify($service.memory.getRecords(), MemoryLogger.replacer))
  117. .map((record) => record.slice(1))
  118. ).toEqual([
  119. ['info', '%c[test]', style, 'test']
  120. ]);
  121. });
  122. it('applies the chosen level', () => {
  123. const logger = $service.getLogger('test', undefined, 'info');
  124. logger.debug('debug');
  125. logger.trace('trace');
  126. logger.info('info');
  127. // Expect the console logger to only contain the 'info' log
  128. expect(records.map((record) => record.slice(1))).toEqual([
  129. ['info']
  130. ]);
  131. // Expect the memory logger to only contain the 'info' log
  132. expect(JSON
  133. .parse(JSON.stringify($service.memory.getRecords(), MemoryLogger.replacer))
  134. .map((record) => record.slice(1))
  135. ).toEqual([
  136. ['info', '[test]', 'info']
  137. ]);
  138. });
  139. });
  140. });