From 2329718c27411e49498befae23bedde4e5e0f40f Mon Sep 17 00:00:00 2001 From: "Marlapati Venkata Naga Sai Teja[marlapativ]" Date: Sat, 20 Jul 2024 17:05:04 +0000 Subject: [PATCH] feat(logger): introduce loglevel constant --- packages/logger/src/constants.ts | 11 +++++++++- packages/logger/src/index.ts | 1 + packages/logger/src/types/Log.ts | 22 +++----------------- packages/logger/tests/unit/Logger.test.ts | 25 +++++++++++++---------- 4 files changed, 28 insertions(+), 31 deletions(-) diff --git a/packages/logger/src/constants.ts b/packages/logger/src/constants.ts index 90a26baa24..5fbe2be5be 100644 --- a/packages/logger/src/constants.ts +++ b/packages/logger/src/constants.ts @@ -14,4 +14,13 @@ const LogJsonIndent = { COMPACT: 0, } as const; -export { LogJsonIndent }; +const LogLevel = { + DEBUG: 'DEBUG', + INFO: 'INFO', + WARN: 'WARN', + ERROR: 'ERROR', + SILENT: 'SILENT', + CRITICAL: 'CRITICAL', +} as const; + +export { LogJsonIndent, LogLevel }; diff --git a/packages/logger/src/index.ts b/packages/logger/src/index.ts index 1fe46bcf9b..c063afb43d 100644 --- a/packages/logger/src/index.ts +++ b/packages/logger/src/index.ts @@ -1,3 +1,4 @@ export { Logger } from './Logger.js'; export { LogFormatter } from './formatter/LogFormatter.js'; export { LogItem } from './formatter/LogItem.js'; +export { LogLevel } from './constants.js'; diff --git a/packages/logger/src/types/Log.ts b/packages/logger/src/types/Log.ts index e415ee2544..e1ace4ef15 100644 --- a/packages/logger/src/types/Log.ts +++ b/packages/logger/src/types/Log.ts @@ -1,27 +1,11 @@ import type { EnvironmentVariablesService } from '../config/EnvironmentVariablesService.js'; import type { LogItem } from '../formatter/LogItem.js'; import type { UnformattedAttributes } from './Logger.js'; - -type LogLevelDebug = 'DEBUG'; -type LogLevelInfo = 'INFO'; -type LogLevelWarn = 'WARN'; -type LogLevelError = 'ERROR'; -type LogLevelSilent = 'SILENT'; -type LogLevelCritical = 'CRITICAL'; +import { LogLevel } from '../constants.js'; type LogLevel = - | LogLevelDebug - | Lowercase - | LogLevelInfo - | Lowercase - | LogLevelWarn - | Lowercase - | LogLevelError - | Lowercase - | LogLevelSilent - | Lowercase - | LogLevelCritical - | Lowercase; + | (typeof LogLevel)[keyof typeof LogLevel] + | Lowercase<(typeof LogLevel)[keyof typeof LogLevel]>; type LogLevelThresholds = { [key in Uppercase]: number; diff --git a/packages/logger/tests/unit/Logger.test.ts b/packages/logger/tests/unit/Logger.test.ts index 1cb720e9db..db12665f08 100644 --- a/packages/logger/tests/unit/Logger.test.ts +++ b/packages/logger/tests/unit/Logger.test.ts @@ -5,11 +5,14 @@ */ import context from '@aws-lambda-powertools/testing-utils/context'; import type { LambdaInterface } from '@aws-lambda-powertools/commons/types'; -import { Logger, LogFormatter } from '../../src/index.js'; +import { Logger, LogFormatter, LogLevel } from '../../src/index.js'; import { ConfigServiceInterface } from '../../src/types/ConfigServiceInterface.js'; import { EnvironmentVariablesService } from '../../src/config/EnvironmentVariablesService.js'; import { PowertoolsLogFormatter } from '../../src/formatter/PowertoolsLogFormatter.js'; -import { LogLevelThresholds, LogLevel } from '../../src/types/Log.js'; +import { + LogLevelThresholds, + type LogLevel as LogLevelType, +} from '../../src/types/Log.js'; import { type LogFunction, type ConstructorOptions, @@ -598,7 +601,7 @@ describe('Class: Logger', () => { test(`when the level is DEBUG, it ${debugAction} print to stdout`, () => { // Prepare const logger = new Logger({ - logLevel: 'DEBUG', + logLevel: LogLevel.DEBUG, }); const consoleSpy = jest.spyOn( logger['console'], @@ -656,7 +659,7 @@ describe('Class: Logger', () => { test(`when the log level is WARN, it ${warnAction} print to stdout`, () => { // Prepare const logger = new Logger({ - logLevel: 'WARN', + logLevel: LogLevel.WARN, }); const consoleSpy = jest.spyOn( logger['console'], @@ -714,7 +717,7 @@ describe('Class: Logger', () => { test('when the log level is SILENT, it DOES NOT print to stdout', () => { // Prepare const logger = new Logger({ - logLevel: 'SILENT', + logLevel: LogLevel.SILENT, }); const consoleSpy = jest.spyOn( logger['console'], @@ -2347,7 +2350,7 @@ describe('Class: Logger', () => { test('when logEvent is enabled, it logs the event in the first log', async () => { // Prepare const logger = new Logger({ - logLevel: 'DEBUG', + logLevel: LogLevel.DEBUG, }); const consoleSpy = jest.spyOn(logger['console'], 'info'); class LambdaFunction implements LambdaInterface { @@ -3141,7 +3144,7 @@ describe('Class: Logger', () => { const logger = new Logger(); // Act - logger.setLogLevel('ERROR'); + logger.setLogLevel(LogLevel.ERROR); // Assess expect(logger.level).toBe(20); @@ -3153,7 +3156,7 @@ describe('Class: Logger', () => { const logger = new Logger(); // Act & Assess - expect(() => logger.setLogLevel('INVALID' as LogLevel)).toThrow( + expect(() => logger.setLogLevel('INVALID' as LogLevelType)).toThrow( 'Invalid log level: INVALID' ); }); @@ -3240,7 +3243,7 @@ describe('Class: Logger', () => { process.env.POWERTOOLS_LOGGER_SAMPLE_RATE = '1'; const logger: Logger = new Logger({ - logLevel: 'ERROR', + logLevel: LogLevel.ERROR, }); // Assess @@ -3396,7 +3399,7 @@ describe('Class: Logger', () => { test('when sample rate in constructor is out of expected range, it should be ignored', () => { // Prepare const logger: Logger = new Logger({ - logLevel: 'INFO', + logLevel: LogLevel.INFO, sampleRateValue: 42, }); const consoleSpy = jest.spyOn(logger['console'], 'info'); @@ -3498,7 +3501,7 @@ describe('Class: Logger', () => { test('when sample rate calculation is refreshed, it DOES NOT overwrite the sample rate value', () => { // Prepare const logger = new Logger({ - logLevel: 'INFO', + logLevel: LogLevel.INFO, sampleRateValue: 1, }); const consoleSpy = jest.spyOn(logger['console'], 'info');