|
| 1 | +/** |
| 2 | + * Copyright 2019, OpenTelemetry Authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import * as assert from 'assert'; |
| 18 | +import { ConsoleLogger } from '../../src/common/ConsoleLogger'; |
| 19 | +import { LogLevel } from '../../src/common/types'; |
| 20 | + |
| 21 | +describe('ConsoleLogger', () => { |
| 22 | + const origDebug = console.debug; |
| 23 | + const origInfo = console.info; |
| 24 | + const origWarn = console.warn; |
| 25 | + const origError = console.error; |
| 26 | + let debugCalledArgs: unknown; |
| 27 | + let infoCalledArgs: unknown; |
| 28 | + let warnCalledArgs: unknown; |
| 29 | + let errorCalledArgs: unknown; |
| 30 | + |
| 31 | + beforeEach(() => { |
| 32 | + // mock |
| 33 | + console.debug = (...args: unknown[]) => { |
| 34 | + debugCalledArgs = args; |
| 35 | + }; |
| 36 | + console.info = (...args: unknown[]) => { |
| 37 | + infoCalledArgs = args; |
| 38 | + }; |
| 39 | + console.warn = (...args: unknown[]) => { |
| 40 | + warnCalledArgs = args; |
| 41 | + }; |
| 42 | + console.error = (...args: unknown[]) => { |
| 43 | + errorCalledArgs = args; |
| 44 | + }; |
| 45 | + }); |
| 46 | + |
| 47 | + afterEach(() => { |
| 48 | + // restore |
| 49 | + debugCalledArgs = null; |
| 50 | + infoCalledArgs = null; |
| 51 | + warnCalledArgs = null; |
| 52 | + errorCalledArgs = null; |
| 53 | + console.debug = origDebug; |
| 54 | + console.info = origInfo; |
| 55 | + console.warn = origWarn; |
| 56 | + console.error = origError; |
| 57 | + }); |
| 58 | + |
| 59 | + describe('constructor', () => { |
| 60 | + it('should log with default level', () => { |
| 61 | + const consoleLogger = new ConsoleLogger(); |
| 62 | + consoleLogger.error('error called %s', 'param1'); |
| 63 | + assert.deepStrictEqual(errorCalledArgs, ['error called %s', 'param1']); |
| 64 | + consoleLogger.warn('warn called %s', 'param1'); |
| 65 | + assert.deepStrictEqual(warnCalledArgs, ['warn called %s', 'param1']); |
| 66 | + consoleLogger.info('info called %s', 'param1'); |
| 67 | + assert.deepStrictEqual(infoCalledArgs, ['info called %s', 'param1']); |
| 68 | + consoleLogger.debug('debug called %s', 'param1'); |
| 69 | + assert.deepStrictEqual(debugCalledArgs, ['debug called %s', 'param1']); |
| 70 | + }); |
| 71 | + |
| 72 | + it('should log with debug', () => { |
| 73 | + const consoleLogger = new ConsoleLogger(LogLevel.DEBUG); |
| 74 | + consoleLogger.error('error called'); |
| 75 | + assert.deepStrictEqual(errorCalledArgs, ['error called']); |
| 76 | + consoleLogger.warn('warn called %s', 'param1'); |
| 77 | + assert.deepStrictEqual(warnCalledArgs, ['warn called %s', 'param1']); |
| 78 | + consoleLogger.info('info called %s', 'param1'); |
| 79 | + assert.deepStrictEqual(infoCalledArgs, ['info called %s', 'param1']); |
| 80 | + consoleLogger.debug('debug called %s', 'param1'); |
| 81 | + assert.deepStrictEqual(debugCalledArgs, ['debug called %s', 'param1']); |
| 82 | + }); |
| 83 | + |
| 84 | + it('should log with info', () => { |
| 85 | + const consoleLogger = new ConsoleLogger(LogLevel.INFO); |
| 86 | + consoleLogger.error('error called %s', 'param1'); |
| 87 | + assert.deepStrictEqual(errorCalledArgs, ['error called %s', 'param1']); |
| 88 | + consoleLogger.warn('warn called %s', 'param1'); |
| 89 | + assert.deepStrictEqual(warnCalledArgs, ['warn called %s', 'param1']); |
| 90 | + consoleLogger.info('info called %s', 'param1'); |
| 91 | + assert.deepStrictEqual(infoCalledArgs, ['info called %s', 'param1']); |
| 92 | + consoleLogger.debug('debug called %s', 'param1'); |
| 93 | + assert.strictEqual(debugCalledArgs, null); |
| 94 | + }); |
| 95 | + |
| 96 | + it('should log with warn', () => { |
| 97 | + const consoleLogger = new ConsoleLogger(LogLevel.WARN); |
| 98 | + consoleLogger.error('error called %s', 'param1'); |
| 99 | + assert.deepStrictEqual(errorCalledArgs, ['error called %s', 'param1']); |
| 100 | + consoleLogger.warn('warn called %s', 'param1'); |
| 101 | + assert.deepStrictEqual(warnCalledArgs, ['warn called %s', 'param1']); |
| 102 | + consoleLogger.info('info called %s', 'param1'); |
| 103 | + assert.strictEqual(infoCalledArgs, null); |
| 104 | + consoleLogger.debug('debug called %s', 'param1'); |
| 105 | + assert.strictEqual(debugCalledArgs, null); |
| 106 | + }); |
| 107 | + |
| 108 | + it('should log with error', () => { |
| 109 | + const consoleLogger = new ConsoleLogger(LogLevel.ERROR); |
| 110 | + consoleLogger.error('error called %s', 'param1'); |
| 111 | + assert.deepStrictEqual(errorCalledArgs, ['error called %s', 'param1']); |
| 112 | + consoleLogger.warn('warn called %s', 'param1'); |
| 113 | + assert.strictEqual(warnCalledArgs, null); |
| 114 | + consoleLogger.info('info called %s', 'param1'); |
| 115 | + assert.strictEqual(infoCalledArgs, null); |
| 116 | + consoleLogger.debug('debug called %s', 'param1'); |
| 117 | + assert.strictEqual(debugCalledArgs, null); |
| 118 | + }); |
| 119 | + }); |
| 120 | +}); |
0 commit comments