Skip to content

feat!: version 2, the ESM update #2117

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 74 commits into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
74 commits
Select commit Hold shift + click to select a range
bc68af2
feat(logger): Support for external observability providers (#1511)
erikayao93 Jun 30, 2023
d7d3f19
chore(logger): PowertoolsLogFormatter docstring and variable naming u…
erikayao93 Jul 7, 2023
7e8cf2c
feat(logger): Support for external observability providers (#1511)
erikayao93 Jun 30, 2023
9368331
chore(logger): PowertoolsLogFormatter docstring and variable naming u…
erikayao93 Jul 7, 2023
a087819
chore(maintenance): bump dependencies & drop nodejs14x (#1687)
dreamorosi Sep 16, 2023
2ed10ed
chore: update release script to mark all utilities as alpha
dreamorosi Sep 18, 2023
a0122e3
chore: restore version to ease conflicts
dreamorosi Sep 27, 2023
e423e8b
chore: release version change
dreamorosi Sep 27, 2023
72ec0b6
chore: release version change
dreamorosi Sep 27, 2023
5c8b2b1
chore(maintenance): remove `createLogger` and `createTracer` helpers …
dreamorosi Oct 2, 2023
8629016
feat(logger): add esmodule support (#1734)
antstanley Oct 11, 2023
3977c60
feat(commons): add esmodule support (#1735)
dreamorosi Oct 11, 2023
3ba9d43
feat(parameters): add esmodule support (#1736)
dreamorosi Oct 12, 2023
4ee57bd
feat(batch): add esmodule support (#1737)
dreamorosi Oct 12, 2023
2ff1026
feat(internal): add esmodule support (#1738)
dreamorosi Oct 12, 2023
57e7b67
feat(metrics): add esmodule support (#1739)
dreamorosi Oct 12, 2023
793ef7f
feat(tracer): add esmodule support (#1741)
dreamorosi Oct 12, 2023
7331038
feat(idempotency): add esmodule support (#1743)
dreamorosi Oct 12, 2023
ae75a4a
chore(metrics): fix import
dreamorosi Oct 14, 2023
a0a973b
chore(ci): v2 release line
dreamorosi Oct 14, 2023
3bf0b01
chore(ci): fix alpha versioning pre-release
dreamorosi Oct 15, 2023
6788a84
docs(maintenance): add processes tab (#1747)
dreamorosi Oct 18, 2023
2c9433b
chore(docs): add invisible unicode char to decorator docs (#1755)
dreamorosi Oct 18, 2023
a31c44c
chore: remove extra comma
dreamorosi Oct 18, 2023
cd226ae
chore(docs): upgrade doc intro
dreamorosi Oct 19, 2023
7f0becd
feat(logger): align sampling debug logs feature implementation with t…
shdq Oct 19, 2023
b4568f9
chore(logger): refactor types and interfaces (#1758)
dreamorosi Oct 20, 2023
138162d
chore(maintenance): bump Middy v4 & run tests (#1760)
dreamorosi Oct 20, 2023
37e13bd
chore(parameters): fix esm bundling
dreamorosi Oct 21, 2023
902a07c
chore(parameters): change declare client param
dreamorosi Oct 21, 2023
da3a1c4
chore(layers) widen version check in e2e
dreamorosi Oct 21, 2023
ab01025
chore(maintenance): enable `isolatedModules` and isolate cache (#1765)
dreamorosi Oct 23, 2023
33d837e
fix: idempotency types
dreamorosi Oct 30, 2023
5f41c07
build(maintenance): bump aws sdk dev dependencies
dreamorosi Oct 31, 2023
cf229ab
chore(parameters): add export types
dreamorosi Oct 31, 2023
bec45c5
chore(logger): set default UTC timezone (#1775)
dreamorosi Nov 3, 2023
748d3c9
fix(metrics): deduplicate dimensions when serialising (#1780)
am29d Nov 2, 2023
948543d
chore(release): v1.14.2 [skip ci]
github-actions[bot] Nov 3, 2023
f0dd4c0
feat(metrics): log directly to stdout (#1786)
dreamorosi Nov 10, 2023
3453106
feat(maintenance): add support for nodejs20.x runtime (#1790)
dreamorosi Nov 14, 2023
e8c8e28
chore(commons): simplify config service interface
dreamorosi Nov 20, 2023
0ed2ed1
chore(release): v1.15.0 [skip ci]
github-actions[bot] Nov 14, 2023
74e94d4
feat(logger): add support for `AWS_LAMBDA_LOG_LEVEL` and `POWERTOOLS_…
dreamorosi Nov 16, 2023
dba2923
chore(release): v1.16.0 [skip ci]
github-actions[bot] Nov 16, 2023
c06d4b6
chore(maintenance): drop support for Node.js 14 (#1802)
dreamorosi Nov 24, 2023
24b8e08
chore: package lock
dreamorosi Nov 24, 2023
4ba4cd0
chore(docs): update docs url in comments & readme files (#1728)
dreamorosi Oct 9, 2023
ea55aa9
chore(ci): update v2 release workflow (#1745)
dreamorosi Oct 14, 2023
01cfa22
docs(maintenance): add processes tab (#1747)
dreamorosi Oct 18, 2023
875df2c
chore(maintenance): add --require-hashes flag to pip installs (#1827)
dreamorosi Dec 20, 2023
af5f262
bump version to 9.5.2, rerun pip-compile with correct deps (#1830)
am29d Dec 21, 2023
802abee
chore(ci): Dependabot fine tuning (#1862)
am29d Jan 9, 2024
0f81306
chore(deps-dev): bump @aws-sdk/client-cloudwatch from 3.438.0 to 3.48…
dependabot[bot] Jan 9, 2024
51bc6f4
chore(deps-dev): bump @types/node from 20.11.0 to 20.11.2 (#1912)
dependabot[bot] Jan 15, 2024
c5d2721
chore(deps): fix dependencies and dependabot config (#1917)
dreamorosi Jan 16, 2024
c92b2d1
chore(deps-dev): bump @typescript-eslint/parser from 6.19.0 to 6.19.1…
dependabot[bot] Jan 25, 2024
3b99b97
chore(deps-dev): bump @typescript-eslint/eslint-plugin (#1948)
dependabot[bot] Jan 25, 2024
05d03a4
chore(ci): refactor workflows to scope permissions (#1978)
dreamorosi Jan 26, 2024
85eb1ac
chore(maintenance): rebase conflicts
dreamorosi Jan 27, 2024
d51b9c7
chore(maintenance): rebase conflicts
dreamorosi Jan 27, 2024
ad1d98d
chore: rebase conflicts
dreamorosi Feb 20, 2024
c9ae783
chore: remove v2 specific release
dreamorosi Feb 20, 2024
4085298
chore: release patch script
dreamorosi Feb 20, 2024
b5bc335
docs: update docs & snippets
dreamorosi Feb 20, 2024
409fec2
chore: address SonarCloud findings
dreamorosi Feb 20, 2024
3f972eb
chore: address SonarCloud findings
dreamorosi Feb 20, 2024
5b5c7a2
chore: address SonarCloud findings
dreamorosi Feb 20, 2024
0df2691
tests: coverage
dreamorosi Feb 20, 2024
3f7fa9b
chore: add layers v2
dreamorosi Feb 21, 2024
61ce4cb
chore: bump ua version
dreamorosi Feb 21, 2024
93dc33f
docs: add install steps to batch
dreamorosi Feb 21, 2024
e6afb55
chore: fix install command
dreamorosi Feb 22, 2024
acb17b5
chore: remove unused dependency
dreamorosi Feb 22, 2024
0c01873
Update examples/cdk/package.json
dreamorosi Feb 27, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
chore(logger): refactor types and interfaces (#1758)
* chore(logger): refactor types and interfaces

* chore: grouped type files

* chore: fix code smell

* chore: fix ci

* chore: fix ci
  • Loading branch information
dreamorosi committed Feb 20, 2024
commit b4568f9910bc617106dca006dafa8346b78fb9bc
183 changes: 90 additions & 93 deletions packages/logger/src/Logger.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,32 @@
import { randomInt } from 'node:crypto';
import { Console } from 'node:console';
import { format } from 'node:util';
import type { Context, Handler } from 'aws-lambda';
import { Utility } from '@aws-lambda-powertools/commons';
import { PowertoolsLogFormatter } from './formatter/PowertoolsLogFormatter.js';
import { LogFormatterInterface } from './formatter/LogFormatterInterface.js';
import { LogItem } from './log/LogItem.js';
import type { HandlerMethodDecorator } from '@aws-lambda-powertools/commons/types';
import type { Context, Handler } from 'aws-lambda';
import merge from 'lodash.merge';
import { ConfigServiceInterface } from './config/ConfigServiceInterface.js';
import { Console } from 'node:console';
import { format } from 'node:util';
import { randomInt } from 'node:crypto';
import { EnvironmentVariablesService } from './config/EnvironmentVariablesService.js';
import { LogJsonIndent } from './types/Logger.js';
import { LogJsonIndent } from './constants.js';
import { LogItem } from './formatter/LogItem.js';
import { PowertoolsLogFormatter } from './formatter/PowertoolsLogFormatter.js';
import type { ConfigServiceInterface } from './types/ConfigServiceInterface.js';
import type {
Environment,
LogAttributes,
LogLevel,
LogLevelThresholds,
LogFormatterInterface,
} from './types/Log.js';
import type {
ClassThatLogs,
HandlerMethodDecorator,
LambdaFunctionContext,
LogFunction,
ConstructorOptions,
InjectLambdaContextOptions,
LogItemExtraInput,
LogItemMessage,
PowertoolLogData,
HandlerOptions,
LoggerInterface,
PowertoolsLogData,
} from './types/Logger.js';

/**
* ## Intro
* The Logger utility provides an opinionated logger with output structured as JSON.
Expand Down Expand Up @@ -112,7 +113,7 @@ import type {
* @implements {ClassThatLogs}
* @see https://docs.powertools.aws.dev/lambda/typescript/latest/core/logger/
*/
class Logger extends Utility implements ClassThatLogs {
class Logger extends Utility implements LoggerInterface {
/**
* Console instance used to print logs.
*
Expand Down Expand Up @@ -156,9 +157,9 @@ class Logger extends Utility implements ClassThatLogs {
SILENT: 28,
};

private persistentLogAttributes?: LogAttributes = {};
private persistentLogAttributes: LogAttributes = {};

private powertoolLogData: PowertoolLogData = <PowertoolLogData>{};
private powertoolsLogData: PowertoolsLogData = <PowertoolsLogData>{};

/**
* Log level used by the current instance of Logger.
Expand Down Expand Up @@ -188,17 +189,15 @@ class Logger extends Utility implements ClassThatLogs {
* @returns {void}
*/
public addContext(context: Context): void {
const lambdaContext: Partial<LambdaFunctionContext> = {
invokedFunctionArn: context.invokedFunctionArn,
coldStart: this.getColdStart(),
awsRequestId: context.awsRequestId,
memoryLimitInMB: Number(context.memoryLimitInMB),
functionName: context.functionName,
functionVersion: context.functionVersion,
};

this.addToPowertoolLogData({
lambdaContext,
this.addToPowertoolsLogData({
lambdaContext: {
invokedFunctionArn: context.invokedFunctionArn,
coldStart: this.getColdStart(),
awsRequestId: context.awsRequestId,
memoryLimitInMB: context.memoryLimitInMB,
functionName: context.functionName,
functionVersion: context.functionVersion,
},
});
}

Expand Down Expand Up @@ -230,23 +229,27 @@ class Logger extends Utility implements ClassThatLogs {
* @returns {Logger}
*/
public createChild(options: ConstructorOptions = {}): Logger {
const parentsOptions = {
logLevel: this.getLevelName(),
customConfigService: this.getCustomConfigService(),
logFormatter: this.getLogFormatter(),
sampleRateValue: this.powertoolLogData.sampleRateValue,
};
const parentsPowertoolsLogData = this.getPowertoolLogData();
const childLogger = this.createLogger(
merge(parentsOptions, parentsPowertoolsLogData, options)
// Merge parent logger options with options passed to createChild,
// the latter having precedence.
merge(
{},
{
logLevel: this.getLevelName(),
serviceName: this.powertoolsLogData.serviceName,
sampleRateValue: this.powertoolsLogData.sampleRateValue,
logFormatter: this.getLogFormatter(),
customConfigService: this.getCustomConfigService(),
environment: this.powertoolsLogData.environment,
persistentLogAttributes: this.persistentLogAttributes,
},
options
)
);

const parentsPersistentLogAttributes = this.getPersistentLogAttributes();
childLogger.addPersistentLogAttributes(parentsPersistentLogAttributes);

if (parentsPowertoolsLogData.lambdaContext) {
childLogger.addContext(parentsPowertoolsLogData.lambdaContext as Context);
}
if (this.powertoolsLogData.lambdaContext)
childLogger.addContext(
this.powertoolsLogData.lambdaContext as unknown as Context
);

return childLogger;
}
Expand Down Expand Up @@ -316,7 +319,7 @@ class Logger extends Utility implements ClassThatLogs {
* @returns {LogAttributes}
*/
public getPersistentLogAttributes(): LogAttributes {
return this.persistentLogAttributes as LogAttributes;
return this.persistentLogAttributes;
}

/**
Expand Down Expand Up @@ -362,7 +365,9 @@ class Logger extends Utility implements ClassThatLogs {
* @see https://www.typescriptlang.org/docs/handbook/decorators.html#method-decorators
* @returns {HandlerMethodDecorator}
*/
public injectLambdaContext(options?: HandlerOptions): HandlerMethodDecorator {
public injectLambdaContext(
options?: InjectLambdaContextOptions
): HandlerMethodDecorator {
return (_target, _propertyKey, descriptor) => {
/**
* The descriptor.value is the method this decorator decorates, it cannot be undefined.
Expand Down Expand Up @@ -410,7 +415,7 @@ class Logger extends Utility implements ClassThatLogs {
public static injectLambdaContextAfterOrOnError(
logger: Logger,
initialPersistentAttributes: LogAttributes,
options?: HandlerOptions
options?: InjectLambdaContextOptions
): void {
if (options && options.clearState === true) {
logger.setPersistentLogAttributes(initialPersistentAttributes);
Expand All @@ -421,13 +426,13 @@ class Logger extends Utility implements ClassThatLogs {
logger: Logger,
event: unknown,
context: Context,
options?: HandlerOptions
options?: InjectLambdaContextOptions
): void {
logger.addContext(context);

let shouldLogEvent = undefined;
if (options && options.hasOwnProperty('logEvent')) {
shouldLogEvent = options.logEvent;
if (Object.hasOwn(options || {}, 'logEvent')) {
shouldLogEvent = options!.logEvent;
}
logger.logEventIfEnabled(event, shouldLogEvent);
}
Expand All @@ -440,9 +445,7 @@ class Logger extends Utility implements ClassThatLogs {
* @returns {void}
*/
public logEventIfEnabled(event: unknown, overwriteValue?: boolean): void {
if (!this.shouldLogEvent(overwriteValue)) {
return;
}
if (!this.shouldLogEvent(overwriteValue)) return;
this.info('Lambda invocation event', { event });
}

Expand All @@ -454,7 +457,7 @@ class Logger extends Utility implements ClassThatLogs {
* @returns {void}
*/
public refreshSampleRateCalculation(): void {
this.setInitialSampleRate(this.powertoolLogData.sampleRateValue);
this.setInitialSampleRate(this.powertoolsLogData.sampleRateValue);
}

/**
Expand All @@ -474,11 +477,11 @@ class Logger extends Utility implements ClassThatLogs {
* @returns {void}
*/
public removePersistentLogAttributes(keys: string[]): void {
keys.forEach((key) => {
if (this.persistentLogAttributes && key in this.persistentLogAttributes) {
for (const key of keys) {
if (Object.hasOwn(this.persistentLogAttributes, key)) {
delete this.persistentLogAttributes[key];
}
});
}
}

/**
Expand Down Expand Up @@ -564,16 +567,12 @@ class Logger extends Utility implements ClassThatLogs {
/**
* It stores information that is printed in all log items.
*
* @param {Partial<PowertoolLogData>} attributesArray
* @param {Partial<PowertoolsLogData>} attributes
* @private
* @returns {void}
*/
private addToPowertoolLogData(
...attributesArray: Array<Partial<PowertoolLogData>>
): void {
attributesArray.forEach((attributes: Partial<PowertoolLogData>) => {
merge(this.powertoolLogData, attributes);
});
private addToPowertoolsLogData(attributes: Partial<PowertoolsLogData>): void {
merge(this.powertoolsLogData, attributes);
}

private awsLogLevelShortCircuit(selectedLogLevel?: string): boolean {
Expand Down Expand Up @@ -624,7 +623,7 @@ class Logger extends Utility implements ClassThatLogs {
message: typeof input === 'string' ? input : input.message,
xRayTraceId: this.envVarsService.getXrayTraceId(),
},
this.getPowertoolLogData()
this.getPowertoolsLogData()
);

let additionalLogAttributes: LogAttributes = {};
Expand Down Expand Up @@ -694,15 +693,15 @@ class Logger extends Utility implements ClassThatLogs {
* @returns - The name of the log level
*/
private getLogLevelNameFromNumber(logLevel: number): Uppercase<LogLevel> {
const found = Object.entries(this.logLevelThresholds).find(
([key, value]) => {
if (value === logLevel) {
return key;
}
let found;
for (const [key, value] of Object.entries(this.logLevelThresholds)) {
if (value === logLevel) {
found = key;
break;
}
)!;
}

return found[0] as Uppercase<LogLevel>;
return found as Uppercase<LogLevel>;
}

/**
Expand All @@ -712,8 +711,8 @@ class Logger extends Utility implements ClassThatLogs {
* @private
* @returns {LogAttributes}
*/
private getPowertoolLogData(): PowertoolLogData {
return this.powertoolLogData;
private getPowertoolsLogData(): PowertoolsLogData {
return this.powertoolsLogData;
}

/**
Expand Down Expand Up @@ -794,7 +793,7 @@ class Logger extends Utility implements ClassThatLogs {
logLevel === 24
? 'error'
: (this.getLogLevelNameFromNumber(logLevel).toLowerCase() as keyof Omit<
ClassThatLogs,
LogFunction,
'critical'
>);

Expand Down Expand Up @@ -923,14 +922,14 @@ class Logger extends Utility implements ClassThatLogs {
* @returns {void}
*/
private setInitialSampleRate(sampleRateValue?: number): void {
this.powertoolLogData.sampleRateValue = 0;
this.powertoolsLogData.sampleRateValue = 0;
const constructorValue = sampleRateValue;
const customConfigValue =
this.getCustomConfigService()?.getSampleRateValue();
const envVarsValue = this.getEnvVarsService().getSampleRateValue();
for (const value of [constructorValue, customConfigValue, envVarsValue]) {
if (this.isValidSampleRate(value)) {
this.powertoolLogData.sampleRateValue = value;
this.powertoolsLogData.sampleRateValue = value;

if (value && randomInt(0, 100) / 100 <= value) {
this.setLogLevel('DEBUG');
Expand Down Expand Up @@ -1005,7 +1004,7 @@ class Logger extends Utility implements ClassThatLogs {
this.setCustomConfigService(customConfigService);
this.setInitialLogLevel(logLevel);
this.setLogFormatter(logFormatter);
this.setPowertoolLogData(serviceName, environment);
this.setPowertoolsLogData(serviceName, environment);
this.setInitialSampleRate(sampleRateValue);
this.setLogEvent();
this.setLogIndentation();
Expand All @@ -1024,26 +1023,24 @@ class Logger extends Utility implements ClassThatLogs {
* @private
* @returns {void}
*/
private setPowertoolLogData(
private setPowertoolsLogData(
serviceName?: string,
environment?: Environment,
persistentLogAttributes: LogAttributes = {}
): void {
this.addToPowertoolLogData(
{
awsRegion: this.getEnvVarsService().getAwsRegion(),
environment:
environment ||
this.getCustomConfigService()?.getCurrentEnvironment() ||
this.getEnvVarsService().getCurrentEnvironment(),
serviceName:
serviceName ||
this.getCustomConfigService()?.getServiceName() ||
this.getEnvVarsService().getServiceName() ||
this.getDefaultServiceName(),
},
persistentLogAttributes
);
this.addToPowertoolsLogData({
awsRegion: this.getEnvVarsService().getAwsRegion(),
environment:
environment ||
this.getCustomConfigService()?.getCurrentEnvironment() ||
this.getEnvVarsService().getCurrentEnvironment(),
serviceName:
serviceName ||
this.getCustomConfigService()?.getServiceName() ||
this.getEnvVarsService().getServiceName() ||
this.getDefaultServiceName(),
});
this.addPersistentLogAttributes(persistentLogAttributes);
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/logger/src/config/EnvironmentVariablesService.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ConfigServiceInterface } from './ConfigServiceInterface.js';
import { ConfigServiceInterface } from '../types/ConfigServiceInterface.js';
import { EnvironmentVariablesService as CommonEnvironmentVariablesService } from '@aws-lambda-powertools/commons';

/**
Expand Down
17 changes: 17 additions & 0 deletions packages/logger/src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* The indent level for JSON logs.
*
* By default Logger will use the `LogJsonIndent.COMPACT` indent level, which
* produces logs on a single line. This is the most efficient option for
* CloudWatch Logs.
*
* When enabling the `POWERTOOLS_DEV` environment variable, Logger will use the
* `LogJsonIndent.PRETTY` indent level, which indents the JSON logs for easier
* reading.
*/
const LogJsonIndent = {
PRETTY: 4,
COMPACT: 0,
} as const;

export { LogJsonIndent };
Loading