From 3584db15df0b725d2a357c67554e8faf310cd75a Mon Sep 17 00:00:00 2001 From: Sergei Cherniaev Date: Thu, 3 Nov 2022 18:08:33 +0400 Subject: [PATCH 01/13] test(logger): add tests for POWERTOOLS_DEV env var and for pretty printing logs when var is set to truthy value --- packages/logger/tests/unit/Logger.test.ts | 47 +++++++- .../EnvironmentVariablesService.test.ts | 108 ++++++++++++++++++ 2 files changed, 154 insertions(+), 1 deletion(-) diff --git a/packages/logger/tests/unit/Logger.test.ts b/packages/logger/tests/unit/Logger.test.ts index 17b7969230..61a577c96c 100644 --- a/packages/logger/tests/unit/Logger.test.ts +++ b/packages/logger/tests/unit/Logger.test.ts @@ -11,7 +11,7 @@ import * as dummyEvent from '../../../../tests/resources/events/custom/hello-wor import { createLogger, Logger } from '../../src'; import { EnvironmentVariablesService } from '../../src/config'; import { PowertoolLogFormatter } from '../../src/formatter'; -import { ClassThatLogs } from '../../src/types'; +import { ClassThatLogs, LogJsonIndent } from '../../src/types'; import { Context, Handler } from 'aws-lambda'; import { Console } from 'console'; @@ -1411,4 +1411,49 @@ describe('Class: Logger', () => { }); }); + describe('Feature: Pretty indentation for a local or non-production environment', () => { + + test('when the `POWERTOOLS_DEV` env var is SET it makes log output has multiple lines', () => { + + // Prepare + process.env.POWERTOOLS_DEV = 'true'; + const INDENTATION = LogJsonIndent.PRETTY; + const logger = new Logger(); + const consoleSpy = jest.spyOn(logger['console'], 'info').mockImplementation(); + + // Act + logger.info('Message with pretty identation'); + + // Assess + expect(consoleSpy).toBeCalledTimes(1); + expect(consoleSpy).toHaveBeenNthCalledWith(1, JSON.stringify({ + level: 'INFO', + message: 'Message with pretty identation', + service: 'hello-world', + timestamp: '2016-06-20T12:08:10.000Z', + xray_trace_id: '1-5759e988-bd862e3fe1be46a994272793', + }, null, INDENTATION)); + }); + + test('when the `POWERTOOLS_DEV` env var is NOT SET it makes log output as one-liner', () => { + + // Prepare + const logger = new Logger(); + const consoleSpy = jest.spyOn(logger['console'], 'info').mockImplementation(); + + // Act + logger.info('Message without pretty identation'); + + // Assess + expect(consoleSpy).toBeCalledTimes(1); + expect(consoleSpy).toHaveBeenNthCalledWith(1, JSON.stringify({ + level: 'INFO', + message: 'Message without pretty identation', + service: 'hello-world', + timestamp: '2016-06-20T12:08:10.000Z', + xray_trace_id: '1-5759e988-bd862e3fe1be46a994272793', + })); + }); + }); + }); \ No newline at end of file diff --git a/packages/logger/tests/unit/config/EnvironmentVariablesService.test.ts b/packages/logger/tests/unit/config/EnvironmentVariablesService.test.ts index b64c4c228f..e17ba205ca 100644 --- a/packages/logger/tests/unit/config/EnvironmentVariablesService.test.ts +++ b/packages/logger/tests/unit/config/EnvironmentVariablesService.test.ts @@ -220,4 +220,112 @@ describe('Class: EnvironmentVariablesService', () => { }); + describe('Method: getDevMode', () => { + + test('It returns true if the environment variable POWERTOOLS_DEV is "true"', () => { + + // Prepare + process.env.POWERTOOLS_DEV = 'true'; + const service = new EnvironmentVariablesService(); + + // Act + const value = service.getDevMode(); + + // Assess + expect(value).toEqual(true); + }); + + test('It returns true if the environment variable POWERTOOLS_DEV is "TRUE"', () => { + + // Prepare + process.env.POWERTOOLS_DEV = 'TRUE'; + const service = new EnvironmentVariablesService(); + + // Act + const value = service.getDevMode(); + + // Assess + expect(value).toEqual(true); + }); + + test('It returns true if the environment variable POWERTOOLS_DEV is "1"', () => { + + // Prepare + process.env.POWERTOOLS_DEV = '1'; + const service = new EnvironmentVariablesService(); + + // Act + const value = service.getDevMode(); + + // Assess + expect(value).toEqual(true); + }); + + test('It returns false if the environment variable POWERTOOLS_DEV is "false"', () => { + + // Prepare + process.env.POWERTOOLS_DEV = 'false'; + const service = new EnvironmentVariablesService(); + + // Act + const value = service.getDevMode(); + + // Assess + expect(value).toEqual(false); + }); + + test('It returns false if the environment variable POWERTOOLS_DEV is "FALSE"', () => { + + // Prepare + process.env.POWERTOOLS_DEV = 'FALSE'; + const service = new EnvironmentVariablesService(); + + // Act + const value = service.getDevMode(); + + // Assess + expect(value).toEqual(false); + }); + + test('It returns false if the environment variable POWERTOOLS_DEV is "0"', () => { + + // Prepare + process.env.POWERTOOLS_DEV = '0'; + const service = new EnvironmentVariablesService(); + + // Act + const value = service.getDevMode(); + + // Assess + expect(value).toEqual(false); + }); + + test('It returns false if the environment variable POWERTOOLS_DEV is NOT set', () => { + + // Prepare + process.env.POWERTOOLS_DEV = 'somethingsilly'; + const service = new EnvironmentVariablesService(); + + // Act + const value = service.getDevMode(); + + // Assess + expect(value).toEqual(false); + }); + + test('It returns false if the environment variable POWERTOOLS_DEV is "somethingsilly"', () => { + + // Prepare + process.env.POWERTOOLS_DEV = 'somethingsilly'; + const service = new EnvironmentVariablesService(); + + // Act + const value = service.getDevMode(); + + // Assess + expect(value).toEqual(false); + }); + + }); + }); \ No newline at end of file From 29a244473a7c72d8138d306ca3e8acfb8e78a3c3 Mon Sep 17 00:00:00 2001 From: Sergei Cherniaev Date: Thu, 3 Nov 2022 18:10:31 +0400 Subject: [PATCH 02/13] feat(logger): add POWERTOOLS_DEV env var and method to get its value --- packages/logger/src/config/ConfigServiceInterface.ts | 7 +++++++ .../logger/src/config/EnvironmentVariablesService.ts | 12 ++++++++++++ 2 files changed, 19 insertions(+) diff --git a/packages/logger/src/config/ConfigServiceInterface.ts b/packages/logger/src/config/ConfigServiceInterface.ts index 1f1a6ecc32..c8633756bb 100644 --- a/packages/logger/src/config/ConfigServiceInterface.ts +++ b/packages/logger/src/config/ConfigServiceInterface.ts @@ -22,6 +22,13 @@ interface ConfigServiceInterface { */ getCurrentEnvironment(): string + /** + * It returns the value of the POWERTOOLS_DEV environment variable. + * + * @returns {boolean} + */ + getDevMode(): boolean + /** * It returns the value of the POWERTOOLS_LOGGER_LOG_EVENT environment variable. * diff --git a/packages/logger/src/config/EnvironmentVariablesService.ts b/packages/logger/src/config/EnvironmentVariablesService.ts index 28b5ee7b4b..8879e9b8f9 100644 --- a/packages/logger/src/config/EnvironmentVariablesService.ts +++ b/packages/logger/src/config/EnvironmentVariablesService.ts @@ -20,6 +20,7 @@ class EnvironmentVariablesService extends CommonEnvironmentVariablesService impl // Reserved environment variables private awsRegionVariable = 'AWS_REGION'; private currentEnvironmentVariable = 'ENVIRONMENT'; + private devModeVariable = 'POWERTOOLS_DEV'; private functionNameVariable = 'AWS_LAMBDA_FUNCTION_NAME'; private functionVersionVariable = 'AWS_LAMBDA_FUNCTION_VERSION'; private logEventVariable = 'POWERTOOLS_LOGGER_LOG_EVENT'; @@ -45,6 +46,17 @@ class EnvironmentVariablesService extends CommonEnvironmentVariablesService impl return this.get(this.currentEnvironmentVariable); } + /** + * It returns the value of the POWERTOOLS_DEV environment variable. + * + * @returns {boolean} + */ + public getDevMode(): boolean { + const value = this.get(this.devModeVariable); + + return value.toLowerCase() === 'true' || value === '1'; + } + /** * It returns the value of the AWS_LAMBDA_FUNCTION_MEMORY_SIZE environment variable. * From cb2dc62b9445d4d8d698d7da055039d545f50f8d Mon Sep 17 00:00:00 2001 From: Sergei Cherniaev Date: Thu, 3 Nov 2022 18:12:09 +0400 Subject: [PATCH 03/13] test(logger): fix tests adding method that gets value of POWERTOOLS_DEV env var --- packages/logger/tests/unit/helpers.test.ts | 5 ++++- packages/logger/tests/unit/middleware/middy.test.ts | 3 +++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/packages/logger/tests/unit/helpers.test.ts b/packages/logger/tests/unit/helpers.test.ts index 9309bea207..6d90f606ec 100644 --- a/packages/logger/tests/unit/helpers.test.ts +++ b/packages/logger/tests/unit/helpers.test.ts @@ -307,7 +307,10 @@ describe('Helper: createLogger function', () => { getServiceName(): string { return 'my-backend-service'; }, - + getDevMode(): boolean { + return false; + }, + }; // Prepare const loggerOptions:ConstructorOptions = { diff --git a/packages/logger/tests/unit/middleware/middy.test.ts b/packages/logger/tests/unit/middleware/middy.test.ts index e734e61e61..6b545e4841 100644 --- a/packages/logger/tests/unit/middleware/middy.test.ts +++ b/packages/logger/tests/unit/middleware/middy.test.ts @@ -336,6 +336,9 @@ describe('Middy middleware', () => { getServiceName(): string { return 'my-backend-service'; }, + getDevMode(): boolean { + return false; + }, }; From 19e8dacddf570a0fd953dea01aab4a9bcad4b92c Mon Sep 17 00:00:00 2001 From: Sergei Cherniaev Date: Thu, 3 Nov 2022 18:13:21 +0400 Subject: [PATCH 04/13] feat(logger): add pretty printing to logs if POWERTOOLS_DEV env var set to truthy value --- packages/logger/src/Logger.ts | 6 +++++- packages/logger/src/types/Logger.ts | 5 +++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/packages/logger/src/Logger.ts b/packages/logger/src/Logger.ts index be9ee86253..903fa28d19 100644 --- a/packages/logger/src/Logger.ts +++ b/packages/logger/src/Logger.ts @@ -6,6 +6,7 @@ import { LogItem } from './log'; import cloneDeep from 'lodash.clonedeep'; import merge from 'lodash.merge'; import { ConfigServiceInterface, EnvironmentVariablesService } from './config'; +import { LogJsonIndent } from './types'; import type { ClassThatLogs, Environment, @@ -569,7 +570,10 @@ class Logger extends Utility implements ClassThatLogs { const consoleMethod = logLevel.toLowerCase() as keyof ClassThatLogs; - this.console[consoleMethod](JSON.stringify(log.getAttributes(), this.removeCircularDependencies())); + const isDevMode = this.getEnvVarsService().getDevMode(); + const INDENTATION: LogJsonIndent = isDevMode ? LogJsonIndent.PRETTY : LogJsonIndent.COMPACT; + + this.console[consoleMethod](JSON.stringify(log.getAttributes(), this.removeCircularDependencies(), INDENTATION)); } /** diff --git a/packages/logger/src/types/Logger.ts b/packages/logger/src/types/Logger.ts index add56ee28a..2976277c3c 100644 --- a/packages/logger/src/types/Logger.ts +++ b/packages/logger/src/types/Logger.ts @@ -74,3 +74,8 @@ export { ConstructorOptions, HandlerOptions }; + +export const enum LogJsonIndent { + PRETTY = 4, + COMPACT = 0, +} From 7d30f4f114fb056d491b449aa338aeafe44cf647 Mon Sep 17 00:00:00 2001 From: Sergei Cherniaev Date: Fri, 4 Nov 2022 16:18:29 +0400 Subject: [PATCH 05/13] refactor(review): add private class property for indent size, move evaluation of this property to setOptions() --- packages/logger/src/Logger.ts | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/packages/logger/src/Logger.ts b/packages/logger/src/Logger.ts index 903fa28d19..5f6acf81d4 100644 --- a/packages/logger/src/Logger.ts +++ b/packages/logger/src/Logger.ts @@ -125,6 +125,8 @@ class Logger extends Utility implements ClassThatLogs { private logFormatter?: LogFormatterInterface; + private logIndentation: number = LogJsonIndent.COMPACT; + private logLevel?: LogLevel; private readonly logLevelThresholds: LogLevelThresholds = { @@ -570,10 +572,7 @@ class Logger extends Utility implements ClassThatLogs { const consoleMethod = logLevel.toLowerCase() as keyof ClassThatLogs; - const isDevMode = this.getEnvVarsService().getDevMode(); - const INDENTATION: LogJsonIndent = isDevMode ? LogJsonIndent.PRETTY : LogJsonIndent.COMPACT; - - this.console[consoleMethod](JSON.stringify(log.getAttributes(), this.removeCircularDependencies(), INDENTATION)); + this.console[consoleMethod](JSON.stringify(log.getAttributes(), this.removeCircularDependencies(), this.logIndentation)); } /** @@ -666,6 +665,19 @@ class Logger extends Utility implements ClassThatLogs { this.logFormatter = logFormatter || new PowertoolLogFormatter(); } + /** + * If the `POWERTOOLS_DEV' env variable is set, + * it adds JSON indentation for pretty printing logs. + * + * @private + * @returns {void} + */ + private setLogIndentation(): void { + if (this.getEnvVarsService().getDevMode()) { + this.logIndentation = LogJsonIndent.PRETTY; + } + } + /** * It sets the Logger's instance log level. * @@ -735,6 +747,7 @@ class Logger extends Utility implements ClassThatLogs { this.setLogFormatter(logFormatter); this.setPowertoolLogData(serviceName, environment); this.setLogEvent(); + this.setLogIndentation(); this.addPersistentLogAttributes(persistentLogAttributes); From c724fe32b8834be033b0180b2792825b78a5623c Mon Sep 17 00:00:00 2001 From: Sergei Cherniaev Date: Fri, 4 Nov 2022 16:22:18 +0400 Subject: [PATCH 06/13] test(logger): fix tests according to the changes in default logger config --- packages/logger/tests/unit/Logger.test.ts | 5 +++++ packages/logger/tests/unit/helpers.test.ts | 3 +++ 2 files changed, 8 insertions(+) diff --git a/packages/logger/tests/unit/Logger.test.ts b/packages/logger/tests/unit/Logger.test.ts index 61a577c96c..6fbf9a527a 100644 --- a/packages/logger/tests/unit/Logger.test.ts +++ b/packages/logger/tests/unit/Logger.test.ts @@ -573,6 +573,7 @@ describe('Class: Logger', () => { customConfigService: undefined, envVarsService: expect.any(EnvironmentVariablesService), logEvent: false, + logIndentation: 0, logFormatter: expect.any(PowertoolLogFormatter), logLevel: 'DEBUG', logLevelThresholds: { @@ -1271,6 +1272,7 @@ describe('Class: Logger', () => { customConfigService: undefined, envVarsService: expect.any(EnvironmentVariablesService), logEvent: false, + logIndentation: 0, logFormatter: expect.any(PowertoolLogFormatter), logLevel: 'DEBUG', logLevelThresholds: { @@ -1295,6 +1297,7 @@ describe('Class: Logger', () => { customConfigService: undefined, envVarsService: expect.any(EnvironmentVariablesService), logEvent: false, + logIndentation: 0, logFormatter: expect.any(PowertoolLogFormatter), logLevel: 'DEBUG', logLevelThresholds: { @@ -1321,6 +1324,7 @@ describe('Class: Logger', () => { customConfigService: undefined, envVarsService: expect.any(EnvironmentVariablesService), logEvent: false, + logIndentation: 0, logFormatter: expect.any(PowertoolLogFormatter), logLevel: 'DEBUG', logLevelThresholds: { @@ -1345,6 +1349,7 @@ describe('Class: Logger', () => { customConfigService: undefined, envVarsService: expect.any(EnvironmentVariablesService), logEvent: false, + logIndentation: 0, logFormatter: expect.any(PowertoolLogFormatter), logLevel: 'ERROR', logLevelThresholds: { diff --git a/packages/logger/tests/unit/helpers.test.ts b/packages/logger/tests/unit/helpers.test.ts index 6d90f606ec..c6f7ad4cae 100644 --- a/packages/logger/tests/unit/helpers.test.ts +++ b/packages/logger/tests/unit/helpers.test.ts @@ -77,6 +77,7 @@ describe('Helper: createLogger function', () => { customConfigService: expect.any(EnvironmentVariablesService), envVarsService: expect.any(EnvironmentVariablesService), logEvent: false, + logIndentation: 0, logFormatter: expect.any(PowertoolLogFormatter), logLevel: 'WARN', console: expect.any(Console), @@ -117,6 +118,7 @@ describe('Helper: createLogger function', () => { customConfigService: undefined, envVarsService: expect.any(EnvironmentVariablesService), logEvent: false, + logIndentation: 0, logFormatter: expect.any(PowertoolLogFormatter), logLevel: 'INFO', console: expect.any(Console), @@ -238,6 +240,7 @@ describe('Helper: createLogger function', () => { customConfigService: undefined, envVarsService: expect.any(EnvironmentVariablesService), logEvent: false, + logIndentation: 0, logFormatter: expect.any(PowertoolLogFormatter), logLevel: 'INFO', console: expect.any(Console), From c6eb7fe1eab22ad91a0e54e986ebec2082f91906 Mon Sep 17 00:00:00 2001 From: Sergei Cherniaev Date: Fri, 4 Nov 2022 17:11:56 +0400 Subject: [PATCH 07/13] docs(logger): update sections for pretty printing logs using POWERTOOLS_DEV env var --- docs/core/logger.md | 3 +++ docs/index.md | 1 + 2 files changed, 4 insertions(+) diff --git a/docs/core/logger.md b/docs/core/logger.md index ab0bd44986..2969ea97f7 100644 --- a/docs/core/logger.md +++ b/docs/core/logger.md @@ -104,6 +104,9 @@ Your Logger will include the following keys to your structured logging (default | **xray_trace_id**: `string` | `1-5759e988-bd862e3fe1be46a994272793` | X-Ray Trace ID. This value is always presented in Lambda environment, whether [tracing is enabled](https://docs.aws.amazon.com/lambda/latest/dg/services-xray.html){target="_blank"} or not. Logger will always log this value. | | **error**: `Object` | `{ name: "Error", location: "/my-project/handler.ts:18", message: "Unexpected error #1", stack: "[stacktrace]"}` | Optional - An object containing information about the Error passed to the logger | +???+ info + When `POWERTOOLS_DEV` environment variable is present and set to `"true"`, Logger will pretty-print log messages for easier readability. It is for debugging purposes and is not supposed to use in production environment. + ### Capturing Lambda context info You can enrich your structured logs with key Lambda context information in multiple ways. diff --git a/docs/index.md b/docs/index.md index 88b35688a6..fcc1cdee24 100644 --- a/docs/index.md +++ b/docs/index.md @@ -293,6 +293,7 @@ Core utilities such as Tracing, Logging, and Metrics will be available across al | **POWERTOOLS_TRACER_CAPTURE_HTTPS_REQUESTS** | Captures HTTP(s) requests as segments. | [Tracer](./core/tracer) | `true` | | **POWERTOOLS_LOGGER_LOG_EVENT** | Logs incoming event | [Logger](./core/logger) | `false` | | **POWERTOOLS_LOGGER_SAMPLE_RATE** | Debug log sampling | [Logger](./core/logger) | `0` | +| **POWERTOOLS_DEV** | Increase JSON indentation to ease debugging when running functions locally or in a non-production environment | [Logger](./core/logger) | `0` | | **LOG_LEVEL** | Sets logging level | [Logger](./core/logger) | `INFO` | ## Tenets From 7475f740fcdd399554fd5b40ab1b68d9d534db69 Mon Sep 17 00:00:00 2001 From: Andrea Amorosi Date: Mon, 7 Nov 2022 12:27:28 +0100 Subject: [PATCH 08/13] Update docs/core/logger.md --- docs/core/logger.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/core/logger.md b/docs/core/logger.md index 2969ea97f7..f18f88aa7a 100644 --- a/docs/core/logger.md +++ b/docs/core/logger.md @@ -105,7 +105,7 @@ Your Logger will include the following keys to your structured logging (default | **error**: `Object` | `{ name: "Error", location: "/my-project/handler.ts:18", message: "Unexpected error #1", stack: "[stacktrace]"}` | Optional - An object containing information about the Error passed to the logger | ???+ info - When `POWERTOOLS_DEV` environment variable is present and set to `"true"`, Logger will pretty-print log messages for easier readability. It is for debugging purposes and is not supposed to use in production environment. + When `POWERTOOLS_DEV` environment variable is present and set to `"true"`, Logger will pretty-print log messages for easier readability. We recommend to use this setting only when debugging on local environments. ### Capturing Lambda context info From 9f239eb81893255b657f45f26a508d040303cb25 Mon Sep 17 00:00:00 2001 From: Sergei Cherniaev Date: Tue, 8 Nov 2022 09:03:43 +0300 Subject: [PATCH 09/13] Update docs/core/logger.md Co-authored-by: ijemmy --- docs/core/logger.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/core/logger.md b/docs/core/logger.md index f18f88aa7a..1153edfb3d 100644 --- a/docs/core/logger.md +++ b/docs/core/logger.md @@ -105,7 +105,7 @@ Your Logger will include the following keys to your structured logging (default | **error**: `Object` | `{ name: "Error", location: "/my-project/handler.ts:18", message: "Unexpected error #1", stack: "[stacktrace]"}` | Optional - An object containing information about the Error passed to the logger | ???+ info - When `POWERTOOLS_DEV` environment variable is present and set to `"true"`, Logger will pretty-print log messages for easier readability. We recommend to use this setting only when debugging on local environments. + When `POWERTOOLS_DEV` environment variable is present and set to `"true"` or `"1"`, Logger will pretty-print log messages for easier readability. We recommend to use this setting only when debugging on local environments. ### Capturing Lambda context info From 1c2ef89a820255ae7188e226916274a59c9379a5 Mon Sep 17 00:00:00 2001 From: Sergei Cherniaev Date: Wed, 9 Nov 2022 18:54:50 +0400 Subject: [PATCH 10/13] fix(docs): update default value for POWERTOOLS_DEV env var --- docs/index.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/index.md b/docs/index.md index fcc1cdee24..1656fdfc85 100644 --- a/docs/index.md +++ b/docs/index.md @@ -293,7 +293,7 @@ Core utilities such as Tracing, Logging, and Metrics will be available across al | **POWERTOOLS_TRACER_CAPTURE_HTTPS_REQUESTS** | Captures HTTP(s) requests as segments. | [Tracer](./core/tracer) | `true` | | **POWERTOOLS_LOGGER_LOG_EVENT** | Logs incoming event | [Logger](./core/logger) | `false` | | **POWERTOOLS_LOGGER_SAMPLE_RATE** | Debug log sampling | [Logger](./core/logger) | `0` | -| **POWERTOOLS_DEV** | Increase JSON indentation to ease debugging when running functions locally or in a non-production environment | [Logger](./core/logger) | `0` | +| **POWERTOOLS_DEV** | Increase JSON indentation to ease debugging when running functions locally or in a non-production environment | [Logger](./core/logger) | `false` | | **LOG_LEVEL** | Sets logging level | [Logger](./core/logger) | `INFO` | ## Tenets From 284eaffe0c8aecb62d1dc1dbb66a0bcdcceb68f1 Mon Sep 17 00:00:00 2001 From: Sergei Cherniaev Date: Thu, 10 Nov 2022 13:41:54 +0400 Subject: [PATCH 11/13] feat(logger): add isValueTrue method that checks the string value for truthiness --- .../src/config/ConfigServiceInterface.ts | 8 ++++++++ .../src/config/EnvironmentVariablesService.ts | 18 +++++++++++++++--- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/packages/logger/src/config/ConfigServiceInterface.ts b/packages/logger/src/config/ConfigServiceInterface.ts index c8633756bb..43f25d3649 100644 --- a/packages/logger/src/config/ConfigServiceInterface.ts +++ b/packages/logger/src/config/ConfigServiceInterface.ts @@ -57,6 +57,14 @@ interface ConfigServiceInterface { */ getServiceName(): string + /** + * It returns true if the string value represents a boolean true value. + * + * @param {string} value + * @returns boolean + */ + isValueTrue(value: string): boolean + } export { diff --git a/packages/logger/src/config/EnvironmentVariablesService.ts b/packages/logger/src/config/EnvironmentVariablesService.ts index 8879e9b8f9..fab7ec9fc8 100644 --- a/packages/logger/src/config/EnvironmentVariablesService.ts +++ b/packages/logger/src/config/EnvironmentVariablesService.ts @@ -54,7 +54,7 @@ class EnvironmentVariablesService extends CommonEnvironmentVariablesService impl public getDevMode(): boolean { const value = this.get(this.devModeVariable); - return value.toLowerCase() === 'true' || value === '1'; + return this.isValueTrue(value); } /** @@ -93,8 +93,8 @@ class EnvironmentVariablesService extends CommonEnvironmentVariablesService impl */ public getLogEvent(): boolean { const value = this.get(this.logEventVariable); - - return value.toLowerCase() === 'true' || value === '1'; + + return this.isValueTrue(value); } /** @@ -117,6 +117,18 @@ class EnvironmentVariablesService extends CommonEnvironmentVariablesService impl return (value && value.length > 0) ? Number(value) : undefined; } + /** + * It returns true if the string value represents a boolean true value. + * + * @param {string} value + * @returns boolean + */ + public isValueTrue(value: string): boolean { + const truthyValues: string[] = [ '1', 'y', 'yes', 't', 'true', 'on' ]; + + return truthyValues.includes(value.toLowerCase()); + } + } export { From dab7a0056f1c3f3793e8dcf5a8f5a781e1ea40ac Mon Sep 17 00:00:00 2001 From: Sergei Cherniaev Date: Thu, 10 Nov 2022 13:43:11 +0400 Subject: [PATCH 12/13] test(logger): adjust tests for config interface --- packages/logger/tests/unit/helpers.test.ts | 3 +++ packages/logger/tests/unit/middleware/middy.test.ts | 4 +++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/packages/logger/tests/unit/helpers.test.ts b/packages/logger/tests/unit/helpers.test.ts index c6f7ad4cae..ab49edd114 100644 --- a/packages/logger/tests/unit/helpers.test.ts +++ b/packages/logger/tests/unit/helpers.test.ts @@ -313,6 +313,9 @@ describe('Helper: createLogger function', () => { getDevMode(): boolean { return false; }, + isValueTrue(): boolean { + return true; + }, }; // Prepare diff --git a/packages/logger/tests/unit/middleware/middy.test.ts b/packages/logger/tests/unit/middleware/middy.test.ts index 6b545e4841..88e37b9fce 100644 --- a/packages/logger/tests/unit/middleware/middy.test.ts +++ b/packages/logger/tests/unit/middleware/middy.test.ts @@ -339,7 +339,9 @@ describe('Middy middleware', () => { getDevMode(): boolean { return false; }, - + isValueTrue(): boolean { + return true; + }, }; const logger = new Logger({ From 5a089bc8173ba9feb884ab082396489d3e34efb5 Mon Sep 17 00:00:00 2001 From: Sergei Cherniaev Date: Thu, 10 Nov 2022 16:20:23 +0400 Subject: [PATCH 13/13] test(logger): add tests for isValueTrue method, remove redundunt tests --- .../EnvironmentVariablesService.test.ts | 112 ++++-------------- 1 file changed, 24 insertions(+), 88 deletions(-) diff --git a/packages/logger/tests/unit/config/EnvironmentVariablesService.test.ts b/packages/logger/tests/unit/config/EnvironmentVariablesService.test.ts index e17ba205ca..ccaf0084bf 100644 --- a/packages/logger/tests/unit/config/EnvironmentVariablesService.test.ts +++ b/packages/logger/tests/unit/config/EnvironmentVariablesService.test.ts @@ -119,32 +119,6 @@ describe('Class: EnvironmentVariablesService', () => { expect(value).toEqual(true); }); - test('It returns true if the environment variable POWERTOOLS_LOGGER_LOG_EVENT is "TRUE"', () => { - - // Prepare - process.env.POWERTOOLS_LOGGER_LOG_EVENT = 'TRUE'; - const service = new EnvironmentVariablesService(); - - // Act - const value = service.getLogEvent(); - - // Assess - expect(value).toEqual(true); - }); - - test('It returns true if the environment variable POWERTOOLS_LOGGER_LOG_EVENT is "1"', () => { - - // Prepare - process.env.POWERTOOLS_LOGGER_LOG_EVENT = '1'; - const service = new EnvironmentVariablesService(); - - // Act - const value = service.getLogEvent(); - - // Assess - expect(value).toEqual(true); - }); - test('It returns false if the environment variable POWERTOOLS_LOGGER_LOG_EVENT is "false"', () => { // Prepare @@ -158,19 +132,6 @@ describe('Class: EnvironmentVariablesService', () => { expect(value).toEqual(false); }); - test('It returns false if the environment variable POWERTOOLS_LOGGER_LOG_EVENT is "0"', () => { - - // Prepare - process.env.POWERTOOLS_LOGGER_LOG_EVENT = '0'; - const service = new EnvironmentVariablesService(); - - // Act - const value = service.getLogEvent(); - - // Assess - expect(value).toEqual(false); - }); - test('It returns false if the environment variable POWERTOOLS_LOGGER_LOG_EVENT is "somethingsilly"', () => { // Prepare @@ -235,32 +196,6 @@ describe('Class: EnvironmentVariablesService', () => { expect(value).toEqual(true); }); - test('It returns true if the environment variable POWERTOOLS_DEV is "TRUE"', () => { - - // Prepare - process.env.POWERTOOLS_DEV = 'TRUE'; - const service = new EnvironmentVariablesService(); - - // Act - const value = service.getDevMode(); - - // Assess - expect(value).toEqual(true); - }); - - test('It returns true if the environment variable POWERTOOLS_DEV is "1"', () => { - - // Prepare - process.env.POWERTOOLS_DEV = '1'; - const service = new EnvironmentVariablesService(); - - // Act - const value = service.getDevMode(); - - // Assess - expect(value).toEqual(true); - }); - test('It returns false if the environment variable POWERTOOLS_DEV is "false"', () => { // Prepare @@ -274,23 +209,10 @@ describe('Class: EnvironmentVariablesService', () => { expect(value).toEqual(false); }); - test('It returns false if the environment variable POWERTOOLS_DEV is "FALSE"', () => { - - // Prepare - process.env.POWERTOOLS_DEV = 'FALSE'; - const service = new EnvironmentVariablesService(); - - // Act - const value = service.getDevMode(); - - // Assess - expect(value).toEqual(false); - }); - - test('It returns false if the environment variable POWERTOOLS_DEV is "0"', () => { + test('It returns false if the environment variable POWERTOOLS_DEV is NOT set', () => { // Prepare - process.env.POWERTOOLS_DEV = '0'; + process.env.POWERTOOLS_DEV = 'somethingsilly'; const service = new EnvironmentVariablesService(); // Act @@ -300,7 +222,7 @@ describe('Class: EnvironmentVariablesService', () => { expect(value).toEqual(false); }); - test('It returns false if the environment variable POWERTOOLS_DEV is NOT set', () => { + test('It returns false if the environment variable POWERTOOLS_DEV is "somethingsilly"', () => { // Prepare process.env.POWERTOOLS_DEV = 'somethingsilly'; @@ -313,19 +235,33 @@ describe('Class: EnvironmentVariablesService', () => { expect(value).toEqual(false); }); - test('It returns false if the environment variable POWERTOOLS_DEV is "somethingsilly"', () => { + }); + describe('Method: isValueTrue', () => { + + const valuesToTest: Array> = [ + [ '1', true ], + [ 'y', true ], + [ 'yes', true ], + [ 't', true ], + [ 'TRUE', true ], + [ 'on', true ], + [ '', false ], + [ 'false', false ], + [ 'fasle', false ], + [ 'somethingsilly', false ], + [ '0', false ] + ]; + + test.each(valuesToTest)('It takes string "%s" and returns %s', (input, output) => { // Prepare - process.env.POWERTOOLS_DEV = 'somethingsilly'; const service = new EnvironmentVariablesService(); - // Act - const value = service.getDevMode(); - + const value = service.isValueTrue(input as string); // Assess - expect(value).toEqual(false); + expect(value).toBe(output); }); - + }); }); \ No newline at end of file