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; } } diff --git a/packages/logger/src/Logger.ts b/packages/logger/src/Logger.ts index 1396c16f91..06d1c01a68 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'; @@ -617,16 +617,13 @@ 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 = {}; + additionalLogAttributes = merge( + additionalLogAttributes, + this.getPersistentLogAttributes() + ); if (typeof input !== 'string') { - logItem.addAttributes(input); + additionalLogAttributes = merge(additionalLogAttributes, input); } extraInput.forEach((item: Error | LogAttributes | string) => { const attributes: LogAttributes = @@ -636,9 +633,14 @@ class Logger extends Utility implements ClassThatLogs { ? { extra: item } : item; - logItem.addAttributes(attributes); + additionalLogAttributes = merge(additionalLogAttributes, attributes); }); + const logItem = this.getLogFormatter().formatAttributes( + unformattedBaseAttributes, + additionalLogAttributes + ); + return logItem; } @@ -915,7 +917,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/LogFormatter.ts b/packages/logger/src/formatter/LogFormatter.ts index dac142a05a..6f5bcbdb79 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,13 @@ abstract class LogFormatter implements LogFormatterInterface { * It formats key-value pairs of log attributes. * * @param {UnformattedAttributes} attributes - * @returns {LogAttributes} + * @param {LogAttributes} additionalLogAttributes + * @returns {LogItem} */ public abstract formatAttributes( - attributes: UnformattedAttributes - ): LogAttributes; + attributes: UnformattedAttributes, + 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..0fe1dd9909 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,13 @@ interface LogFormatterInterface { * It formats key-value pairs of log attributes. * * @param {UnformattedAttributes} attributes - * @returns {PowertoolLog} + * @param {LogAttributes} additionalLogAttributes + * @returns {LogItem} */ - formatAttributes(attributes: UnformattedAttributes): LogAttributes; + formatAttributes( + attributes: UnformattedAttributes, + additionalLogAttributes: LogAttributes + ): LogItem; /** * It formats a given Error parameter. diff --git a/packages/logger/src/formatter/PowertoolLogFormatter.ts b/packages/logger/src/formatter/PowertoolsLogFormatter.ts similarity index 58% rename from packages/logger/src/formatter/PowertoolLogFormatter.ts rename to packages/logger/src/formatter/PowertoolsLogFormatter.ts index 0fa77928e2..6a599d6eb8 100644 --- a/packages/logger/src/formatter/PowertoolLogFormatter.ts +++ b/packages/logger/src/formatter/PowertoolsLogFormatter.ts @@ -1,6 +1,7 @@ import { LogFormatter } from '.'; -import { UnformattedAttributes } from '../types'; -import { PowertoolLog } from '../types/formats'; +import { LogAttributes, UnformattedAttributes } from '../types'; +import { PowertoolsLog } from '../types/formats'; +import { LogItem } from '../log'; /** * This class is used to transform a set of log key-value pairs @@ -9,15 +10,19 @@ import { PowertoolLog } from '../types/formats'; * @class * @extends {LogFormatter} */ -class PowertoolLogFormatter extends LogFormatter { +class PowertoolsLogFormatter extends LogFormatter { /** * It formats key-value pairs of log attributes. * * @param {UnformattedAttributes} attributes - * @returns {PowertoolLog} + * @param {LogAttributes} additionalLogAttributes + * @returns {PowertoolsLog} */ - public formatAttributes(attributes: UnformattedAttributes): PowertoolLog { - return { + public formatAttributes( + attributes: UnformattedAttributes, + additionalLogAttributes: LogAttributes + ): LogItem { + const baseAttributes: PowertoolsLog = { cold_start: attributes.lambdaContext?.coldStart, function_arn: attributes.lambdaContext?.invokedFunctionArn, function_memory_size: attributes.lambdaContext?.memoryLimitInMB, @@ -30,7 +35,13 @@ class PowertoolLogFormatter extends LogFormatter { timestamp: this.formatTimestamp(attributes.timestamp), xray_trace_id: attributes.xRayTraceId, }; + + const powertoolLogItem = new LogItem({ attributes: baseAttributes }); + + powertoolLogItem.addAttributes(additionalLogAttributes); + + return powertoolLogItem; } } -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/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 { 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'; 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/formatter/PowertoolLogFormatter.test.ts b/packages/logger/tests/unit/formatter/PowertoolsLogFormatter.test.ts similarity index 82% rename from packages/logger/tests/unit/formatter/PowertoolLogFormatter.test.ts rename to packages/logger/tests/unit/formatter/PowertoolsLogFormatter.test.ts index 49daebe597..11e184e5c3 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 { UnformattedAttributes } from '../../../src/types'; +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', @@ -29,12 +29,16 @@ describe('Class: PowertoolLogFormatter', () => { timestamp: new Date(), message: 'This is a WARN log', }; + const additionalLogAttributes: LogAttributes = {}; // Act - const value = formatter.formatAttributes(unformattedAttributes); + const value = formatter.formatAttributes( + unformattedAttributes, + additionalLogAttributes + ); // Assess - expect(value).toEqual({ + expect(value.getAttributes()).toEqual({ cold_start: undefined, function_arn: undefined, function_memory_size: undefined, @@ -51,7 +55,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', @@ -72,12 +76,16 @@ describe('Class: PowertoolLogFormatter', () => { awsRequestId: 'abcdefg123456789', }, }; + const additionalLogAttributes: LogAttributes = {}; // Act - const value = formatter.formatAttributes(unformattedAttributes); + const value = formatter.formatAttributes( + unformattedAttributes, + 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, @@ -96,7 +104,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!'); }; @@ -110,12 +118,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]+/ ), }); } @@ -125,7 +133,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 @@ -142,12 +150,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]+/ ), }); } @@ -157,7 +165,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); }; @@ -173,14 +181,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]+/ ), }); } @@ -190,7 +198,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'); }; @@ -204,12 +212,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]+/ ), }); } @@ -219,7 +227,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'); }; @@ -233,12 +241,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]+/ ), }); } @@ -248,7 +256,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 @@ -266,12 +274,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]+/ ), }); } @@ -281,7 +289,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('%'); }; @@ -295,12 +303,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]+/ ), }); } @@ -312,7 +320,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()); @@ -325,7 +333,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' + @@ -340,7 +348,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 @@ -352,7 +360,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 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 f2815b01cd..39df13ea72 100644 --- a/packages/logger/tests/unit/middleware/middy.test.ts +++ b/packages/logger/tests/unit/middleware/middy.test.ts @@ -15,7 +15,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'; @@ -75,7 +75,7 @@ describe('Middy middleware', () => { envVarsService: expect.any(EnvironmentVariablesService), customConfigService: undefined, logLevel: 8, - logFormatter: expect.any(PowertoolLogFormatter), + logFormatter: expect.any(PowertoolsLogFormatter), }) ); }); @@ -114,7 +114,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);