Skip to content

Commit d239d1c

Browse files
authored
chore(maintenance): migrate logger utility to biome (#2813)
1 parent 8b0cae4 commit d239d1c

21 files changed

+302
-194
lines changed

packages/logger/package.json

+4-9
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
"build:cjs": "tsc --build tsconfig.json && echo '{ \"type\": \"commonjs\" }' > lib/cjs/package.json",
2121
"build:esm": "tsc --build tsconfig.esm.json && echo '{ \"type\": \"module\" }' > lib/esm/package.json",
2222
"build": "npm run build:esm & npm run build:cjs",
23-
"lint": "eslint --ext .ts,.js --no-error-on-unmatched-pattern .",
24-
"lint-fix": "eslint --fix --ext .ts,.js --no-error-on-unmatched-pattern .",
23+
"lint": "biome lint .",
24+
"lint:fix": "biome check --write .",
2525
"prepack": "node ../../.github/scripts/release_patch_package_json.js ."
2626
},
2727
"homepage": "https://github.com/aws-powertools/powertools-lambda-typescript/tree/main/packages/logger#readme",
@@ -53,10 +53,7 @@
5353
"lib/cjs/middleware/middy.d.ts",
5454
"lib/esm/middleware/middy.d.ts"
5555
],
56-
"types": [
57-
"lib/cjs/types/index.d.ts",
58-
"lib/esm/types/index.d.ts"
59-
]
56+
"types": ["lib/cjs/types/index.d.ts", "lib/esm/types/index.d.ts"]
6057
}
6158
},
6259
"types": "./lib/cjs/index.d.ts",
@@ -73,9 +70,7 @@
7370
"optional": true
7471
}
7572
},
76-
"files": [
77-
"lib"
78-
],
73+
"files": ["lib"],
7974
"repository": {
8075
"type": "git",
8176
"url": "git+https://github.com/aws-powertools/powertools-lambda-typescript.git"

packages/logger/src/Logger.ts

+20-23
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,30 @@
1+
import { Console } from 'node:console';
2+
import { randomInt } from 'node:crypto';
13
import { Utility } from '@aws-lambda-powertools/commons';
24
import type { HandlerMethodDecorator } from '@aws-lambda-powertools/commons/types';
35
import type { Context, Handler } from 'aws-lambda';
46
import merge from 'lodash.merge';
5-
import { Console } from 'node:console';
6-
import { randomInt } from 'node:crypto';
77
import { EnvironmentVariablesService } from './config/EnvironmentVariablesService.js';
88
import { LogJsonIndent } from './constants.js';
9-
import { LogItem } from './formatter/LogItem.js';
9+
import type { LogItem } from './formatter/LogItem.js';
1010
import { PowertoolsLogFormatter } from './formatter/PowertoolsLogFormatter.js';
1111
import type { ConfigServiceInterface } from './types/ConfigServiceInterface.js';
1212
import type {
1313
Environment,
1414
LogAttributes,
15+
LogFormatterInterface,
1516
LogLevel,
1617
LogLevelThresholds,
17-
LogFormatterInterface,
1818
} from './types/Log.js';
1919
import type {
20-
LogFunction,
2120
ConstructorOptions,
21+
CustomJsonReplacerFn,
2222
InjectLambdaContextOptions,
23+
LogFunction,
2324
LogItemExtraInput,
2425
LogItemMessage,
2526
LoggerInterface,
2627
PowertoolsLogData,
27-
CustomJsonReplacerFn,
2828
} from './types/Logger.js';
2929

3030
/**
@@ -442,10 +442,7 @@ class Logger extends Utility implements LoggerInterface {
442442
options?: InjectLambdaContextOptions
443443
): HandlerMethodDecorator {
444444
return (_target, _propertyKey, descriptor) => {
445-
/**
446-
* The descriptor.value is the method this decorator decorates, it cannot be undefined.
447-
*/
448-
/* eslint-disable @typescript-eslint/no-non-null-assertion */
445+
// biome-ignore lint/style/noNonNullAssertion: The descriptor.value is the method this decorator decorates, it cannot be undefined.
449446
const originalMethod = descriptor.value!;
450447

451448
// eslint-disable-next-line @typescript-eslint/no-this-alias
@@ -463,8 +460,6 @@ class Logger extends Utility implements LoggerInterface {
463460
let result: unknown;
464461
try {
465462
result = await originalMethod.apply(this, [event, context, callback]);
466-
} catch (error) {
467-
throw error;
468463
} finally {
469464
if (options?.clearState || options?.resetKeys) loggerRef.resetKeys();
470465
}
@@ -697,22 +692,24 @@ class Logger extends Utility implements LoggerInterface {
697692
const references = new WeakSet();
698693

699694
return (key, value) => {
700-
if (this.#jsonReplacerFn) value = this.#jsonReplacerFn?.(key, value);
695+
let replacedValue = value;
696+
if (this.#jsonReplacerFn)
697+
replacedValue = this.#jsonReplacerFn?.(key, replacedValue);
701698

702-
if (value instanceof Error) {
703-
value = this.getLogFormatter().formatError(value);
699+
if (replacedValue instanceof Error) {
700+
replacedValue = this.getLogFormatter().formatError(replacedValue);
704701
}
705-
if (typeof value === 'bigint') {
706-
return value.toString();
702+
if (typeof replacedValue === 'bigint') {
703+
return replacedValue.toString();
707704
}
708-
if (typeof value === 'object' && value !== null) {
709-
if (references.has(value)) {
705+
if (typeof replacedValue === 'object' && replacedValue !== null) {
706+
if (references.has(replacedValue)) {
710707
return;
711708
}
712-
references.add(value);
709+
references.add(replacedValue);
713710
}
714711

715-
return value;
712+
return replacedValue;
716713
};
717714
}
718715

@@ -855,10 +852,10 @@ class Logger extends Utility implements LoggerInterface {
855852
* @returns - The name of the log level
856853
*/
857854
private getLogLevelNameFromNumber(logLevel: number): Uppercase<LogLevel> {
858-
let found;
855+
let found: Uppercase<LogLevel> | undefined;
859856
for (const [key, value] of Object.entries(this.logLevelThresholds)) {
860857
if (value === logLevel) {
861-
found = key;
858+
found = key as Uppercase<LogLevel>;
862859
break;
863860
}
864861
}

packages/logger/src/config/EnvironmentVariablesService.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { ConfigServiceInterface } from '../types/ConfigServiceInterface.js';
21
import { EnvironmentVariablesService as CommonEnvironmentVariablesService } from '@aws-lambda-powertools/commons';
2+
import type { ConfigServiceInterface } from '../types/ConfigServiceInterface.js';
33

44
/**
55
* Class EnvironmentVariablesService

packages/logger/src/formatter/LogFormatter.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import type {
55
LogFormatterOptions,
66
} from '../types/Log.js';
77
import type { UnformattedAttributes } from '../types/Logger.js';
8-
import { LogItem } from './LogItem.js';
8+
import type { LogItem } from './LogItem.js';
99

1010
/**
1111
* This class defines and implements common methods for the formatting of log attributes.
@@ -92,9 +92,8 @@ abstract class LogFormatter implements LogFormatterInterface {
9292
const stackLines = stack.split('\n');
9393
const regex = /\(([^)]*?):(\d+?):(\d+?)\)\\?$/;
9494

95-
let i;
96-
for (i = 0; i < stackLines.length; i++) {
97-
const match = regex.exec(stackLines[i]);
95+
for (const item of stackLines) {
96+
const match = regex.exec(item);
9897

9998
if (Array.isArray(match)) {
10099
return `${match[1]}:${Number(match[2])}`;

packages/logger/src/middleware/middy.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { Logger } from '../Logger.js';
2-
import type { InjectLambdaContextOptions } from '../types/Logger.js';
31
import { LOGGER_KEY } from '@aws-lambda-powertools/commons';
42
import type {
53
MiddlewareLikeObj,
64
MiddyLikeRequest,
75
} from '@aws-lambda-powertools/commons/types';
6+
import { Logger } from '../Logger.js';
7+
import type { InjectLambdaContextOptions } from '../types/Logger.js';
88

99
/**
1010
* A middy middleware that helps emitting CloudWatch EMF metrics in your logs.
@@ -35,7 +35,7 @@ const injectLambdaContext = (
3535
target: Logger | Logger[],
3636
options?: InjectLambdaContextOptions
3737
): MiddlewareLikeObj => {
38-
const loggers = target instanceof Array ? target : [target];
38+
const loggers = Array.isArray(target) ? target : [target];
3939
const isResetStateEnabled =
4040
options && (options.clearState || options.resetKeys);
4141

@@ -54,7 +54,7 @@ const injectLambdaContext = (
5454
const injectLambdaContextBefore = async (
5555
request: MiddyLikeRequest
5656
): Promise<void> => {
57-
loggers.forEach((logger: Logger) => {
57+
for (const logger of loggers) {
5858
if (isResetStateEnabled) {
5959
setCleanupFunction(request);
6060
}
@@ -64,14 +64,14 @@ const injectLambdaContext = (
6464
request.context,
6565
options
6666
);
67-
});
67+
}
6868
};
6969

7070
const injectLambdaContextAfterOrOnError = async (): Promise<void> => {
7171
if (isResetStateEnabled) {
72-
loggers.forEach((logger: Logger) => {
72+
for (const logger of loggers) {
7373
logger.resetKeys();
74-
});
74+
}
7575
}
7676
};
7777

packages/logger/src/types/Log.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import type { EnvironmentVariablesService } from '../config/EnvironmentVariablesService.js';
2+
import type { LogLevel as LogLevelList } from '../constants.js';
23
import type { LogItem } from '../formatter/LogItem.js';
34
import type { UnformattedAttributes } from './Logger.js';
4-
import { LogLevel } from '../constants.js';
55

66
type LogLevel =
7-
| (typeof LogLevel)[keyof typeof LogLevel]
8-
| Lowercase<(typeof LogLevel)[keyof typeof LogLevel]>;
7+
| (typeof LogLevelList)[keyof typeof LogLevelList]
8+
| Lowercase<(typeof LogLevelList)[keyof typeof LogLevelList]>;
99

1010
type LogLevelThresholds = {
1111
[key in Uppercase<LogLevel>]: number;

packages/logger/src/types/Logger.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import type { HandlerMethodDecorator } from '@aws-lambda-powertools/commons/types';
2+
import type { Context } from 'aws-lambda';
23
import type { ConfigServiceInterface } from './ConfigServiceInterface.js';
34
import type {
45
Environment,
56
LogAttributes,
67
LogAttributesWithMessage,
7-
LogLevel,
88
LogFormatterInterface,
9+
LogLevel,
910
} from './Log.js';
10-
import type { Context } from 'aws-lambda';
1111

1212
type LogFunction = {
1313
[key in Exclude<Lowercase<LogLevel>, 'silent'>]: (

packages/logger/tests/e2e/basicFeatures.middy.test.FunctionCode.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
import type { APIGatewayAuthorizerResult, Context } from 'aws-lambda';
2+
import middy from 'middy5';
13
import { Logger } from '../../src/index.js';
24
import { injectLambdaContext } from '../../src/middleware/middy.js';
3-
import type { Context, APIGatewayAuthorizerResult } from 'aws-lambda';
45
import type { TestEvent, TestOutput } from '../helpers/types.js';
5-
import middy from 'middy5';
66

77
const PERSISTENT_KEY = process.env.PERSISTENT_KEY || 'persistentKey';
88
const PERSISTENT_VALUE = process.env.PERSISTENT_VALUE || 'persistentValue';

packages/logger/tests/e2e/basicFeatures.middy.test.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
*
44
* @group e2e/logger/basicFeatures
55
*/
6+
import { join } from 'node:path';
67
import {
7-
invokeFunction,
88
TestInvocationLogs,
99
TestStack,
10+
invokeFunction,
1011
} from '@aws-lambda-powertools/testing-utils';
1112
import type { APIGatewayAuthorizerResult } from 'aws-lambda';
12-
import { join } from 'node:path';
1313
import { LoggerTestNodejsFunction } from '../helpers/resources.js';
1414
import {
1515
RESOURCE_NAME_PREFIX,
@@ -21,7 +21,7 @@ import {
2121
commonEnvironmentVars,
2222
} from './constants.js';
2323

24-
describe(`Logger E2E tests, basic functionalities middy usage`, () => {
24+
describe('Logger E2E tests, basic functionalities middy usage', () => {
2525
const testStack = new TestStack({
2626
stackNameProps: {
2727
stackNamePrefix: RESOURCE_NAME_PREFIX,

packages/logger/tests/e2e/childLogger.manual.test.FunctionCode.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { Logger } from '../../src/index.js';
21
import type { Context } from 'aws-lambda';
2+
import { Logger } from '../../src/index.js';
33
import type { LogLevel } from '../../src/types/index.js';
4-
import { TestEvent, TestOutput } from '../helpers/types.js';
4+
import type { TestEvent, TestOutput } from '../helpers/types.js';
55

66
const PERSISTENT_KEY = process.env.PERSISTENT_KEY || 'persistentKey';
77
const PERSISTENT_VALUE = process.env.ERSISTENT_VALUE || 'persistentValue';

packages/logger/tests/e2e/childLogger.manual.test.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,23 @@
33
*
44
* @group e2e/logger/childLogger
55
*/
6+
import { join } from 'node:path';
67
import {
7-
invokeFunction,
88
TestInvocationLogs,
99
TestStack,
10+
invokeFunction,
1011
} from '@aws-lambda-powertools/testing-utils';
11-
import { join } from 'node:path';
1212
import { LoggerTestNodejsFunction } from '../helpers/resources.js';
1313
import {
14-
commonEnvironmentVars,
1514
RESOURCE_NAME_PREFIX,
1615
SETUP_TIMEOUT,
1716
STACK_OUTPUT_LOG_GROUP,
1817
TEARDOWN_TIMEOUT,
1918
TEST_CASE_TIMEOUT,
19+
commonEnvironmentVars,
2020
} from './constants.js';
2121

22-
describe(`Logger E2E tests, child logger`, () => {
22+
describe('Logger E2E tests, child logger', () => {
2323
const testStack = new TestStack({
2424
stackNameProps: {
2525
stackNamePrefix: RESOURCE_NAME_PREFIX,

packages/logger/tests/e2e/logEventEnvVarSetting.middy.test.FunctionCode.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
import type { Context } from 'aws-lambda';
2+
import middy from 'middy4';
13
import { Logger } from '../../src/index.js';
24
import { injectLambdaContext } from '../../src/middleware/middy.js';
35
import type { TestEvent, TestOutput } from '../helpers/types.js';
4-
import type { Context } from 'aws-lambda';
5-
import middy from 'middy4';
66

77
const logger = new Logger();
88

packages/logger/tests/e2e/logEventEnvVarSetting.middy.test.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
*
44
* @group e2e/logger/logEventEnvVarSetting
55
*/
6+
import { join } from 'node:path';
67
import {
7-
invokeFunction,
88
TestInvocationLogs,
99
TestStack,
10+
invokeFunction,
1011
} from '@aws-lambda-powertools/testing-utils';
11-
import { join } from 'node:path';
1212
import { LoggerTestNodejsFunction } from '../helpers/resources.js';
1313
import {
1414
RESOURCE_NAME_PREFIX,
@@ -18,7 +18,7 @@ import {
1818
TEST_CASE_TIMEOUT,
1919
} from './constants.js';
2020

21-
describe(`Logger E2E tests, log event via env var setting with middy`, () => {
21+
describe('Logger E2E tests, log event via env var setting with middy', () => {
2222
const testStack = new TestStack({
2323
stackNameProps: {
2424
stackNamePrefix: RESOURCE_NAME_PREFIX,

packages/logger/tests/e2e/sampleRate.decorator.test.FunctionCode.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1+
import type { LambdaInterface } from '@aws-lambda-powertools/commons/types';
2+
import type { Context } from 'aws-lambda';
13
import { Logger } from '../../src/index.js';
24
import type { TestEvent, TestOutput } from '../helpers/types.js';
3-
import type { Context } from 'aws-lambda';
4-
import type { LambdaInterface } from '@aws-lambda-powertools/commons/types';
55

6-
const SAMPLE_RATE = parseFloat(process.env.SAMPLE_RATE || '0.1');
6+
const SAMPLE_RATE = Number.parseFloat(process.env.SAMPLE_RATE || '0.1');
77
const LOG_MSG = process.env.LOG_MSG || 'Hello World';
88

99
const logger = new Logger({

packages/logger/tests/e2e/sampleRate.decorator.test.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
*
44
* @group e2e/logger/sampleRate
55
*/
6+
import { randomUUID } from 'node:crypto';
7+
import { join } from 'node:path';
68
import {
7-
invokeFunction,
89
TestInvocationLogs,
910
TestStack,
11+
invokeFunction,
1012
} from '@aws-lambda-powertools/testing-utils';
11-
import { randomUUID } from 'node:crypto';
12-
import { join } from 'node:path';
1313
import { LoggerTestNodejsFunction } from '../helpers/resources.js';
1414
import {
1515
RESOURCE_NAME_PREFIX,
@@ -19,7 +19,7 @@ import {
1919
TEST_CASE_TIMEOUT,
2020
} from './constants.js';
2121

22-
describe(`Logger E2E tests, sample rate and injectLambdaContext()`, () => {
22+
describe('Logger E2E tests, sample rate and injectLambdaContext()', () => {
2323
const testStack = new TestStack({
2424
stackNameProps: {
2525
stackNamePrefix: RESOURCE_NAME_PREFIX,

0 commit comments

Comments
 (0)