From 8c5d9c9510c341b89fa6602b3bcfde07a50345e0 Mon Sep 17 00:00:00 2001 From: erikayao93 Date: Thu, 15 Jun 2023 23:45:08 +0000 Subject: [PATCH 1/8] Updated formatAttributes for additional parameters and LogItem return type --- packages/logger/src/Logger.ts | 19 +++++----- packages/logger/src/formatter/LogFormatter.ts | 11 ++++-- .../src/formatter/LogFormatterInterface.ts | 11 ++++-- .../src/formatter/PowertoolLogFormatter.ts | 35 +++++++++++++++++-- packages/logger/src/log/LogItem.ts | 10 ++---- 5 files changed, 61 insertions(+), 25 deletions(-) diff --git a/packages/logger/src/Logger.ts b/packages/logger/src/Logger.ts index bb20e957e9..ec0aa9d978 100644 --- a/packages/logger/src/Logger.ts +++ b/packages/logger/src/Logger.ts @@ -617,16 +617,9 @@ class Logger extends Utility implements ClassThatLogs { this.getPowertoolLogData() ); - const logItem = new LogItem({ - baseAttributes: this.getLogFormatter().formatAttributes( - unformattedBaseAttributes - ), - persistentAttributes: this.getPersistentLogAttributes(), - }); - - // Add ephemeral attributes + let additionalLogAttributes: LogAttributes = {}; if (typeof input !== 'string') { - logItem.addAttributes(input); + additionalLogAttributes = merge(additionalLogAttributes, input); } extraInput.forEach((item: Error | LogAttributes | string) => { const attributes: LogAttributes = @@ -636,9 +629,15 @@ class Logger extends Utility implements ClassThatLogs { ? { extra: item } : item; - logItem.addAttributes(attributes); + additionalLogAttributes = merge(additionalLogAttributes, attributes); }); + const logItem = this.getLogFormatter().formatAttributes( + unformattedBaseAttributes, + this.getPersistentLogAttributes(), + additionalLogAttributes + ); + return logItem; } diff --git a/packages/logger/src/formatter/LogFormatter.ts b/packages/logger/src/formatter/LogFormatter.ts index dac142a05a..cd5db9659f 100644 --- a/packages/logger/src/formatter/LogFormatter.ts +++ b/packages/logger/src/formatter/LogFormatter.ts @@ -1,5 +1,6 @@ import { LogFormatterInterface } from '.'; import { LogAttributes, UnformattedAttributes } from '../types'; +import { LogItem } from '../log'; /** * This class defines and implements common methods for the formatting of log attributes. @@ -13,11 +14,15 @@ abstract class LogFormatter implements LogFormatterInterface { * It formats key-value pairs of log attributes. * * @param {UnformattedAttributes} attributes - * @returns {LogAttributes} + * @param {LogAttributes} persistentLogAttributes + * @param {LogAttributes} additionalLogAttributes + * @returns {LogItem} */ public abstract formatAttributes( - attributes: UnformattedAttributes - ): LogAttributes; + attributes: UnformattedAttributes, + persistentLogAttributes: LogAttributes, + additionalLogAttributes: LogAttributes + ): LogItem; /** * It formats a given Error parameter. diff --git a/packages/logger/src/formatter/LogFormatterInterface.ts b/packages/logger/src/formatter/LogFormatterInterface.ts index b6a771b84e..633e7c1fba 100644 --- a/packages/logger/src/formatter/LogFormatterInterface.ts +++ b/packages/logger/src/formatter/LogFormatterInterface.ts @@ -1,4 +1,5 @@ import { LogAttributes, UnformattedAttributes } from '../types'; +import { LogItem } from '../log'; /** * @interface @@ -8,9 +9,15 @@ interface LogFormatterInterface { * It formats key-value pairs of log attributes. * * @param {UnformattedAttributes} attributes - * @returns {PowertoolLog} + * @param {LogAttributes} persistentLogAttributes + * @param {LogAttributes} additionalLogAttributes + * @returns {LogItem} */ - formatAttributes(attributes: UnformattedAttributes): LogAttributes; + formatAttributes( + attributes: UnformattedAttributes, + persistentLogAttributes: LogAttributes, + additionalLogAttributes: LogAttributes + ): LogItem; /** * It formats a given Error parameter. diff --git a/packages/logger/src/formatter/PowertoolLogFormatter.ts b/packages/logger/src/formatter/PowertoolLogFormatter.ts index 0fa77928e2..c09ca7835e 100644 --- a/packages/logger/src/formatter/PowertoolLogFormatter.ts +++ b/packages/logger/src/formatter/PowertoolLogFormatter.ts @@ -1,6 +1,7 @@ import { LogFormatter } from '.'; -import { UnformattedAttributes } from '../types'; +import { LogAttributes, UnformattedAttributes } from '../types'; import { PowertoolLog } from '../types/formats'; +import { LogItem } from '../log'; /** * This class is used to transform a set of log key-value pairs @@ -10,12 +11,40 @@ import { PowertoolLog } from '../types/formats'; * @extends {LogFormatter} */ class PowertoolLogFormatter extends LogFormatter { + public formatAttributes( + attributes: UnformattedAttributes, + persistentLogAttributes: LogAttributes, + additionalLogAttributes: LogAttributes + ): LogItem { + const baseAttributes: PowertoolLog = { + // standard attributes from UnformattedAttributes + cold_start: attributes.lambdaContext?.coldStart, + function_arn: attributes.lambdaContext?.invokedFunctionArn, + function_memory_size: attributes.lambdaContext?.memoryLimitInMB, + function_name: attributes.lambdaContext?.functionName, + function_request_id: attributes.lambdaContext?.awsRequestId, + level: attributes.logLevel, + message: attributes.message, + sampling_rate: attributes.sampleRateValue, + service: attributes.serviceName, + timestamp: this.formatTimestamp(attributes.timestamp), + xray_trace_id: attributes.xRayTraceId, + }; + + const powertoolLogItem = new LogItem({ attributes: baseAttributes }); + + powertoolLogItem.addAttributes(persistentLogAttributes); + powertoolLogItem.addAttributes(additionalLogAttributes); + + return powertoolLogItem; + } + /** * It formats key-value pairs of log attributes. * * @param {UnformattedAttributes} attributes * @returns {PowertoolLog} - */ + public formatAttributes(attributes: UnformattedAttributes): PowertoolLog { return { cold_start: attributes.lambdaContext?.coldStart, @@ -30,7 +59,7 @@ class PowertoolLogFormatter extends LogFormatter { timestamp: this.formatTimestamp(attributes.timestamp), xray_trace_id: attributes.xRayTraceId, }; - } + }*/ } export { PowertoolLogFormatter }; diff --git a/packages/logger/src/log/LogItem.ts b/packages/logger/src/log/LogItem.ts index 1c562ec171..00a66ec786 100644 --- a/packages/logger/src/log/LogItem.ts +++ b/packages/logger/src/log/LogItem.ts @@ -5,16 +5,12 @@ import { LogAttributes } from '../types'; class LogItem implements LogItemInterface { private attributes: LogAttributes = {}; - public constructor(params: { - baseAttributes: LogAttributes; - persistentAttributes: LogAttributes; - }) { + public constructor(params: { attributes: LogAttributes }) { // Add attributes in the log item in this order: // - Base attributes supported by the Powertool by default - // - Persistent attributes provided by developer, not formatted + // - Persistent attributes provided by developer, not formatted (done later) // - Ephemeral attributes provided as parameters for a single log item (done later) - this.addAttributes(params.baseAttributes); - this.addAttributes(params.persistentAttributes); + this.addAttributes(params.attributes); } public addAttributes(attributes: LogAttributes): LogItem { From 9cbfd5480d6ec4aa0c5c930dbaa00150b36a2616 Mon Sep 17 00:00:00 2001 From: erikayao93 Date: Fri, 16 Jun 2023 18:29:44 +0000 Subject: [PATCH 2/8] Updated the unit tests to pass with new formatter --- .../formatter/PowertoolLogFormatter.test.ts | 22 ++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/packages/logger/tests/unit/formatter/PowertoolLogFormatter.test.ts b/packages/logger/tests/unit/formatter/PowertoolLogFormatter.test.ts index 49daebe597..f6a282ff3e 100644 --- a/packages/logger/tests/unit/formatter/PowertoolLogFormatter.test.ts +++ b/packages/logger/tests/unit/formatter/PowertoolLogFormatter.test.ts @@ -5,7 +5,7 @@ */ import { AssertionError, strictEqual } from 'assert'; import { PowertoolLogFormatter } from '../../../src/formatter'; -import { UnformattedAttributes } from '../../../src/types'; +import { LogAttributes, UnformattedAttributes } from '../../../src/types'; describe('Class: PowertoolLogFormatter', () => { const mockDate = new Date(1466424490000); @@ -29,12 +29,18 @@ describe('Class: PowertoolLogFormatter', () => { timestamp: new Date(), message: 'This is a WARN log', }; + const persistentLogAttributes: LogAttributes = {}; + const additionalLogAttributes: LogAttributes = {}; // Act - const value = formatter.formatAttributes(unformattedAttributes); + const value = formatter.formatAttributes( + unformattedAttributes, + persistentLogAttributes, + additionalLogAttributes + ); // Assess - expect(value).toEqual({ + expect(value.getAttributes()).toEqual({ cold_start: undefined, function_arn: undefined, function_memory_size: undefined, @@ -72,12 +78,18 @@ describe('Class: PowertoolLogFormatter', () => { awsRequestId: 'abcdefg123456789', }, }; + const persistentLogAttributes: LogAttributes = {}; + const additionalLogAttributes: LogAttributes = {}; // Act - const value = formatter.formatAttributes(unformattedAttributes); + const value = formatter.formatAttributes( + unformattedAttributes, + persistentLogAttributes, + additionalLogAttributes + ); // Assess - expect(value).toEqual({ + expect(value.getAttributes()).toEqual({ cold_start: true, function_arn: 'arn:aws:lambda:eu-west-1:123456789012:function:Example', function_memory_size: 123, From 30ad38749595914ad38bd2a7a4b9efcba161ede0 Mon Sep 17 00:00:00 2001 From: erikayao93 Date: Fri, 16 Jun 2023 18:44:00 +0000 Subject: [PATCH 3/8] Updated Powertool named objects to Powertools --- packages/logger/src/Logger.ts | 4 +- ...Formatter.ts => PowertoolsLogFormatter.ts} | 37 ++++++------------- packages/logger/src/formatter/index.ts | 2 +- .../{PowertoolLog.ts => PowertoolsLog.ts} | 4 +- packages/logger/src/types/formats/index.ts | 2 +- 5 files changed, 17 insertions(+), 32 deletions(-) rename packages/logger/src/formatter/{PowertoolLogFormatter.ts => PowertoolsLogFormatter.ts} (62%) rename packages/logger/src/types/formats/{PowertoolLog.ts => PowertoolsLog.ts} (96%) diff --git a/packages/logger/src/Logger.ts b/packages/logger/src/Logger.ts index ec0aa9d978..d7180b29b8 100644 --- a/packages/logger/src/Logger.ts +++ b/packages/logger/src/Logger.ts @@ -2,7 +2,7 @@ import { randomInt } from 'node:crypto'; import { Console } from 'node:console'; import type { Context, Handler } from 'aws-lambda'; import { Utility } from '@aws-lambda-powertools/commons'; -import { LogFormatterInterface, PowertoolLogFormatter } from './formatter'; +import { LogFormatterInterface, PowertoolsLogFormatter } from './formatter'; import { LogItem } from './log'; import merge from 'lodash.merge'; import { ConfigServiceInterface, EnvironmentVariablesService } from './config'; @@ -914,7 +914,7 @@ class Logger extends Utility implements ClassThatLogs { * @returns {void} */ private setLogFormatter(logFormatter?: LogFormatterInterface): void { - this.logFormatter = logFormatter || new PowertoolLogFormatter(); + this.logFormatter = logFormatter || new PowertoolsLogFormatter(); } /** diff --git a/packages/logger/src/formatter/PowertoolLogFormatter.ts b/packages/logger/src/formatter/PowertoolsLogFormatter.ts similarity index 62% rename from packages/logger/src/formatter/PowertoolLogFormatter.ts rename to packages/logger/src/formatter/PowertoolsLogFormatter.ts index c09ca7835e..33e0296d3d 100644 --- a/packages/logger/src/formatter/PowertoolLogFormatter.ts +++ b/packages/logger/src/formatter/PowertoolsLogFormatter.ts @@ -1,6 +1,6 @@ import { LogFormatter } from '.'; import { LogAttributes, UnformattedAttributes } from '../types'; -import { PowertoolLog } from '../types/formats'; +import { PowertoolsLog } from '../types/formats'; import { LogItem } from '../log'; /** @@ -10,13 +10,20 @@ import { LogItem } from '../log'; * @class * @extends {LogFormatter} */ -class PowertoolLogFormatter extends LogFormatter { +class PowertoolsLogFormatter extends LogFormatter { + /** + * It formats key-value pairs of log attributes. + * + * @param {UnformattedAttributes} attributes + * @param {LogAttributes} additionalLogAttributes + * @returns {PowertoolsLog} + */ public formatAttributes( attributes: UnformattedAttributes, persistentLogAttributes: LogAttributes, additionalLogAttributes: LogAttributes ): LogItem { - const baseAttributes: PowertoolLog = { + const baseAttributes: PowertoolsLog = { // standard attributes from UnformattedAttributes cold_start: attributes.lambdaContext?.coldStart, function_arn: attributes.lambdaContext?.invokedFunctionArn, @@ -38,28 +45,6 @@ class PowertoolLogFormatter extends LogFormatter { return powertoolLogItem; } - - /** - * It formats key-value pairs of log attributes. - * - * @param {UnformattedAttributes} attributes - * @returns {PowertoolLog} - - public formatAttributes(attributes: UnformattedAttributes): PowertoolLog { - return { - cold_start: attributes.lambdaContext?.coldStart, - function_arn: attributes.lambdaContext?.invokedFunctionArn, - function_memory_size: attributes.lambdaContext?.memoryLimitInMB, - function_name: attributes.lambdaContext?.functionName, - function_request_id: attributes.lambdaContext?.awsRequestId, - level: attributes.logLevel, - message: attributes.message, - sampling_rate: attributes.sampleRateValue, - service: attributes.serviceName, - timestamp: this.formatTimestamp(attributes.timestamp), - xray_trace_id: attributes.xRayTraceId, - }; - }*/ } -export { PowertoolLogFormatter }; +export { PowertoolsLogFormatter }; diff --git a/packages/logger/src/formatter/index.ts b/packages/logger/src/formatter/index.ts index 1f7f14d2ab..ef5d7b16d8 100644 --- a/packages/logger/src/formatter/index.ts +++ b/packages/logger/src/formatter/index.ts @@ -1,3 +1,3 @@ export * from './LogFormatter'; export * from './LogFormatterInterface'; -export * from './PowertoolLogFormatter'; +export * from './PowertoolsLogFormatter'; diff --git a/packages/logger/src/types/formats/PowertoolLog.ts b/packages/logger/src/types/formats/PowertoolsLog.ts similarity index 96% rename from packages/logger/src/types/formats/PowertoolLog.ts rename to packages/logger/src/types/formats/PowertoolsLog.ts index add42d298c..fa360fef59 100644 --- a/packages/logger/src/types/formats/PowertoolLog.ts +++ b/packages/logger/src/types/formats/PowertoolsLog.ts @@ -1,6 +1,6 @@ import type { LogAttributes, LogLevel } from '..'; -type PowertoolLog = LogAttributes & { +type PowertoolsLog = LogAttributes & { /** * timestamp * @@ -90,4 +90,4 @@ type PowertoolLog = LogAttributes & { lambda_request_id?: string; }; -export type { PowertoolLog }; +export type { PowertoolsLog }; diff --git a/packages/logger/src/types/formats/index.ts b/packages/logger/src/types/formats/index.ts index 5462610cd6..5a828a385f 100644 --- a/packages/logger/src/types/formats/index.ts +++ b/packages/logger/src/types/formats/index.ts @@ -1 +1 @@ -export * from './PowertoolLog'; +export * from './PowertoolsLog'; From d1b01b784959b6dfc3de9c7b3def1f33dfe1bc8e Mon Sep 17 00:00:00 2001 From: erikayao93 Date: Fri, 16 Jun 2023 18:55:32 +0000 Subject: [PATCH 4/8] Updated tests to match new naming consistency --- ...test.ts => PowertoolsLogFormatter.test.ts} | 58 +++++++++---------- 1 file changed, 29 insertions(+), 29 deletions(-) rename packages/logger/tests/unit/formatter/{PowertoolLogFormatter.test.ts => PowertoolsLogFormatter.test.ts} (86%) diff --git a/packages/logger/tests/unit/formatter/PowertoolLogFormatter.test.ts b/packages/logger/tests/unit/formatter/PowertoolsLogFormatter.test.ts similarity index 86% rename from packages/logger/tests/unit/formatter/PowertoolLogFormatter.test.ts rename to packages/logger/tests/unit/formatter/PowertoolsLogFormatter.test.ts index f6a282ff3e..a2296639fd 100644 --- a/packages/logger/tests/unit/formatter/PowertoolLogFormatter.test.ts +++ b/packages/logger/tests/unit/formatter/PowertoolsLogFormatter.test.ts @@ -4,10 +4,10 @@ * @group unit/logger/all */ import { AssertionError, strictEqual } from 'assert'; -import { PowertoolLogFormatter } from '../../../src/formatter'; +import { PowertoolsLogFormatter } from '../../../src/formatter'; import { LogAttributes, UnformattedAttributes } from '../../../src/types'; -describe('Class: PowertoolLogFormatter', () => { +describe('Class: PowertoolsLogFormatter', () => { const mockDate = new Date(1466424490000); const dateSpy = jest.spyOn(global, 'Date').mockImplementation(() => mockDate); @@ -18,7 +18,7 @@ describe('Class: PowertoolLogFormatter', () => { describe('Method: formatAttributes', () => { test('when optional parameters DO NOT have a value set, it returns an object with expected structure and values', () => { // Prepare - const formatter = new PowertoolLogFormatter(); + const formatter = new PowertoolsLogFormatter(); const unformattedAttributes: UnformattedAttributes = { sampleRateValue: undefined, awsRegion: 'eu-west-1', @@ -57,7 +57,7 @@ describe('Class: PowertoolLogFormatter', () => { test('when optional parameters DO have a value set, it returns an object with expected structure and values', () => { // Prepare - const formatter = new PowertoolLogFormatter(); + const formatter = new PowertoolsLogFormatter(); const unformattedAttributes: UnformattedAttributes = { sampleRateValue: 0.25, awsRegion: 'eu-west-1', @@ -108,7 +108,7 @@ describe('Class: PowertoolLogFormatter', () => { describe('Method: formatError', () => { test('when an error of type Error is passed, it returns an object with expected structure and values', () => { // Prepare - const formatter = new PowertoolLogFormatter(); + const formatter = new PowertoolsLogFormatter(); const shouldThrow = (): void => { throw new Error('Ouch!'); }; @@ -122,12 +122,12 @@ describe('Class: PowertoolLogFormatter', () => { const formattedError = formatter.formatError(error); expect(formattedError).toEqual({ location: expect.stringMatching( - /PowertoolLogFormatter.test.ts:[0-9]+$/ + /PowertoolsLogFormatter.test.ts:[0-9]+$/ ), message: 'Ouch!', name: 'Error', stack: expect.stringMatching( - /PowertoolLogFormatter.test.ts:[0-9]+:[0-9]+/ + /PowertoolsLogFormatter.test.ts:[0-9]+:[0-9]+/ ), }); } @@ -137,7 +137,7 @@ describe('Class: PowertoolLogFormatter', () => { test('when an error of type ReferenceError is passed, it returns an object with expected structure and values', () => { // Prepare - const formatter = new PowertoolLogFormatter(); + const formatter = new PowertoolsLogFormatter(); const shouldThrow = (): void => { // This is a reference error purposely to test the formatter // eslint-disable-next-line @typescript-eslint/ban-ts-comment @@ -154,12 +154,12 @@ describe('Class: PowertoolLogFormatter', () => { const formattedReferenceError = formatter.formatError(error); expect(formattedReferenceError).toEqual({ location: expect.stringMatching( - /PowertoolLogFormatter.test.ts:[0-9]+$/ + /PowertoolsLogFormatter.test.ts:[0-9]+$/ ), message: 'doesNotExist is not defined', name: 'ReferenceError', stack: expect.stringMatching( - /PowertoolLogFormatter.test.ts:[0-9]+:[0-9]+/ + /PowertoolsLogFormatter.test.ts:[0-9]+:[0-9]+/ ), }); } @@ -169,7 +169,7 @@ describe('Class: PowertoolLogFormatter', () => { test('when an error of type AssertionError is passed, it returns an object with expected structure and values', () => { // Prepare - const formatter = new PowertoolLogFormatter(); + const formatter = new PowertoolsLogFormatter(); const shouldThrow = (): void => { strictEqual(1, 2); }; @@ -185,14 +185,14 @@ describe('Class: PowertoolLogFormatter', () => { ); expect(formattedAssertionError).toEqual({ location: expect.stringMatching( - /PowertoolLogFormatter.test.ts:[0-9]+/ + /PowertoolsLogFormatter.test.ts:[0-9]+/ ), message: expect.stringMatching( /Expected values to be strictly equal/ ), name: 'AssertionError', stack: expect.stringMatching( - /PowertoolLogFormatter.test.ts:[0-9]+:[0-9]+/ + /PowertoolsLogFormatter.test.ts:[0-9]+:[0-9]+/ ), }); } @@ -202,7 +202,7 @@ describe('Class: PowertoolLogFormatter', () => { test('when an error of type RangeError is passed, it returns an object with expected structure and values', () => { // Prepare - const formatter = new PowertoolLogFormatter(); + const formatter = new PowertoolsLogFormatter(); const shouldThrow = (): void => { throw new RangeError('The argument must be between 10 and 20'); }; @@ -216,12 +216,12 @@ describe('Class: PowertoolLogFormatter', () => { const formattedRangeError = formatter.formatError(error); expect(formattedRangeError).toEqual({ location: expect.stringMatching( - /PowertoolLogFormatter.test.ts:[0-9]+/ + /PowertoolsLogFormatter.test.ts:[0-9]+/ ), message: 'The argument must be between 10 and 20', name: 'RangeError', stack: expect.stringMatching( - /PowertoolLogFormatter.test.ts:[0-9]+:[0-9]+/ + /PowertoolsLogFormatter.test.ts:[0-9]+:[0-9]+/ ), }); } @@ -231,7 +231,7 @@ describe('Class: PowertoolLogFormatter', () => { test('when an error of type SyntaxError is passed, it returns an object with expected structure and values', () => { // Prepare - const formatter = new PowertoolLogFormatter(); + const formatter = new PowertoolsLogFormatter(); const shouldThrow = (): void => { eval('foo bar'); }; @@ -245,12 +245,12 @@ describe('Class: PowertoolLogFormatter', () => { const formattedSyntaxError = formatter.formatError(error); expect(formattedSyntaxError).toEqual({ location: expect.stringMatching( - /PowertoolLogFormatter.test.ts:[0-9]+/ + /PowertoolsLogFormatter.test.ts:[0-9]+/ ), message: 'Unexpected identifier', name: 'SyntaxError', stack: expect.stringMatching( - /PowertoolLogFormatter.test.ts:[0-9]+:[0-9]+/ + /PowertoolsLogFormatter.test.ts:[0-9]+:[0-9]+/ ), }); } @@ -260,7 +260,7 @@ describe('Class: PowertoolLogFormatter', () => { test('when an error of type TypeError is passed, it returns an object with expected structure and values', () => { // Prepare - const formatter = new PowertoolLogFormatter(); + const formatter = new PowertoolsLogFormatter(); const shouldThrow = (): void => { // This is a reference error purposely to test the formatter // eslint-disable-next-line @typescript-eslint/ban-ts-comment @@ -278,12 +278,12 @@ describe('Class: PowertoolLogFormatter', () => { const formattedTypeError = formatter.formatError(error); expect(formattedTypeError).toEqual({ location: expect.stringMatching( - /PowertoolLogFormatter.test.ts:[0-9]+/ + /PowertoolsLogFormatter.test.ts:[0-9]+/ ), message: expect.stringMatching(/Cannot read propert/), name: 'TypeError', stack: expect.stringMatching( - /PowertoolLogFormatter.test.ts:[0-9]+:[0-9]+/ + /PowertoolsLogFormatter.test.ts:[0-9]+:[0-9]+/ ), }); } @@ -293,7 +293,7 @@ describe('Class: PowertoolLogFormatter', () => { test('when an error of type URIError is passed, it returns an object with expected structure and values', () => { // Prepare - const formatter = new PowertoolLogFormatter(); + const formatter = new PowertoolsLogFormatter(); const shouldThrow = (): void => { decodeURIComponent('%'); }; @@ -307,12 +307,12 @@ describe('Class: PowertoolLogFormatter', () => { const formattedURIError = formatter.formatError(error); expect(formattedURIError).toEqual({ location: expect.stringMatching( - /PowertoolLogFormatter.test.ts:[0-9]+/ + /PowertoolsLogFormatter.test.ts:[0-9]+/ ), message: 'URI malformed', name: 'URIError', stack: expect.stringMatching( - /PowertoolLogFormatter.test.ts:[0-9]+:[0-9]+/ + /PowertoolsLogFormatter.test.ts:[0-9]+:[0-9]+/ ), }); } @@ -324,7 +324,7 @@ describe('Class: PowertoolLogFormatter', () => { describe('Method: formatTimestamp', () => { test('it returns a datetime value ISO 8601 compliant', () => { // Prepare - const formatter = new PowertoolLogFormatter(); + const formatter = new PowertoolsLogFormatter(); // Act const timestamp = formatter.formatTimestamp(new Date()); @@ -337,7 +337,7 @@ describe('Class: PowertoolLogFormatter', () => { describe('Method: getCodeLocation', () => { test('when the stack IS present, it returns a datetime value ISO 8601 compliant', () => { // Prepare - const formatter = new PowertoolLogFormatter(); + const formatter = new PowertoolsLogFormatter(); const stack = 'Error: Things keep happening!\n' + ' at /home/foo/bar/file-that-threw-the-error.ts:22:5\n' + @@ -352,7 +352,7 @@ describe('Class: PowertoolLogFormatter', () => { test('when the stack IS NOT present, it returns a datetime value ISO 8601 compliant', () => { // Prepare - const formatter = new PowertoolLogFormatter(); + const formatter = new PowertoolsLogFormatter(); const stack = undefined; // Act @@ -364,7 +364,7 @@ describe('Class: PowertoolLogFormatter', () => { test('when the stack IS NOT present, it returns a datetime value ISO 8601 compliant', () => { // Prepare - const formatter = new PowertoolLogFormatter(); + const formatter = new PowertoolsLogFormatter(); const stack = 'A weird stack trace...'; // Act From 256132d1ccd8454de1dd0c9f07304ef9e67c3343 Mon Sep 17 00:00:00 2001 From: erikayao93 Date: Fri, 16 Jun 2023 18:59:48 +0000 Subject: [PATCH 5/8] Updated for tests for new naming consistency --- packages/logger/tests/unit/Logger.test.ts | 34 +++++++++---------- packages/logger/tests/unit/helpers.test.ts | 16 ++++----- .../tests/unit/middleware/middy.test.ts | 6 ++-- 3 files changed, 28 insertions(+), 28 deletions(-) diff --git a/packages/logger/tests/unit/Logger.test.ts b/packages/logger/tests/unit/Logger.test.ts index 61424c04c1..fc1860d841 100644 --- a/packages/logger/tests/unit/Logger.test.ts +++ b/packages/logger/tests/unit/Logger.test.ts @@ -10,7 +10,7 @@ import { } from '@aws-lambda-powertools/commons'; import { createLogger, Logger } from '../../src'; import { EnvironmentVariablesService } from '../../src/config'; -import { PowertoolLogFormatter } from '../../src/formatter'; +import { PowertoolsLogFormatter } from '../../src/formatter'; import { ClassThatLogs, LogJsonIndent, @@ -798,7 +798,7 @@ describe('Class: Logger', () => { envVarsService: expect.any(EnvironmentVariablesService), logEvent: false, logIndentation: 0, - logFormatter: expect.any(PowertoolLogFormatter), + logFormatter: expect.any(PowertoolsLogFormatter), logLevel: 8, logLevelThresholds: { ...logLevelThresholds, @@ -1621,7 +1621,7 @@ describe('Class: Logger', () => { envVarsService: expect.any(EnvironmentVariablesService), logEvent: false, logIndentation: INDENTATION, - logFormatter: expect.any(PowertoolLogFormatter), + logFormatter: expect.any(PowertoolsLogFormatter), logLevel: 8, logLevelThresholds: { ...logLevelThresholds, @@ -1644,7 +1644,7 @@ describe('Class: Logger', () => { envVarsService: expect.any(EnvironmentVariablesService), logEvent: false, logIndentation: INDENTATION, - logFormatter: expect.any(PowertoolLogFormatter), + logFormatter: expect.any(PowertoolsLogFormatter), logLevel: 8, logLevelThresholds: { ...logLevelThresholds, @@ -1667,7 +1667,7 @@ describe('Class: Logger', () => { envVarsService: expect.any(EnvironmentVariablesService), logEvent: false, logIndentation: INDENTATION, - logFormatter: expect.any(PowertoolLogFormatter), + logFormatter: expect.any(PowertoolsLogFormatter), logLevel: 8, logLevelThresholds: { ...logLevelThresholds, @@ -1733,7 +1733,7 @@ describe('Class: Logger', () => { envVarsService: expect.any(EnvironmentVariablesService), logEvent: false, logIndentation: INDENTATION, - logFormatter: expect.any(PowertoolLogFormatter), + logFormatter: expect.any(PowertoolsLogFormatter), logLevel: 8, logLevelThresholds: { ...logLevelThresholds, @@ -1756,7 +1756,7 @@ describe('Class: Logger', () => { envVarsService: expect.any(EnvironmentVariablesService), logEvent: false, logIndentation: INDENTATION, - logFormatter: expect.any(PowertoolLogFormatter), + logFormatter: expect.any(PowertoolsLogFormatter), logLevel: 8, logLevelThresholds: { ...logLevelThresholds, @@ -1782,7 +1782,7 @@ describe('Class: Logger', () => { envVarsService: expect.any(EnvironmentVariablesService), logEvent: false, logIndentation: INDENTATION, - logFormatter: expect.any(PowertoolLogFormatter), + logFormatter: expect.any(PowertoolsLogFormatter), logLevel: 8, logLevelThresholds: { ...logLevelThresholds, @@ -1805,7 +1805,7 @@ describe('Class: Logger', () => { envVarsService: expect.any(EnvironmentVariablesService), logEvent: false, logIndentation: INDENTATION, - logFormatter: expect.any(PowertoolLogFormatter), + logFormatter: expect.any(PowertoolsLogFormatter), logLevel: 20, logLevelThresholds: { ...logLevelThresholds, @@ -1849,7 +1849,7 @@ describe('Class: Logger', () => { envVarsService: expect.any(EnvironmentVariablesService), logEvent: false, logIndentation: INDENTATION, - logFormatter: expect.any(PowertoolLogFormatter), + logFormatter: expect.any(PowertoolsLogFormatter), logLevel: 8, logLevelThresholds: { ...logLevelThresholds, @@ -1872,7 +1872,7 @@ describe('Class: Logger', () => { envVarsService: expect.any(EnvironmentVariablesService), logEvent: false, logIndentation: INDENTATION, - logFormatter: expect.any(PowertoolLogFormatter), + logFormatter: expect.any(PowertoolsLogFormatter), logLevel: 8, logLevelThresholds: { ...logLevelThresholds, @@ -1902,7 +1902,7 @@ describe('Class: Logger', () => { envVarsService: expect.any(EnvironmentVariablesService), logEvent: false, logIndentation: INDENTATION, - logFormatter: expect.any(PowertoolLogFormatter), + logFormatter: expect.any(PowertoolsLogFormatter), logLevel: 8, logLevelThresholds: { ...logLevelThresholds, @@ -1943,7 +1943,7 @@ describe('Class: Logger', () => { envVarsService: expect.any(EnvironmentVariablesService), logEvent: false, logIndentation: 0, - logFormatter: expect.any(PowertoolLogFormatter), + logFormatter: expect.any(PowertoolsLogFormatter), logLevel: 8, logLevelThresholds: { ...logLevelThresholds, @@ -1970,7 +1970,7 @@ describe('Class: Logger', () => { test('child logger should have the same logFormatter as its parent', () => { // Prepare - class MyCustomLogFormatter extends PowertoolLogFormatter {} + class MyCustomLogFormatter extends PowertoolsLogFormatter {} const parentLogger = new Logger({ logFormatter: new MyCustomLogFormatter(), }); @@ -1988,7 +1988,7 @@ describe('Class: Logger', () => { test('child logger with custom logFormatter in options should have provided logFormatter', () => { // Prepare - class MyCustomLogFormatter extends PowertoolLogFormatter {} + class MyCustomLogFormatter extends PowertoolsLogFormatter {} const parentLogger = new Logger(); // Act @@ -1999,7 +1999,7 @@ describe('Class: Logger', () => { // Assess expect(parentLogger).toEqual( expect.objectContaining({ - logFormatter: expect.any(PowertoolLogFormatter), + logFormatter: expect.any(PowertoolsLogFormatter), }) ); @@ -2012,7 +2012,7 @@ describe('Class: Logger', () => { test('child logger should have exact same attributes as the parent logger created with all non-default options', () => { // Prepare - class MyCustomLogFormatter extends PowertoolLogFormatter {} + class MyCustomLogFormatter extends PowertoolsLogFormatter {} class MyCustomEnvironmentVariablesService extends EnvironmentVariablesService {} const options: ConstructorOptions = { diff --git a/packages/logger/tests/unit/helpers.test.ts b/packages/logger/tests/unit/helpers.test.ts index ba16e38c31..8ddda59475 100644 --- a/packages/logger/tests/unit/helpers.test.ts +++ b/packages/logger/tests/unit/helpers.test.ts @@ -8,7 +8,7 @@ import { ConfigServiceInterface, EnvironmentVariablesService, } from '../../src/config'; -import { LogFormatter, PowertoolLogFormatter } from '../../src/formatter'; +import { LogFormatter, PowertoolsLogFormatter } from '../../src/formatter'; import { ConstructorOptions, LogLevelThresholds } from '../../src/types'; import { createLogger, Logger } from './../../src'; @@ -56,7 +56,7 @@ describe('Helper: createLogger function', () => { customConfigService: undefined, defaultServiceName: 'service_undefined', logLevel: 8, - logFormatter: expect.any(PowertoolLogFormatter), + logFormatter: expect.any(PowertoolsLogFormatter), }) ); }); @@ -67,7 +67,7 @@ describe('Helper: createLogger function', () => { logLevel: 'WARN', serviceName: 'my-lambda-service', sampleRateValue: 1, - logFormatter: new PowertoolLogFormatter(), + logFormatter: new PowertoolsLogFormatter(), customConfigService: new EnvironmentVariablesService(), persistentLogAttributes: { awsAccountId: '123456789', @@ -87,7 +87,7 @@ describe('Helper: createLogger function', () => { envVarsService: expect.any(EnvironmentVariablesService), logEvent: false, logIndentation: 0, - logFormatter: expect.any(PowertoolLogFormatter), + logFormatter: expect.any(PowertoolsLogFormatter), logLevel: 16, console: expect.any(Console), logLevelThresholds: { @@ -124,7 +124,7 @@ describe('Helper: createLogger function', () => { envVarsService: expect.any(EnvironmentVariablesService), logEvent: false, logIndentation: 0, - logFormatter: expect.any(PowertoolLogFormatter), + logFormatter: expect.any(PowertoolsLogFormatter), logLevel: 12, console: expect.any(Console), logLevelThresholds: { @@ -223,7 +223,7 @@ describe('Helper: createLogger function', () => { envVarsService: expect.any(EnvironmentVariablesService), customConfigService: undefined, logLevel: 20, - logFormatter: expect.any(PowertoolLogFormatter), + logFormatter: expect.any(PowertoolsLogFormatter), }) ); }); @@ -252,7 +252,7 @@ describe('Helper: createLogger function', () => { envVarsService: expect.any(EnvironmentVariablesService), customConfigService: undefined, logLevel: 16, - logFormatter: expect.any(PowertoolLogFormatter), + logFormatter: expect.any(PowertoolsLogFormatter), }) ); }); @@ -274,7 +274,7 @@ describe('Helper: createLogger function', () => { envVarsService: expect.any(EnvironmentVariablesService), logEvent: false, logIndentation: 0, - logFormatter: expect.any(PowertoolLogFormatter), + logFormatter: expect.any(PowertoolsLogFormatter), logLevel: 12, console: expect.any(Console), logLevelThresholds: { diff --git a/packages/logger/tests/unit/middleware/middy.test.ts b/packages/logger/tests/unit/middleware/middy.test.ts index 4c97a93444..3ef3bb36fa 100644 --- a/packages/logger/tests/unit/middleware/middy.test.ts +++ b/packages/logger/tests/unit/middleware/middy.test.ts @@ -14,7 +14,7 @@ import { import { injectLambdaContext } from '../../../src/middleware/middy'; import { Logger } from './../../../src'; import middy from '@middy/core'; -import { PowertoolLogFormatter } from '../../../src/formatter'; +import { PowertoolsLogFormatter } from '../../../src/formatter'; import { Console } from 'console'; import { Context } from 'aws-lambda'; @@ -74,7 +74,7 @@ describe('Middy middleware', () => { envVarsService: expect.any(EnvironmentVariablesService), customConfigService: undefined, logLevel: 8, - logFormatter: expect.any(PowertoolLogFormatter), + logFormatter: expect.any(PowertoolsLogFormatter), }) ); }); @@ -113,7 +113,7 @@ describe('Middy middleware', () => { envVarsService: expect.any(EnvironmentVariablesService), customConfigService: undefined, logLevel: 8, - logFormatter: expect.any(PowertoolLogFormatter), + logFormatter: expect.any(PowertoolsLogFormatter), console: expect.any(Console), }); expect(logger).toEqual(expectation); From 51c5f46c2f96c0ed1ab9a581fc26fc55c443a7ad Mon Sep 17 00:00:00 2001 From: erikayao93 Date: Fri, 16 Jun 2023 21:09:09 +0000 Subject: [PATCH 6/8] Updated formatter for new design decisions --- packages/logger/src/Logger.ts | 4 ++-- packages/logger/src/formatter/LogFormatter.ts | 2 -- packages/logger/src/formatter/LogFormatterInterface.ts | 2 -- packages/logger/src/formatter/PowertoolsLogFormatter.ts | 3 --- .../tests/unit/formatter/PowertoolsLogFormatter.test.ts | 4 ---- 5 files changed, 2 insertions(+), 13 deletions(-) diff --git a/packages/logger/src/Logger.ts b/packages/logger/src/Logger.ts index d7180b29b8..d48594019b 100644 --- a/packages/logger/src/Logger.ts +++ b/packages/logger/src/Logger.ts @@ -617,7 +617,8 @@ class Logger extends Utility implements ClassThatLogs { this.getPowertoolLogData() ); - let additionalLogAttributes: LogAttributes = {}; + let additionalLogAttributes: LogAttributes = + this.getPersistentLogAttributes(); if (typeof input !== 'string') { additionalLogAttributes = merge(additionalLogAttributes, input); } @@ -634,7 +635,6 @@ class Logger extends Utility implements ClassThatLogs { const logItem = this.getLogFormatter().formatAttributes( unformattedBaseAttributes, - this.getPersistentLogAttributes(), additionalLogAttributes ); diff --git a/packages/logger/src/formatter/LogFormatter.ts b/packages/logger/src/formatter/LogFormatter.ts index cd5db9659f..6f5bcbdb79 100644 --- a/packages/logger/src/formatter/LogFormatter.ts +++ b/packages/logger/src/formatter/LogFormatter.ts @@ -14,13 +14,11 @@ abstract class LogFormatter implements LogFormatterInterface { * It formats key-value pairs of log attributes. * * @param {UnformattedAttributes} attributes - * @param {LogAttributes} persistentLogAttributes * @param {LogAttributes} additionalLogAttributes * @returns {LogItem} */ public abstract formatAttributes( attributes: UnformattedAttributes, - persistentLogAttributes: LogAttributes, additionalLogAttributes: LogAttributes ): LogItem; diff --git a/packages/logger/src/formatter/LogFormatterInterface.ts b/packages/logger/src/formatter/LogFormatterInterface.ts index 633e7c1fba..0fe1dd9909 100644 --- a/packages/logger/src/formatter/LogFormatterInterface.ts +++ b/packages/logger/src/formatter/LogFormatterInterface.ts @@ -9,13 +9,11 @@ interface LogFormatterInterface { * It formats key-value pairs of log attributes. * * @param {UnformattedAttributes} attributes - * @param {LogAttributes} persistentLogAttributes * @param {LogAttributes} additionalLogAttributes * @returns {LogItem} */ formatAttributes( attributes: UnformattedAttributes, - persistentLogAttributes: LogAttributes, additionalLogAttributes: LogAttributes ): LogItem; diff --git a/packages/logger/src/formatter/PowertoolsLogFormatter.ts b/packages/logger/src/formatter/PowertoolsLogFormatter.ts index 33e0296d3d..6a599d6eb8 100644 --- a/packages/logger/src/formatter/PowertoolsLogFormatter.ts +++ b/packages/logger/src/formatter/PowertoolsLogFormatter.ts @@ -20,11 +20,9 @@ class PowertoolsLogFormatter extends LogFormatter { */ public formatAttributes( attributes: UnformattedAttributes, - persistentLogAttributes: LogAttributes, additionalLogAttributes: LogAttributes ): LogItem { const baseAttributes: PowertoolsLog = { - // standard attributes from UnformattedAttributes cold_start: attributes.lambdaContext?.coldStart, function_arn: attributes.lambdaContext?.invokedFunctionArn, function_memory_size: attributes.lambdaContext?.memoryLimitInMB, @@ -40,7 +38,6 @@ class PowertoolsLogFormatter extends LogFormatter { const powertoolLogItem = new LogItem({ attributes: baseAttributes }); - powertoolLogItem.addAttributes(persistentLogAttributes); powertoolLogItem.addAttributes(additionalLogAttributes); return powertoolLogItem; diff --git a/packages/logger/tests/unit/formatter/PowertoolsLogFormatter.test.ts b/packages/logger/tests/unit/formatter/PowertoolsLogFormatter.test.ts index a2296639fd..11e184e5c3 100644 --- a/packages/logger/tests/unit/formatter/PowertoolsLogFormatter.test.ts +++ b/packages/logger/tests/unit/formatter/PowertoolsLogFormatter.test.ts @@ -29,13 +29,11 @@ describe('Class: PowertoolsLogFormatter', () => { timestamp: new Date(), message: 'This is a WARN log', }; - const persistentLogAttributes: LogAttributes = {}; const additionalLogAttributes: LogAttributes = {}; // Act const value = formatter.formatAttributes( unformattedAttributes, - persistentLogAttributes, additionalLogAttributes ); @@ -78,13 +76,11 @@ describe('Class: PowertoolsLogFormatter', () => { awsRequestId: 'abcdefg123456789', }, }; - const persistentLogAttributes: LogAttributes = {}; const additionalLogAttributes: LogAttributes = {}; // Act const value = formatter.formatAttributes( unformattedAttributes, - persistentLogAttributes, additionalLogAttributes ); From f9c937fbf2de0467ae7c8f095e890847824dbe65 Mon Sep 17 00:00:00 2001 From: erikayao93 Date: Mon, 19 Jun 2023 15:58:58 +0000 Subject: [PATCH 7/8] Update Logger for ephemeral attributes --- packages/logger/src/Logger.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/packages/logger/src/Logger.ts b/packages/logger/src/Logger.ts index d48594019b..afc0100ef6 100644 --- a/packages/logger/src/Logger.ts +++ b/packages/logger/src/Logger.ts @@ -617,8 +617,11 @@ class Logger extends Utility implements ClassThatLogs { this.getPowertoolLogData() ); - let additionalLogAttributes: LogAttributes = - this.getPersistentLogAttributes(); + let additionalLogAttributes: LogAttributes = {}; + additionalLogAttributes = merge( + additionalLogAttributes, + this.getPersistentLogAttributes() + ); if (typeof input !== 'string') { additionalLogAttributes = merge(additionalLogAttributes, input); } From a81abb5603ba06f98560d91c051b1713604e4af1 Mon Sep 17 00:00:00 2001 From: erikayao93 Date: Mon, 19 Jun 2023 17:41:40 +0000 Subject: [PATCH 8/8] Update bringYourOwnFormatter documentation to match new formatter --- docs/snippets/logger/bringYourOwnFormatterClass.ts | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/docs/snippets/logger/bringYourOwnFormatterClass.ts b/docs/snippets/logger/bringYourOwnFormatterClass.ts index ab7e207cb9..9d189b2b8b 100644 --- a/docs/snippets/logger/bringYourOwnFormatterClass.ts +++ b/docs/snippets/logger/bringYourOwnFormatterClass.ts @@ -3,13 +3,17 @@ import { LogAttributes, UnformattedAttributes, } from '@aws-lambda-powertools/logger/lib/types'; +import { LogItem } from '@aws-lambda-powertools/logger/lib/log'; // Replace this line with your own type type MyCompanyLog = LogAttributes; class MyCompanyLogFormatter extends LogFormatter { - public formatAttributes(attributes: UnformattedAttributes): MyCompanyLog { - return { + public formatAttributes( + attributes: UnformattedAttributes, + additionalLogAttributes: LogAttributes + ): LogItem { + const baseAttributes: MyCompanyLog = { message: attributes.message, service: attributes.serviceName, environment: attributes.environment, @@ -31,6 +35,11 @@ class MyCompanyLogFormatter extends LogFormatter { sampleRateValue: attributes.sampleRateValue, }, }; + + const logItem = new LogItem({ attributes: baseAttributes }); + logItem.addAttributes(additionalLogAttributes); // add any attributes not explicitly defined + + return logItem; } }