|
| 1 | +/** |
| 2 | + * Copyright (c) 2025, WSO2 LLC. (https://www.wso2.com). |
| 3 | + * |
| 4 | + * WSO2 LLC. licenses this file to you under the Apache License, |
| 5 | + * Version 2.0 (the "License"); you may not use this file except |
| 6 | + * in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, |
| 12 | + * software distributed under the License is distributed on an |
| 13 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | + * KIND, either express or implied. See the License for the |
| 15 | + * specific language governing permissions and limitations |
| 16 | + * under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +import logger, {createLogger, createComponentLogger, LogLevel} from '../logger'; |
| 20 | + |
| 21 | +describe('Logger', () => { |
| 22 | + beforeEach(() => { |
| 23 | + // Reset console methods before each test |
| 24 | + jest.spyOn(console, 'log').mockImplementation(); |
| 25 | + jest.spyOn(console, 'info').mockImplementation(); |
| 26 | + jest.spyOn(console, 'warn').mockImplementation(); |
| 27 | + jest.spyOn(console, 'error').mockImplementation(); |
| 28 | + jest.spyOn(console, 'debug').mockImplementation(); |
| 29 | + }); |
| 30 | + |
| 31 | + afterEach(() => { |
| 32 | + jest.restoreAllMocks(); |
| 33 | + }); |
| 34 | + |
| 35 | + describe('Basic logging', () => { |
| 36 | + it('should log info messages', () => { |
| 37 | + logger.info('Test info message'); |
| 38 | + expect(console.info).toHaveBeenCalled(); |
| 39 | + }); |
| 40 | + |
| 41 | + it('should log warning messages', () => { |
| 42 | + logger.warn('Test warning message'); |
| 43 | + expect(console.warn).toHaveBeenCalled(); |
| 44 | + }); |
| 45 | + |
| 46 | + it('should log error messages', () => { |
| 47 | + logger.error('Test error message'); |
| 48 | + expect(console.error).toHaveBeenCalled(); |
| 49 | + }); |
| 50 | + |
| 51 | + it('should log debug messages when level is DEBUG', () => { |
| 52 | + logger.setLevel(LogLevel.DEBUG); |
| 53 | + logger.debug('Test debug message'); |
| 54 | + expect(console.debug).toHaveBeenCalled(); |
| 55 | + }); |
| 56 | + |
| 57 | + it('should not log debug messages when level is INFO', () => { |
| 58 | + logger.setLevel(LogLevel.INFO); |
| 59 | + logger.debug('Test debug message'); |
| 60 | + expect(console.debug).not.toHaveBeenCalled(); |
| 61 | + }); |
| 62 | + }); |
| 63 | + |
| 64 | + describe('Log levels', () => { |
| 65 | + it('should respect log level filtering', () => { |
| 66 | + logger.setLevel(LogLevel.WARN); |
| 67 | + |
| 68 | + logger.debug('Debug message'); |
| 69 | + logger.info('Info message'); |
| 70 | + logger.warn('Warning message'); |
| 71 | + logger.error('Error message'); |
| 72 | + |
| 73 | + expect(console.debug).not.toHaveBeenCalled(); |
| 74 | + expect(console.info).not.toHaveBeenCalled(); |
| 75 | + expect(console.warn).toHaveBeenCalled(); |
| 76 | + expect(console.error).toHaveBeenCalled(); |
| 77 | + }); |
| 78 | + |
| 79 | + it('should silence all logs when level is SILENT', () => { |
| 80 | + logger.setLevel(LogLevel.SILENT); |
| 81 | + |
| 82 | + logger.debug('Debug message'); |
| 83 | + logger.info('Info message'); |
| 84 | + logger.warn('Warning message'); |
| 85 | + logger.error('Error message'); |
| 86 | + |
| 87 | + expect(console.debug).not.toHaveBeenCalled(); |
| 88 | + expect(console.info).not.toHaveBeenCalled(); |
| 89 | + expect(console.warn).not.toHaveBeenCalled(); |
| 90 | + expect(console.error).not.toHaveBeenCalled(); |
| 91 | + }); |
| 92 | + }); |
| 93 | + |
| 94 | + describe('Custom loggers', () => { |
| 95 | + it('should create logger with custom configuration', () => { |
| 96 | + const customLogger = createLogger({ |
| 97 | + level: LogLevel.DEBUG, |
| 98 | + prefix: 'Custom', |
| 99 | + timestamps: false, |
| 100 | + showLevel: false, |
| 101 | + }); |
| 102 | + |
| 103 | + expect(customLogger.getLevel()).toBe(LogLevel.DEBUG); |
| 104 | + expect(customLogger.getConfig().prefix).toBe('Custom'); |
| 105 | + expect(customLogger.getConfig().timestamps).toBe(false); |
| 106 | + expect(customLogger.getConfig().showLevel).toBe(false); |
| 107 | + }); |
| 108 | + |
| 109 | + it('should create component logger with nested prefix', () => { |
| 110 | + const componentLogger = createComponentLogger('Authentication'); |
| 111 | + |
| 112 | + componentLogger.info('Test message'); |
| 113 | + |
| 114 | + expect(console.info).toHaveBeenCalled(); |
| 115 | + // The exact format depends on environment detection |
| 116 | + }); |
| 117 | + |
| 118 | + it('should create child logger', () => { |
| 119 | + const parentLogger = createLogger({prefix: 'Parent'}); |
| 120 | + const childLogger = parentLogger.child('Child'); |
| 121 | + |
| 122 | + expect(childLogger.getConfig().prefix).toBe('Parent - Child'); |
| 123 | + }); |
| 124 | + }); |
| 125 | + |
| 126 | + describe('Configuration', () => { |
| 127 | + it('should update configuration', () => { |
| 128 | + const testLogger = createLogger({level: LogLevel.INFO}); |
| 129 | + |
| 130 | + testLogger.configure({ |
| 131 | + level: LogLevel.DEBUG, |
| 132 | + prefix: 'Updated', |
| 133 | + }); |
| 134 | + |
| 135 | + expect(testLogger.getLevel()).toBe(LogLevel.DEBUG); |
| 136 | + expect(testLogger.getConfig().prefix).toBe('Updated'); |
| 137 | + }); |
| 138 | + }); |
| 139 | + |
| 140 | + describe('Custom formatter', () => { |
| 141 | + it('should use custom formatter when provided', () => { |
| 142 | + const mockFormatter = jest.fn(); |
| 143 | + const customLogger = createLogger({ |
| 144 | + formatter: mockFormatter, |
| 145 | + }); |
| 146 | + |
| 147 | + customLogger.info('Test message', {data: 'test'}); |
| 148 | + |
| 149 | + expect(mockFormatter).toHaveBeenCalledWith(LogLevel.INFO, 'Test message', {data: 'test'}); |
| 150 | + expect(console.info).not.toHaveBeenCalled(); |
| 151 | + }); |
| 152 | + }); |
| 153 | +}); |
0 commit comments