From 313be43493606c920b2a5181b7fb09621c8e0f04 Mon Sep 17 00:00:00 2001 From: Sara Gerion <47529391+saragerion@users.noreply.github.com> Date: Thu, 30 Dec 2021 20:49:16 +0100 Subject: [PATCH 01/12] docs(logger): WIP jsdocs Logger.ts --- .eslintrc.js | 2 +- packages/logger/src/Logger.ts | 134 ++++++++++++++++++++++++++++++++++ 2 files changed, 135 insertions(+), 1 deletion(-) diff --git a/.eslintrc.js b/.eslintrc.js index b83ce24ffc..a8cbf8ceef 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -7,7 +7,7 @@ module.exports = { }, extends: [ 'plugin:@typescript-eslint/eslint-recommended', 'plugin:@typescript-eslint/recommended' ], parser: '@typescript-eslint/parser', - plugins: ['@typescript-eslint'], + plugins: ['@typescript-eslint', 'jsdoc'], settings: { 'import/resolver': { node: {}, diff --git a/packages/logger/src/Logger.ts b/packages/logger/src/Logger.ts index caf980eb7c..27ce6f61d3 100644 --- a/packages/logger/src/Logger.ts +++ b/packages/logger/src/Logger.ts @@ -17,6 +17,27 @@ import type { PowertoolLogData, } from './types'; +/** + * ## Intro + * + * Logger provides an opinionated logger with output structured as JSON. + * + * ## Key features + * + * - Capture key fields from Lambda context, cold start and structures logging output as JSON + * - Log Lambda context when instructed (disabled by default) + * - Log sampling prints all logs for a percentage of invocations (disabled by default) + * - Append additional keys to structured log at any point in time + * + * ## Usage + * + * import { Logger } from "@aws-lambda-powertools/logger"; + * const logger = new Logger(); + * + * @class + * @implements {ClassThatLogs} + * @see https://awslabs.github.io/aws-lambda-powertools-typescript/latest/core/logger/ + */ class Logger implements ClassThatLogs { private static coldStart?: boolean = undefined; @@ -48,10 +69,28 @@ class Logger implements ClassThatLogs { private powertoolLogData: PowertoolLogData = {}; + /** + * It takes a set of options (settings): + * - logging level + * - name of the service + * - sample rate value + * - log formatter + * - config service + * - persistent attributes + * - environment name + * @param {LoggerOptions} options + */ public constructor(options: LoggerOptions = {}) { this.setOptions(options); } + /** + * Adds the current AWS Lambda's invocation context data to the powertoolLogData propriety of the instance. + * This context data will be part of all printed log items. + * + * @param {Context} context + * @return {void} + */ public addContext(context: Context): void { Logger.evaluateColdStartOnce(); const lambdaContext: Partial = { @@ -68,18 +107,42 @@ class Logger implements ClassThatLogs { }); } + /** + * Adds given attributes (key-value pairs) to all log items generated by this Logger instance. + * + * @param {LogAttributes} attributes + * @return {void} + */ public addPersistentLogAttributes(attributes?: LogAttributes): void { this.persistentLogAttributes = merge(this.getPersistentLogAttributes(), attributes); } + /** + * Alias for addPersistentLogAttributes. + * + * @param {LogAttributes} attributes + * @return {void} + */ public appendKeys(attributes?: LogAttributes): void { this.addPersistentLogAttributes(attributes); } + /** + * Creates a separated Logger instance, identical to the current one. + * It's possible to overwrite the new instance options by passing them. + * + * @param {LoggerOptions} options + * @returns {Logger} + */ public createChild(options: LoggerOptions = {}): Logger { return cloneDeep(this).setOptions(options); } + /** + * + * @param {LogItemMessage} input + * @param {Error | LogAttributes | unknown} extraInput + */ public debug(input: LogItemMessage, ...extraInput: LogItemExtraInput): void { if (!this.shouldPrint('DEBUG')) { return; @@ -87,28 +150,53 @@ class Logger implements ClassThatLogs { this.printLog(this.createAndPopulateLogItem('DEBUG', input, extraInput)); } + /** + * + * @param {LogItemMessage} input + * @param {Error | LogAttributes | unknown} extraInput + */ public error(input: LogItemMessage, ...extraInput: LogItemExtraInput): void { this.printLog(this.createAndPopulateLogItem('ERROR', input, extraInput)); } + /** + * + */ public static evaluateColdStartOnce(): void { if (Logger.getColdStartEvaluatedValue() === false) { Logger.evaluateColdStart(); } } + /** + * + * @returns {boolean} + */ public static getColdStartEvaluatedValue(): boolean { return Logger.coldStartEvaluated; } + /** + * + * @returns {boolean | undefined} + */ public static getColdStartValue(): boolean | undefined { return Logger.coldStart; } + /** + * + * @returns {boolean} + */ public getLogsSampled(): boolean { return this.logsSampled; } + /** + * + * @param {LogItemMessage} input + * @param {Error | LogAttributes | unknown} extraInput + */ public info(input: LogItemMessage, ...extraInput: LogItemExtraInput): void { if (!this.shouldPrint('INFO')) { return; @@ -116,6 +204,10 @@ class Logger implements ClassThatLogs { this.printLog(this.createAndPopulateLogItem('INFO', input, extraInput)); } + /** + * + * @returns {HandlerMethodDecorator} + */ public injectLambdaContext(): HandlerMethodDecorator { return (target, propertyKey, descriptor) => { const originalMethod = descriptor.value; @@ -129,18 +221,33 @@ class Logger implements ClassThatLogs { }; } + /** + * + */ public refreshSampleRateCalculation(): void { this.setLogsSampled(); } + /** + * + * @param {boolean} value + */ public static setColdStartEvaluatedValue(value: boolean): void { Logger.coldStartEvaluated = value; } + /** + * + * @param {boolean | undefined} value + */ public static setColdStartValue(value: boolean | undefined): void { Logger.coldStart = value; } + /** + * + * @param {number} sampleRateValue + */ public setSampleRateValue(sampleRateValue?: number): void { this.powertoolLogData.sampleRateValue = sampleRateValue || @@ -148,6 +255,11 @@ class Logger implements ClassThatLogs { this.getEnvVarsService().getSampleRateValue(); } + /** + * + * @param {LogItemMessage} input + * @param {Error | LogAttributes | unknown} extraInput + */ public warn(input: LogItemMessage, ...extraInput: LogItemExtraInput): void { if (!this.shouldPrint('WARN')) { return; @@ -155,12 +267,25 @@ class Logger implements ClassThatLogs { this.printLog(this.createAndPopulateLogItem('WARN', input, extraInput)); } + /** + * + * @param {Partial} attributesArray + * @private + */ private addToPowertoolLogData(...attributesArray: Array>): void { attributesArray.forEach((attributes: Partial) => { this.powertoolLogData = merge(this.getPowertoolLogData(), attributes); }); } + /** + * + * @param {LogLevel} logLevel + * @param {LogItemMessage} input + * @param {LogItemExtraInput} extraInput + * @returns {LogItem} + * @private + */ private createAndPopulateLogItem(logLevel: LogLevel, input: LogItemMessage, extraInput: LogItemExtraInput): LogItem { const unformattedBaseAttributes = merge(this.getPowertoolLogData(), { logLevel, @@ -185,6 +310,10 @@ class Logger implements ClassThatLogs { return logItem; } + /** + * + * @private + */ private static evaluateColdStart(): void { const coldStartValue = Logger.getColdStartValue(); if (typeof coldStartValue === 'undefined') { @@ -198,6 +327,11 @@ class Logger implements ClassThatLogs { Logger.setColdStartEvaluatedValue(true); } + /** + * + * @returns {ConfigServiceInterface | undefined} + * @private + */ private getCustomConfigService(): ConfigServiceInterface | undefined { return this.customConfigService; } From 57c5929b198e51e796d6a3d37d982493375b8f66 Mon Sep 17 00:00:00 2001 From: Sara Gerion Date: Wed, 19 Jan 2022 21:54:30 +0100 Subject: [PATCH 02/12] feat(logger): add jsdocs support --- packages/logger/src/Logger.ts | 213 ++++++++++++++++-- packages/logger/src/config/ConfigService.ts | 43 +++- .../src/config/ConfigServiceInterface.ts | 33 +++ .../src/config/EnvironmentVariablesService.ts | 64 ++++++ packages/logger/src/formatter/LogFormatter.ts | 33 +++ .../src/formatter/LogFormatterInterface.ts | 17 ++ .../src/formatter/PowertoolLogFormatter.ts | 13 ++ packages/logger/src/middleware/middy.ts | 7 + 8 files changed, 403 insertions(+), 20 deletions(-) diff --git a/packages/logger/src/Logger.ts b/packages/logger/src/Logger.ts index dd57dfc706..4ac0faa269 100644 --- a/packages/logger/src/Logger.ts +++ b/packages/logger/src/Logger.ts @@ -19,9 +19,12 @@ import type { } from './types'; /** + * + * # Logger class + * * ## Intro * - * Logger provides an opinionated logger with output structured as JSON. + * The Logger utility provides an opinionated logger with output structured as JSON. * * ## Key features * @@ -30,7 +33,7 @@ import type { * - Log sampling prints all logs for a percentage of invocations (disabled by default) * - Append additional keys to structured log at any point in time * - * ## Usage + * ## Basic usage * * import { Logger } from "@aws-lambda-powertools/logger"; * const logger = new Logger(); @@ -41,6 +44,10 @@ import type { */ class Logger implements ClassThatLogs { + /** + * + * @private + */ private static coldStart?: boolean = undefined; private static coldStartEvaluated: boolean = false; @@ -71,14 +78,8 @@ class Logger implements ClassThatLogs { private powertoolLogData: PowertoolLogData = {}; /** - * It takes a set of options (settings): - * - logging level - * - name of the service - * - sample rate value - * - log formatter - * - config service - * - persistent attributes - * - environment name + * It initializes the Logger class with an optional set of options (settings). + * * * @param {LoggerOptions} options */ public constructor(options: LoggerOptions = {}) { @@ -90,7 +91,7 @@ class Logger implements ClassThatLogs { * This context data will be part of all printed log items. * * @param {Context} context - * @return {void} + * @returns {void} */ public addContext(context: Context): void { Logger.evaluateColdStartOnce(); @@ -112,7 +113,7 @@ class Logger implements ClassThatLogs { * Adds given attributes (key-value pairs) to all log items generated by this Logger instance. * * @param {LogAttributes} attributes - * @return {void} + * @returns {void} */ public addPersistentLogAttributes(attributes?: LogAttributes): void { this.persistentLogAttributes = merge(attributes, this.getPersistentLogAttributes()); @@ -122,14 +123,14 @@ class Logger implements ClassThatLogs { * Alias for addPersistentLogAttributes. * * @param {LogAttributes} attributes - * @return {void} + * @returns {void} */ public appendKeys(attributes?: LogAttributes): void { this.addPersistentLogAttributes(attributes); } /** - * Creates a separated Logger instance, identical to the current one. + * Creates a separate Logger instance, identical to the current one. * It's possible to overwrite the new instance options by passing them. * * @param {LoggerOptions} options @@ -140,25 +141,32 @@ class Logger implements ClassThatLogs { } /** + * It prints a log item with level DEBUG. * * @param {LogItemMessage} input * @param {Error | LogAttributes | unknown} extraInput + * @returns {void} */ public debug(input: LogItemMessage, ...extraInput: LogItemExtraInput): void { this.processLogItem('DEBUG', input, extraInput); } /** + * It prints a log item with level ERROR. * * @param {LogItemMessage} input * @param {Error | LogAttributes | unknown} extraInput + * @returns {void} */ public error(input: LogItemMessage, ...extraInput: LogItemExtraInput): void { this.processLogItem('ERROR', input, extraInput); } /** + * It evaluates whether the current Lambda function invocation has a cold start or not. * + * @static + * @returns {void} */ public static evaluateColdStartOnce(): void { if (!Logger.getColdStartEvaluatedValue()) { @@ -167,7 +175,10 @@ class Logger implements ClassThatLogs { } /** + * It returns a boolean true value if the current Lambda function invocation has been already evaluated to + * determine if it's a cold start, false if not. * + * @static * @returns {boolean} */ public static getColdStartEvaluatedValue(): boolean { @@ -175,7 +186,9 @@ class Logger implements ClassThatLogs { } /** + * It returns a boolean true value if the current Lambda function invocation has a cold start, false otherwise. * + * @static * @returns {boolean | undefined} */ public static getColdStartValue(): boolean | undefined { @@ -183,6 +196,8 @@ class Logger implements ClassThatLogs { } /** + * It returns a boolean true value if, in case of sample rating feature enabled, the current + * Lambda function invocation's logs will be printed to stdout. It returns false otherwise. * * @returns {boolean} */ @@ -191,16 +206,23 @@ class Logger implements ClassThatLogs { } /** + * It prints a log item with level INFO. * * @param {LogItemMessage} input * @param {Error | LogAttributes | unknown} extraInput + * @returns {void} */ public info(input: LogItemMessage, ...extraInput: LogItemExtraInput): void { this.processLogItem('INFO', input, extraInput); } /** + * Method decorator that adds the current Lambda function context as extra + * information in all log items. + * The decorator can be used only when attached to a Lambda function handler which + * is written as method of a class, and should be declared just before the handler declaration. * + * @see https://www.typescriptlang.org/docs/handbook/decorators.html#method-decorators * @returns {HandlerMethodDecorator} */ public injectLambdaContext(): HandlerMethodDecorator { @@ -216,31 +238,46 @@ class Logger implements ClassThatLogs { } /** + * If the sample rate feature is enabled, the calculation that determines whether the logs + * will actually be printed or not for this invocation is done when the Logger class is + * initialized. + * This method will repeat that calculation (with possible different outcome). * + * @returns {void} */ public refreshSampleRateCalculation(): void { this.setLogsSampled(); } /** + * Sets the value of a flag static propriety that tracks whether + * the cold start evaluation already took place. * * @param {boolean} value + * @static + * @returns {void} */ public static setColdStartEvaluatedValue(value: boolean): void { Logger.coldStartEvaluated = value; } /** + * Sets the value of a flag static propriety that tracks whether + * the current Lambda invocation experienced a cold start. * * @param {boolean | undefined} value + * @static + * @returns {void} */ public static setColdStartValue(value: boolean | undefined): void { Logger.coldStart = value; } /** + * It sets the user-provided sample rate value. * * @param {number} sampleRateValue + * @returns {void} */ public setSampleRateValue(sampleRateValue?: number): void { this.powertoolLogData.sampleRateValue = @@ -250,18 +287,23 @@ class Logger implements ClassThatLogs { } /** + * It prints a log item with level WARN. * * @param {LogItemMessage} input * @param {Error | LogAttributes | unknown} extraInput + * @returns {void} */ public warn(input: LogItemMessage, ...extraInput: LogItemExtraInput): void { this.processLogItem('WARN', input, extraInput); } /** + * Adds information to a class propriety that contains information that + * is printed in all log items. * * @param {Partial} attributesArray * @private + * @returns {void} */ private addToPowertoolLogData(...attributesArray: Array>): void { attributesArray.forEach((attributes: Partial) => { @@ -270,14 +312,18 @@ class Logger implements ClassThatLogs { } /** + * It processes a particular log item so that it can be printed to stdout: + * - Merges ephemeral log attributes with persistent log attributes (printed for all logs) and additional info; + * - Formats all the log attributes; * + * @private * @param {LogLevel} logLevel * @param {LogItemMessage} input * @param {LogItemExtraInput} extraInput * @returns {LogItem} - * @private */ private createAndPopulateLogItem(logLevel: LogLevel, input: LogItemMessage, extraInput: LogItemExtraInput): LogItem { + // TODO: this method's logic is hard to understand, there is an opportunity here to simplify this logic. const unformattedBaseAttributes = merge({ logLevel, timestamp: new Date(), @@ -302,8 +348,12 @@ class Logger implements ClassThatLogs { } /** + * It evaluates whether the current Lambda invocation experienced a + * cold start. * * @private + * @static + * @returns {void} */ private static evaluateColdStart(): void { const coldStartValue = Logger.getColdStartValue(); @@ -319,34 +369,74 @@ class Logger implements ClassThatLogs { } /** + * It returns the custom config service, an abstraction used to fetch environment variables. * - * @returns {ConfigServiceInterface | undefined} * @private + * @returns {ConfigServiceInterface | undefined} */ private getCustomConfigService(): ConfigServiceInterface | undefined { return this.customConfigService; } + /** + * It returns the instance of a service that fetches environment variables. + * + * @private + * @returns {EnvironmentVariablesService} + */ private getEnvVarsService(): EnvironmentVariablesService { return this.envVarsService; } + /** + * It returns the instance of a service that formats the structure of a + * log item's keys and values in the desired way. + * + * @private + * @returns {LogFormatterInterface} + */ private getLogFormatter(): LogFormatterInterface { return this.logFormatter; } + /** + * It returns the log level set for the Logger instance. + * + * @private + * @returns {LogLevel} + */ private getLogLevel(): LogLevel { return this.logLevel; } + /** + * It returns the persistent log attributes, which are the attributes + * that will be logged in all log items. + * + * @private + * @returns {LogAttributes} + */ private getPersistentLogAttributes(): LogAttributes { return this.persistentLogAttributes; } + /** + * It returns information that will be added in all log item by + * this Logger instance (different from user-provided persistent attributes). + * + * @private + * @returns {LogAttributes} + */ private getPowertoolLogData(): PowertoolLogData { return this.powertoolLogData; } + /** + * It returns the numeric sample rate value. + * + * @private + * @returns {number} + */ private getSampleRateValue(): number { if (!this.powertoolLogData?.sampleRateValue) { this.setSampleRateValue(); @@ -355,10 +445,24 @@ class Logger implements ClassThatLogs { return this.powertoolLogData?.sampleRateValue; } + /** + * It returns true if the provided log level is valid. + * + * @param {LogLevel} logLevel + * @private + * @returns {boolean} + */ private isValidLogLevel(logLevel?: LogLevel): boolean { return typeof logLevel === 'string' && logLevel.toUpperCase() in this.logLevelThresholds; } + /** + * It prints a given log with given log level. + * + * @param {LogLevel} logLevel + * @param {LogItem} log + * @private + */ private printLog(logLevel: LogLevel, log: LogItem): void { log.prepareForPrint(); @@ -367,6 +471,13 @@ class Logger implements ClassThatLogs { console[consoleMethod](JSON.stringify(log.getAttributes(), this.removeCircularDependencies())); } + /** + * It prints a given log with given log level. + * + * @param {LogLevel} logLevel + * @param {LogItem} log + * @private + */ private processLogItem(logLevel: LogLevel, input: LogItemMessage, extraInput: LogItemExtraInput): void { if (!this.shouldPrint(logLevel)) { return; @@ -375,9 +486,9 @@ class Logger implements ClassThatLogs { } /** - * When the data added in the log item when contains object references, - * JSON.stringify() doesn't try to solve them and throws an error: TypeError: cyclic object value. - * To mitigate this issue, this function will find and remove the cyclic references. + * When the data added in the log item contains object references, + * JSON.stringify() doesn't try to solve them and instead throws an error: TypeError: cyclic object value. + * To mitigate this issue, this method will find and remove all cyclic references. * * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Cyclic_object_value * @private @@ -401,18 +512,49 @@ class Logger implements ClassThatLogs { }; } + /** + * Sets the Logger's customer config service instance, which will be used + * to fetch environment variables. + * + * @private + * @param {ConfigServiceInterface} customConfigService + * @returns {void} + */ private setCustomConfigService(customConfigService?: ConfigServiceInterface): void { this.customConfigService = customConfigService ? customConfigService : undefined; } + /** + * Sets the Logger's custom config service instance, which will be used + * to fetch environment variables. + * + * @private + * @param {ConfigServiceInterface} customConfigService + * @returns {void} + */ private setEnvVarsService(): void { this.envVarsService = new EnvironmentVariablesService(); } + /** + * It sets the log formatter instance, in charge of giving a custom format + * to the structured logs + * + * @private + * @param {LogFormatterInterface} logFormatter + * @returns {void} + */ private setLogFormatter(logFormatter?: LogFormatterInterface): void { this.logFormatter = logFormatter || new PowertoolLogFormatter(); } + /** + * It sets the Logger's instance log level. + * + * @private + * @param {LogLevel} logLevel + * @returns {void} + */ private setLogLevel(logLevel?: LogLevel): void { if (this.isValidLogLevel(logLevel)) { this.logLevel = (logLevel).toUpperCase(); @@ -435,12 +577,27 @@ class Logger implements ClassThatLogs { this.logLevel = Logger.defaultLogLevel; } + /** + * If the sample rate feature is enabled, it sets a propriety that tracks whether this Lambda function invocation + * will print logs or not. + * + * @private + * @returns {void} + */ private setLogsSampled(): void { const sampleRateValue = this.getSampleRateValue(); // TODO: revisit Math.random() as it's not a real randomization this.logsSampled = sampleRateValue !== undefined && (sampleRateValue === 1 || Math.random() < sampleRateValue); } + /** + * It configures the Logger instance settings that will affect the Logger's behaviour + * and the content of all logs. + * + * @private + * @param {LoggerOptions} options + * @returns {Logger} + */ private setOptions(options: LoggerOptions): Logger { const { logLevel, @@ -465,6 +622,15 @@ class Logger implements ClassThatLogs { return this; } + /** + * It adds important data to the Logger instance that will affect the content of all logs. + * + * @param {string} serviceName + * @param {Environment} environment + * @param {LogAttributes} persistentLogAttributes + * @private + * @returns {void} + */ private setPowertoolLogData( serviceName?: string, environment?: Environment, @@ -486,6 +652,15 @@ class Logger implements ClassThatLogs { ); } + /** + * It checks whether the current log item should/can be printed. + * + * @param {string} serviceName + * @param {Environment} environment + * @param {LogAttributes} persistentLogAttributes + * @private + * @returns {boolean} + */ private shouldPrint(logLevel: LogLevel): boolean { if (this.logLevelThresholds[logLevel] >= this.logLevelThresholds[this.getLogLevel()]) { return true; diff --git a/packages/logger/src/config/ConfigService.ts b/packages/logger/src/config/ConfigService.ts index 31663bc56c..4f38cf6ae6 100644 --- a/packages/logger/src/config/ConfigService.ts +++ b/packages/logger/src/config/ConfigService.ts @@ -1,21 +1,62 @@ import { ConfigServiceInterface } from '.'; +/** + * Abstract class ConfigService + * + * This class defines common methods and variables that can be set by the developer + * in the runtime. + * + * @class + * @abstract + * @implements {ConfigServiceInterface} + * @see https://docs.aws.amazon.com/lambda/latest/dg/configuration-envvars.html#configuration-envvars-runtime + * @see https://awslabs.github.io/aws-lambda-powertools-typescript/latest/#environment-variables + */ abstract class ConfigService implements ConfigServiceInterface { - // Custom environment variables + /** + * @see https://awslabs.github.io/aws-lambda-powertools-typescript/latest/#environment-variables + * @protected + */ protected currentEnvironmentVariable = 'ENVIRONMENT'; protected logLevelVariable = 'LOG_LEVEL'; protected sampleRateValueVariable = 'POWERTOOLS_LOGGER_SAMPLE_RATE'; protected serviceNameVariable = 'POWERTOOLS_SERVICE_NAME'; + /** + * It returns the value of an environment variable that has given name. + * + * @param {string} name + * @returns {string} + */ public abstract get(name: string): string; + /** + * It returns the value of the ENVIRONMENT environment variable. + * + * @returns {string} + */ public abstract getCurrentEnvironment(): string; + /** + * It returns the value of the LOG_LEVEL environment variable. + * + * @returns {string} + */ public abstract getLogLevel(): string; + /** + * It returns the value of the POWERTOOLS_LOGGER_SAMPLE_RATE environment variable. + * + * @returns {string|undefined} + */ public abstract getSampleRateValue(): number | undefined; + /** + * It returns the value of the POWERTOOLS_SERVICE_NAME environment variable. + * + * @returns {string} + */ public abstract getServiceName(): string; } diff --git a/packages/logger/src/config/ConfigServiceInterface.ts b/packages/logger/src/config/ConfigServiceInterface.ts index 3659acea66..0e9b62a409 100644 --- a/packages/logger/src/config/ConfigServiceInterface.ts +++ b/packages/logger/src/config/ConfigServiceInterface.ts @@ -1,13 +1,46 @@ +/** + * Interface ConfigServiceInterface + * + * @interface + * @see https://docs.aws.amazon.com/lambda/latest/dg/configuration-envvars.html#configuration-envvars-runtime + * @see https://awslabs.github.io/aws-lambda-powertools-typescript/latest/#environment-variables + */ interface ConfigServiceInterface { + /** + * It returns the value of an environment variable that has given name. + * + * @param {string} name + * @returns {string} + */ get(name: string): string + /** + * It returns the value of the ENVIRONMENT environment variable. + * + * @returns {string} + */ getCurrentEnvironment(): string + /** + * It returns the value of the LOG_LEVEL environment variable. + * + * @returns {string} + */ getLogLevel(): string + /** + * It returns the value of the POWERTOOLS_LOGGER_SAMPLE_RATE environment variable. + * + * @returns {string|undefined} + */ getSampleRateValue(): number | undefined + /** + * It returns the value of the POWERTOOLS_SERVICE_NAME environment variable. + * + * @returns {string} + */ getServiceName(): string } diff --git a/packages/logger/src/config/EnvironmentVariablesService.ts b/packages/logger/src/config/EnvironmentVariablesService.ts index 6553e93166..7c7c075bb7 100644 --- a/packages/logger/src/config/EnvironmentVariablesService.ts +++ b/packages/logger/src/config/EnvironmentVariablesService.ts @@ -1,5 +1,18 @@ import { ConfigService } from '.'; +/** + * Class EnvironmentVariablesService + * + * This class is used to return environment variables that are available in the runtime of + * the current Lambda invocation. + * These variables can be a mix of runtime environment variables set by AWS and + * variables that can be set by the developer additionally. + * + * @class + * @extends {ConfigService} + * @see https://docs.aws.amazon.com/lambda/latest/dg/configuration-envvars.html#configuration-envvars-runtime + * @see https://awslabs.github.io/aws-lambda-powertools-typescript/latest/#environment-variables + */ class EnvironmentVariablesService extends ConfigService { // Reserved environment variables @@ -9,46 +22,97 @@ class EnvironmentVariablesService extends ConfigService { private memoryLimitInMBVariable = 'AWS_LAMBDA_FUNCTION_MEMORY_SIZE'; private xRayTraceIdVariable = '_X_AMZN_TRACE_ID'; + /** + * It returns a the value of an environment variable that has given name. + * + * @param {string} name + * @returns {string} + */ public get(name: string): string { return process.env[name]?.trim() || ''; } + /** + * It returns the value of the AWS_REGION environment variable. + * + * @returns {string} + */ public getAwsRegion(): string { return this.get(this.awsRegionVariable); } + /** + * It returns the value of the ENVIRONMENT environment variable. + * + * @returns {string} + */ public getCurrentEnvironment(): string { return this.get(this.currentEnvironmentVariable); } + /** + * It returns the value of the AWS_LAMBDA_FUNCTION_MEMORY_SIZE environment variable. + * + * @returns {string} + */ public getFunctionMemory(): number { const value = this.get(this.memoryLimitInMBVariable); return Number(value); } + /** + * It returns the value of the AWS_LAMBDA_FUNCTION_NAME environment variable. + * + * @returns {string} + */ public getFunctionName(): string { return this.get(this.functionNameVariable); } + /** + * It returns the value of the AWS_LAMBDA_FUNCTION_VERSION environment variable. + * + * @returns {string} + */ public getFunctionVersion(): string { return this.get(this.functionVersionVariable); } + /** + * It returns the value of the LOG_LEVEL environment variable. + * + * @returns {string} + */ public getLogLevel(): string { return this.get(this.logLevelVariable); } + /** + * It returns the value of the POWERTOOLS_LOGGER_SAMPLE_RATE environment variable. + * + * @returns {string|undefined} + */ public getSampleRateValue(): number | undefined { const value = this.get(this.sampleRateValueVariable); return (value && value.length > 0) ? Number(value) : undefined; } + /** + * It returns the value of the POWERTOOLS_SERVICE_NAME environment variable. + * + * @returns {string} + */ public getServiceName(): string { return this.get(this.serviceNameVariable); } + /** + * It returns the value of the _X_AMZN_TRACE_ID environment variable. + * + * @returns {string} + */ public getXrayTraceId(): string { return this.get(this.xRayTraceIdVariable); } diff --git a/packages/logger/src/formatter/LogFormatter.ts b/packages/logger/src/formatter/LogFormatter.ts index 594e50cb7d..05fbb1300d 100644 --- a/packages/logger/src/formatter/LogFormatter.ts +++ b/packages/logger/src/formatter/LogFormatter.ts @@ -1,10 +1,31 @@ import { LogFormatterInterface } from '.'; import { LogAttributes, UnformattedAttributes } from '../types'; +/** + * Abstract class LogFormatter + * + * This class defines and implements common methods for the formatting of log attributes. + * + * @class + * @abstract + * @implements {LogFormatterInterface} + */ abstract class LogFormatter implements LogFormatterInterface { + /** + * It formats key-value pairs of log attributes. + * + * @param {UnformattedAttributes} attributes + * @returns {LogAttributes} + */ public abstract formatAttributes(attributes: UnformattedAttributes): LogAttributes; + /** + * It formats a given Error parameter. + * + * @param {Error} error + * @returns {LogAttributes} + */ public formatError(error: Error): LogAttributes { return { name: error.name, @@ -14,10 +35,22 @@ abstract class LogFormatter implements LogFormatterInterface { }; } + /** + * It formats a date into a string in simplified extended ISO format (ISO 8601). + * + * @param {Date} now + * @returns {string} + */ public formatTimestamp(now: Date): string { return now.toISOString(); } + /** + * It returns a string containing the location of an error, given a particular stack trace. + * + * @param stack + * @returns {string} + */ public getCodeLocation(stack?: string): string { if (!stack) { return ''; diff --git a/packages/logger/src/formatter/LogFormatterInterface.ts b/packages/logger/src/formatter/LogFormatterInterface.ts index 04e3b69e3f..2be8173c10 100644 --- a/packages/logger/src/formatter/LogFormatterInterface.ts +++ b/packages/logger/src/formatter/LogFormatterInterface.ts @@ -1,9 +1,26 @@ import { LogAttributes, UnformattedAttributes } from '../types'; +/** + * Interface LogFormatterInterface + * + * @interface + */ interface LogFormatterInterface { + /** + * It formats key-value pairs of log attributes. + * + * @param {UnformattedAttributes} attributes + * @returns {PowertoolLog} + */ formatAttributes(attributes: UnformattedAttributes): LogAttributes + /** + * It formats a given Error parameter. + * + * @param {Error} error + * @returns {LogAttributes} + */ formatError(error: Error): LogAttributes } diff --git a/packages/logger/src/formatter/PowertoolLogFormatter.ts b/packages/logger/src/formatter/PowertoolLogFormatter.ts index f29e1b5dd8..c4bd7bc34b 100644 --- a/packages/logger/src/formatter/PowertoolLogFormatter.ts +++ b/packages/logger/src/formatter/PowertoolLogFormatter.ts @@ -2,8 +2,21 @@ import { LogFormatter } from '.'; import { UnformattedAttributes } from '../types'; import { PowertoolLog } from '../types/formats'; +/** + * This class is used to transform a set of log key-value pairs + * in the AWS Lambda Powertools' default structure log format. + * + * @class + * @extends {LogFormatter} + */ class PowertoolLogFormatter extends LogFormatter { + /** + * It formats key-value pairs of log attributes. + * + * @param {UnformattedAttributes} attributes + * @returns {PowertoolLog} + */ public formatAttributes(attributes: UnformattedAttributes): PowertoolLog { return { cold_start: attributes.lambdaContext?.coldStart, diff --git a/packages/logger/src/middleware/middy.ts b/packages/logger/src/middleware/middy.ts index e06b19e156..cc5b2e54bb 100644 --- a/packages/logger/src/middleware/middy.ts +++ b/packages/logger/src/middleware/middy.ts @@ -1,6 +1,13 @@ import type { Logger } from '../Logger'; import middy from '@middy/core'; +/** + * Middy middleware that adds the current Lambda invocation's context + * inside all log items. + * + * @param {Logger|Logger[]} target + * @returns middy.MiddlewareObj + */ const injectLambdaContext = (target: Logger | Logger[]): middy.MiddlewareObj => { const injectLambdaContextBefore = async (request: middy.Request): Promise => { const loggers = target instanceof Array ? target : [target]; From acc172a243fd2bed0516e1e6ae3fe3cc077ec200 Mon Sep 17 00:00:00 2001 From: Sara Gerion Date: Wed, 19 Jan 2022 22:08:03 +0100 Subject: [PATCH 03/12] fix(logger): jsdocs finetuning --- packages/logger/src/Logger.ts | 29 +++++++++++------------------ 1 file changed, 11 insertions(+), 18 deletions(-) diff --git a/packages/logger/src/Logger.ts b/packages/logger/src/Logger.ts index 4ac0faa269..e004e59f25 100644 --- a/packages/logger/src/Logger.ts +++ b/packages/logger/src/Logger.ts @@ -44,10 +44,6 @@ import type { */ class Logger implements ClassThatLogs { - /** - * - * @private - */ private static coldStart?: boolean = undefined; private static coldStartEvaluated: boolean = false; @@ -87,7 +83,7 @@ class Logger implements ClassThatLogs { } /** - * Adds the current AWS Lambda's invocation context data to the powertoolLogData propriety of the instance. + * It adds the current Lambda function's invocation context data to the powertoolLogData propriety of the instance. * This context data will be part of all printed log items. * * @param {Context} context @@ -110,7 +106,7 @@ class Logger implements ClassThatLogs { } /** - * Adds given attributes (key-value pairs) to all log items generated by this Logger instance. + * it adds the given attributes (key-value pairs) to all log items generated by this Logger instance. * * @param {LogAttributes} attributes * @returns {void} @@ -130,7 +126,7 @@ class Logger implements ClassThatLogs { } /** - * Creates a separate Logger instance, identical to the current one. + * it creates a separate Logger instance, identical to the current one. * It's possible to overwrite the new instance options by passing them. * * @param {LoggerOptions} options @@ -175,8 +171,7 @@ class Logger implements ClassThatLogs { } /** - * It returns a boolean true value if the current Lambda function invocation has been already evaluated to - * determine if it's a cold start, false if not. + * It returns a boolean value which is true if the current Lambda function cold start has been already evaluated, false if not. * * @static * @returns {boolean} @@ -186,7 +181,7 @@ class Logger implements ClassThatLogs { } /** - * It returns a boolean true value if the current Lambda function invocation has a cold start, false otherwise. + * It returns an optional boolean value, true if the current Lambda function invocation has a cold start, false otherwise. * * @static * @returns {boolean | undefined} @@ -196,8 +191,7 @@ class Logger implements ClassThatLogs { } /** - * It returns a boolean true value if, in case of sample rating feature enabled, the current - * Lambda function invocation's logs will be printed to stdout. It returns false otherwise. + * It returns a boolean value, if true all the logs will be printed. * * @returns {boolean} */ @@ -250,7 +244,7 @@ class Logger implements ClassThatLogs { } /** - * Sets the value of a flag static propriety that tracks whether + * It sets the value of a flag static propriety that tracks whether * the cold start evaluation already took place. * * @param {boolean} value @@ -262,11 +256,11 @@ class Logger implements ClassThatLogs { } /** - * Sets the value of a flag static propriety that tracks whether + * It sets the value of a flag static propriety that tracks whether * the current Lambda invocation experienced a cold start. * - * @param {boolean | undefined} value * @static + * @param {boolean | undefined} value * @returns {void} */ public static setColdStartValue(value: boolean | undefined): void { @@ -276,7 +270,7 @@ class Logger implements ClassThatLogs { /** * It sets the user-provided sample rate value. * - * @param {number} sampleRateValue + * @param {number} [sampleRateValue] * @returns {void} */ public setSampleRateValue(sampleRateValue?: number): void { @@ -298,8 +292,7 @@ class Logger implements ClassThatLogs { } /** - * Adds information to a class propriety that contains information that - * is printed in all log items. + * It stored information that is printed in all log items. * * @param {Partial} attributesArray * @private From 85ef8e68724e58a1707a10fcd68a18d46bde7535 Mon Sep 17 00:00:00 2001 From: Sara Gerion Date: Wed, 19 Jan 2022 23:01:18 +0100 Subject: [PATCH 04/12] fix(logger): removed trailing eslint jsdocs flag --- .eslintrc.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.eslintrc.js b/.eslintrc.js index a8cbf8ceef..b83ce24ffc 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -7,7 +7,7 @@ module.exports = { }, extends: [ 'plugin:@typescript-eslint/eslint-recommended', 'plugin:@typescript-eslint/recommended' ], parser: '@typescript-eslint/parser', - plugins: ['@typescript-eslint', 'jsdoc'], + plugins: ['@typescript-eslint'], settings: { 'import/resolver': { node: {}, From 4e3a532966f9c77bb4d260c8e042becd5eeff05e Mon Sep 17 00:00:00 2001 From: Sara Gerion <47529391+saragerion@users.noreply.github.com> Date: Thu, 20 Jan 2022 09:26:10 +0100 Subject: [PATCH 05/12] Update packages/logger/src/config/EnvironmentVariablesService.ts Co-authored-by: Florian Chazal --- packages/logger/src/config/EnvironmentVariablesService.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/logger/src/config/EnvironmentVariablesService.ts b/packages/logger/src/config/EnvironmentVariablesService.ts index 7c7c075bb7..588af64a01 100644 --- a/packages/logger/src/config/EnvironmentVariablesService.ts +++ b/packages/logger/src/config/EnvironmentVariablesService.ts @@ -23,7 +23,7 @@ class EnvironmentVariablesService extends ConfigService { private xRayTraceIdVariable = '_X_AMZN_TRACE_ID'; /** - * It returns a the value of an environment variable that has given name. + * It returns the value of an environment variable that has given name. * * @param {string} name * @returns {string} From 10e07d7a86b6dd6df5050d80b88a8132ec325259 Mon Sep 17 00:00:00 2001 From: Sara Gerion <47529391+saragerion@users.noreply.github.com> Date: Thu, 20 Jan 2022 10:25:20 +0100 Subject: [PATCH 06/12] Update packages/logger/src/Logger.ts Co-authored-by: Andrea Amorosi --- packages/logger/src/Logger.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/logger/src/Logger.ts b/packages/logger/src/Logger.ts index e004e59f25..f10590989d 100644 --- a/packages/logger/src/Logger.ts +++ b/packages/logger/src/Logger.ts @@ -83,7 +83,7 @@ class Logger implements ClassThatLogs { } /** - * It adds the current Lambda function's invocation context data to the powertoolLogData propriety of the instance. + * It adds the current Lambda function's invocation context data to the powertoolLogData property of the instance. * This context data will be part of all printed log items. * * @param {Context} context From 9e3452dfd0e9081eb9cc274afc97736c30562fbf Mon Sep 17 00:00:00 2001 From: Sara Gerion <47529391+saragerion@users.noreply.github.com> Date: Thu, 20 Jan 2022 10:25:47 +0100 Subject: [PATCH 07/12] Update packages/logger/src/Logger.ts Co-authored-by: Andrea Amorosi --- packages/logger/src/Logger.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/logger/src/Logger.ts b/packages/logger/src/Logger.ts index f10590989d..ef5a1ba8e3 100644 --- a/packages/logger/src/Logger.ts +++ b/packages/logger/src/Logger.ts @@ -106,7 +106,7 @@ class Logger implements ClassThatLogs { } /** - * it adds the given attributes (key-value pairs) to all log items generated by this Logger instance. + * It adds the given attributes (key-value pairs) to all log items generated by this Logger instance. * * @param {LogAttributes} attributes * @returns {void} From c95159f9aaa2b14c64634927fdf37ea4e2cd81a9 Mon Sep 17 00:00:00 2001 From: Sara Gerion <47529391+saragerion@users.noreply.github.com> Date: Thu, 20 Jan 2022 10:25:53 +0100 Subject: [PATCH 08/12] Update packages/logger/src/Logger.ts Co-authored-by: Andrea Amorosi --- packages/logger/src/Logger.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/logger/src/Logger.ts b/packages/logger/src/Logger.ts index ef5a1ba8e3..4b79686a4c 100644 --- a/packages/logger/src/Logger.ts +++ b/packages/logger/src/Logger.ts @@ -126,7 +126,7 @@ class Logger implements ClassThatLogs { } /** - * it creates a separate Logger instance, identical to the current one. + * It creates a separate Logger instance, identical to the current one * It's possible to overwrite the new instance options by passing them. * * @param {LoggerOptions} options From 81096ca4c157e5ab0fd449ed50930bc03d544c16 Mon Sep 17 00:00:00 2001 From: Sara Gerion <47529391+saragerion@users.noreply.github.com> Date: Thu, 20 Jan 2022 10:26:02 +0100 Subject: [PATCH 09/12] Update packages/logger/src/Logger.ts Co-authored-by: Andrea Amorosi --- packages/logger/src/Logger.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/logger/src/Logger.ts b/packages/logger/src/Logger.ts index 4b79686a4c..f4f54af332 100644 --- a/packages/logger/src/Logger.ts +++ b/packages/logger/src/Logger.ts @@ -171,7 +171,7 @@ class Logger implements ClassThatLogs { } /** - * It returns a boolean value which is true if the current Lambda function cold start has been already evaluated, false if not. + * It returns a boolean value which is true if the current Lambda function cold start has been already evaluated, false otherwise. * * @static * @returns {boolean} From 61e105313bd1e8f9a585ef31087bd0398af36d9f Mon Sep 17 00:00:00 2001 From: Sara Gerion <47529391+saragerion@users.noreply.github.com> Date: Thu, 20 Jan 2022 10:26:21 +0100 Subject: [PATCH 10/12] Update packages/logger/src/Logger.ts Co-authored-by: Andrea Amorosi --- packages/logger/src/Logger.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/logger/src/Logger.ts b/packages/logger/src/Logger.ts index f4f54af332..918e6971b9 100644 --- a/packages/logger/src/Logger.ts +++ b/packages/logger/src/Logger.ts @@ -292,7 +292,7 @@ class Logger implements ClassThatLogs { } /** - * It stored information that is printed in all log items. + * It stores information that is printed in all log items. * * @param {Partial} attributesArray * @private From 637759b59a5c01dc7263714d1dcd691afd4574da Mon Sep 17 00:00:00 2001 From: Sara Gerion <47529391+saragerion@users.noreply.github.com> Date: Thu, 20 Jan 2022 10:26:26 +0100 Subject: [PATCH 11/12] Update packages/logger/src/Logger.ts Co-authored-by: Andrea Amorosi --- packages/logger/src/Logger.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/logger/src/Logger.ts b/packages/logger/src/Logger.ts index 918e6971b9..f2f12670cf 100644 --- a/packages/logger/src/Logger.ts +++ b/packages/logger/src/Logger.ts @@ -571,7 +571,7 @@ class Logger implements ClassThatLogs { } /** - * If the sample rate feature is enabled, it sets a propriety that tracks whether this Lambda function invocation + * If the sample rate feature is enabled, it sets a property that tracks whether this Lambda function invocation * will print logs or not. * * @private From 1472f7167988af7b998b2bba6d50bc3c32ab930f Mon Sep 17 00:00:00 2001 From: Sara Gerion Date: Thu, 20 Jan 2022 11:28:53 +0100 Subject: [PATCH 12/12] docs(logger): improve example for jsdocs --- .../example-function.MyFunctionWithMiddy.ts | 2 +- packages/logger/src/Logger.ts | 80 ++++++++++++++++--- packages/logger/src/formatter/LogFormatter.ts | 2 - .../src/formatter/LogFormatterInterface.ts | 2 - packages/logger/src/middleware/middy.ts | 26 +++++- packages/tracing/src/Tracer.ts | 2 +- packages/tracing/tests/unit/middy.test.ts | 2 +- 7 files changed, 96 insertions(+), 20 deletions(-) diff --git a/examples/cdk/src/example-function.MyFunctionWithMiddy.ts b/examples/cdk/src/example-function.MyFunctionWithMiddy.ts index c2e6f8d444..6b743512aa 100644 --- a/examples/cdk/src/example-function.MyFunctionWithMiddy.ts +++ b/examples/cdk/src/example-function.MyFunctionWithMiddy.ts @@ -62,7 +62,7 @@ const lambdaHandler = async (event: typeof Events.Custom.CustomEvent, context: C return res; }; -// We instrument the handler with the various Middy middlewares +// We instrument the handler with the various Middy middleware export const handler = middy(lambdaHandler) .use(captureLambdaHandler(tracer)) .use( diff --git a/packages/logger/src/Logger.ts b/packages/logger/src/Logger.ts index 3353b8fb17..cd846a010c 100644 --- a/packages/logger/src/Logger.ts +++ b/packages/logger/src/Logger.ts @@ -19,25 +19,87 @@ import type { } from './types'; /** - * - * # Logger class - * * ## Intro - * * The Logger utility provides an opinionated logger with output structured as JSON. * * ## Key features + * * Capture key fields from Lambda context, cold start and structures logging output as JSON + * * Log Lambda context when instructed (disabled by default) + * * Log sampling prints all logs for a percentage of invocations (disabled by default) + * * Append additional keys to structured log at any point in time + * + * ## Usage + * + * For more usage examples, see [our documentation](https://awslabs.github.io/aws-lambda-powertools-typescript/latest/core/logger/). + * + * ### Basic usage + * + * @example + * ```typescript + * import { Logger } from "@aws-lambda-powertools/logger"; + * + * // Logger parameters fetched from the environment variables: + * const logger = new Logger(); + * ``` * - * - Capture key fields from Lambda context, cold start and structures logging output as JSON - * - Log Lambda context when instructed (disabled by default) - * - Log sampling prints all logs for a percentage of invocations (disabled by default) - * - Append additional keys to structured log at any point in time + * ### Functions usage with manual instrumentation * - * ## Basic usage + * If you prefer to manually instrument your Lambda handler you can use the methods in the Logger class directly. * + * @example + * ```typescript * import { Logger } from "@aws-lambda-powertools/logger"; + * * const logger = new Logger(); * + * export const handler = async (_event, context) => { + * logger.addContext(context); + * logger.info("This is an INFO log with some context"); + * }; + * ``` + * + * ### Functions usage with middleware + * + * If you use function-based Lambda handlers you can use the [injectLambdaContext()](#injectLambdaContext) + * middy middleware to automatically add context to your Lambda logs. + * + * @example + * ```typescript + * import { Logger, injectLambdaContext } from "@aws-lambda-powertools/logger"; + * import middy from '@middy/core'; + * + * const logger = new Logger(); + * + * const lambdaHandler = async (_event: any, _context: any) => { + * logger.info("This is an INFO log with some context"); + * }; + * + * export const handler = middy(lambdaHandler).use(injectLambdaContext(logger)); + * ``` + * + * ### Object oriented usage with decorators + * + * If instead you use TypeScript classes to wrap your Lambda handler you can use the [@logger.injectLambdaContext()](./_aws_lambda_powertools_logger.Logger.html#injectLambdaContext) decorator. + * + * @example + * ```typescript + * import { Logger } from "@aws-lambda-powertools/logger"; + * import { LambdaInterface } from '@aws-lambda-powertools/commons'; + * + * const logger = new Logger(); + * + * class Lambda implements LambdaInterface { + * // Decorate your handler class method + * @logger.injectLambdaContext() + * public async handler(_event: any, _context: any): Promise { + * logger.info("This is an INFO log with some context"); + * } + * } + * + * export const myFunction = new Lambda(); + * export const handler = myFunction.handler; + * ``` + * * @class * @implements {ClassThatLogs} * @see https://awslabs.github.io/aws-lambda-powertools-typescript/latest/core/logger/ diff --git a/packages/logger/src/formatter/LogFormatter.ts b/packages/logger/src/formatter/LogFormatter.ts index 05fbb1300d..29b47f99fd 100644 --- a/packages/logger/src/formatter/LogFormatter.ts +++ b/packages/logger/src/formatter/LogFormatter.ts @@ -2,8 +2,6 @@ import { LogFormatterInterface } from '.'; import { LogAttributes, UnformattedAttributes } from '../types'; /** - * Abstract class LogFormatter - * * This class defines and implements common methods for the formatting of log attributes. * * @class diff --git a/packages/logger/src/formatter/LogFormatterInterface.ts b/packages/logger/src/formatter/LogFormatterInterface.ts index 2be8173c10..1139443c28 100644 --- a/packages/logger/src/formatter/LogFormatterInterface.ts +++ b/packages/logger/src/formatter/LogFormatterInterface.ts @@ -1,8 +1,6 @@ import { LogAttributes, UnformattedAttributes } from '../types'; /** - * Interface LogFormatterInterface - * * @interface */ interface LogFormatterInterface { diff --git a/packages/logger/src/middleware/middy.ts b/packages/logger/src/middleware/middy.ts index cc5b2e54bb..d0353962a3 100644 --- a/packages/logger/src/middleware/middy.ts +++ b/packages/logger/src/middleware/middy.ts @@ -2,11 +2,29 @@ import type { Logger } from '../Logger'; import middy from '@middy/core'; /** - * Middy middleware that adds the current Lambda invocation's context - * inside all log items. + * A middy middleware that adds the current Lambda invocation's context inside all log items. * - * @param {Logger|Logger[]} target - * @returns middy.MiddlewareObj + * ## Usage + * + * @example + * ```typescript + * import { Logger, injectLambdaContext } from "@aws-lambda-powertools/logger"; + * + * import middy from '@middy/core'; + * + * + * const logger = new Logger(); + * + * const lambdaHandler = async (_event: any, _context: any) => { + * logger.info("This is an INFO log with some context"); + * }; + * + * + * export const handler = middy(lambdaHandler).use(injectLambdaContext(logger)); + * ``` + * + * @param {Logger|Logger[]} target - The Tracer instance to use for tracing + * @returns {middy.MiddlewareObj} - The middy middleware object */ const injectLambdaContext = (target: Logger | Logger[]): middy.MiddlewareObj => { const injectLambdaContextBefore = async (request: middy.Request): Promise => { diff --git a/packages/tracing/src/Tracer.ts b/packages/tracing/src/Tracer.ts index 12e9669aed..8ac49de38e 100644 --- a/packages/tracing/src/Tracer.ts +++ b/packages/tracing/src/Tracer.ts @@ -21,7 +21,7 @@ import { Segment, Subsegment } from 'aws-xray-sdk-core'; * * For more usage examples, see [our documentation](https://awslabs.github.io/aws-lambda-powertools-typescript/latest/core/tracer/). * - * ### Functions usage with middlewares + * ### Functions usage with middleware * * If you use function-based Lambda handlers you can use the [captureLambdaHandler()](./_aws_lambda_powertools_tracer.Tracer.html) middy middleware to automatically: * * handle the subsegment lifecycle diff --git a/packages/tracing/tests/unit/middy.test.ts b/packages/tracing/tests/unit/middy.test.ts index 0bf588c522..5829ba25cd 100644 --- a/packages/tracing/tests/unit/middy.test.ts +++ b/packages/tracing/tests/unit/middy.test.ts @@ -14,7 +14,7 @@ jest.spyOn(console, 'debug').mockImplementation(() => null); jest.spyOn(console, 'warn').mockImplementation(() => null); jest.spyOn(console, 'error').mockImplementation(() => null); -describe('Middy middlewares', () => { +describe('Middy middleware', () => { const ENVIRONMENT_VARIABLES = process.env; const context = { callbackWaitsForEmptyEventLoop: true,