Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion packages/logger/src/formatter/LogFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ abstract class LogFormatter {
/**
* If a specific timezone is configured and it's not the default `UTC`,
* format the timestamp with the appropriate timezone offset.
**/
*/
const configuredTimezone = getStringFromEnv({
key: 'TZ',
defaultValue: '',
Expand Down
6 changes: 3 additions & 3 deletions packages/logger/src/middleware/middy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ const injectLambdaContext = (
};
};

const before = async (request: MiddyLikeRequest): Promise<void> => {
const before = (request: MiddyLikeRequest) => {
for (const logger of loggers) {
if (isResetStateEnabled) {
setCleanupFunction(request);
Expand All @@ -102,7 +102,7 @@ const injectLambdaContext = (
}
};

const after = async (): Promise<void> => {
const after = () => {
for (const logger of loggers) {
logger.clearBuffer();

Expand All @@ -112,7 +112,7 @@ const injectLambdaContext = (
}
};

const onError = async ({ error }: { error: unknown }): Promise<void> => {
const onError = ({ error }: { error: unknown }) => {
for (const logger of loggers) {
if (options?.flushBufferOnUncaughtError) {
logger.flushBuffer();
Expand Down
6 changes: 4 additions & 2 deletions packages/logger/tests/e2e/advancedUses.test.FunctionCode.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { setTimeout } from 'node:timers/promises';
import { Logger } from '@aws-lambda-powertools/logger';
import {
correlationPaths,
Expand All @@ -18,7 +19,7 @@ const logger = new Logger({

logger.debug('a never buffered debug log');

export const handlerManual = async (event: unknown) => {
export const handlerManual = (event: unknown) => {
logger.addContext({} as Context); // we want only the cold start value
logger.setCorrelationId(event, correlationPaths.EVENT_BRIDGE);

Expand All @@ -42,7 +43,7 @@ export const handlerMiddy = middy()
flushBufferOnUncaughtError: true,
})
)
.handler(async () => {
.handler(() => {
logger.debug('a buffered debug log');
logger.info('an info log');
throw new Error('ops');
Expand All @@ -56,6 +57,7 @@ class Lambda {
public async handler(_event: unknown, _context: Context) {
logger.debug('a buffered debug log');
logger.info('an info log');
await setTimeout(1); // simulate async work
throw new Error('ops');
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ const logger = new Logger({
},
});

const testFunction = async (
const testFunction = (
_event: TestEvent,
context: Context
): TestOutput => {
): Awaited<TestOutput> => {
// Test feature 1: Context data injection (all logs should have the same context data)
// Test feature 2: Event log (this log should have the event data)
// Test feature 3: Log level filtering (log level is set to INFO)
Expand Down
20 changes: 10 additions & 10 deletions packages/logger/tests/e2e/basicFeatures.middy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ describe('Logger E2E tests, basic functionalities middy usage', () => {
});

describe('Log level filtering', () => {
it('should filter log based on POWERTOOLS_LOG_LEVEL (INFO) environment variable in Lambda', async () => {
it('should filter log based on POWERTOOLS_LOG_LEVEL (INFO) environment variable in Lambda', () => {
for (let i = 0; i < invocationCount; i++) {
// Get log messages of the invocation and filter by level
const debugLogs = invocationLogs[i].getFunctionLogs('DEBUG');
Expand All @@ -76,7 +76,7 @@ describe('Logger E2E tests, basic functionalities middy usage', () => {
});

describe('Context data', () => {
it('should inject context info in each log', async () => {
it('should inject context info in each log', () => {
for (let i = 0; i < invocationCount; i++) {
// Get log messages of the invocation
const logMessages = invocationLogs[i].getFunctionLogs();
Expand All @@ -92,7 +92,7 @@ describe('Logger E2E tests, basic functionalities middy usage', () => {
}
});

it('should include coldStart equal to TRUE only on the first invocation, FALSE otherwise', async () => {
it('should include coldStart equal to TRUE only on the first invocation, FALSE otherwise', () => {
for (let i = 0; i < invocationCount; i++) {
// Get log messages of the invocation
const logMessages = invocationLogs[i].getFunctionLogs();
Expand All @@ -109,7 +109,7 @@ describe('Logger E2E tests, basic functionalities middy usage', () => {
});
});

it('logs the event for every invocation, only once, and without keys from previous invocations', async () => {
it('logs the event for every invocation, only once, and without keys from previous invocations', () => {
const { RUNTIME_ADDED_KEY: runtimeAddedKey } = commonEnvironmentVars;

for (let i = 0; i < invocationCount; i++) {
Expand All @@ -132,7 +132,7 @@ describe('Logger E2E tests, basic functionalities middy usage', () => {
});

describe('Persistent additional log keys and values', () => {
it('should contain persistent value in every log', async () => {
it('should contain persistent value in every log', () => {
const {
PERSISTENT_KEY: persistentKey,
PERSISTENT_VALUE: persistentValue,
Expand All @@ -151,7 +151,7 @@ describe('Logger E2E tests, basic functionalities middy usage', () => {
}
});

it('should not contain persistent keys that were removed on runtime', async () => {
it('should not contain persistent keys that were removed on runtime', () => {
const { REMOVABLE_KEY: removableKey, REMOVABLE_VALUE: removableValue } =
commonEnvironmentVars;

Expand All @@ -176,7 +176,7 @@ describe('Logger E2E tests, basic functionalities middy usage', () => {
});

describe('One-time additional log keys and values', () => {
it('should log additional keys and value only once', async () => {
it('should log additional keys and value only once', () => {
const {
SINGLE_LOG_ITEM_KEY: singleLogItemKey,
SINGLE_LOG_ITEM_VALUE: singleLogItemValue,
Expand All @@ -200,7 +200,7 @@ describe('Logger E2E tests, basic functionalities middy usage', () => {
});

describe('Error logging', () => {
it('should log error only once', async () => {
it('should log error only once', () => {
const { ERROR_MSG: errorMsg } = commonEnvironmentVars;

for (let i = 0; i < invocationCount; i++) {
Expand All @@ -226,7 +226,7 @@ describe('Logger E2E tests, basic functionalities middy usage', () => {
});

describe('Arbitrary object logging', () => {
it('should log additional arbitrary object only once', async () => {
it('should log additional arbitrary object only once', () => {
const {
ARBITRARY_OBJECT_KEY: objectKey,
ARBITRARY_OBJECT_DATA: objectData,
Expand Down Expand Up @@ -263,7 +263,7 @@ describe('Logger E2E tests, basic functionalities middy usage', () => {
});

describe('X-Ray Trace ID injection', () => {
it('should inject & parse the X-Ray Trace ID of the current invocation into every log', async () => {
it('should inject & parse the X-Ray Trace ID of the current invocation into every log', () => {
for (let i = 0; i < invocationCount; i++) {
// Get log messages of the invocation
const logMessages = invocationLogs[i].getFunctionLogs();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ const childLogger = parentLogger.createChild({
logLevel: CHILD_LOG_LEVEL,
});

export const handler = async (
export const handler = (
_event: TestEvent,
context: Context
): TestOutput => {
): Awaited<TestOutput> => {
parentLogger.addContext(context);

childLogger.info(CHILD_LOG_MSG);
Expand Down
8 changes: 4 additions & 4 deletions packages/logger/tests/e2e/childLogger.manual.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ describe('Logger E2E tests, child logger', () => {
});

describe('Child logger', () => {
it('should not log at same level of parent because of its own logLevel', async () => {
it('should not log at same level of parent because of its own logLevel', () => {
const { PARENT_LOG_MSG: parentLogMsg, CHILD_LOG_MSG: childLogMsg } =
commonEnvironmentVars;

Expand All @@ -77,7 +77,7 @@ describe('Logger E2E tests, child logger', () => {
}
});

it('should log only level passed to a child', async () => {
it('should log only level passed to a child', () => {
const { CHILD_LOG_MSG: childLogMsg } = commonEnvironmentVars;
for (let i = 0; i < invocationCount; i++) {
// Get log messages of the invocation
Expand All @@ -95,7 +95,7 @@ describe('Logger E2E tests, child logger', () => {
}
});

it('should NOT inject context into the child logger', async () => {
it('should NOT inject context into the child logger', () => {
const { CHILD_LOG_MSG: childLogMsg } = commonEnvironmentVars;

for (let i = 0; i < invocationCount; i++) {
Expand All @@ -118,7 +118,7 @@ describe('Logger E2E tests, child logger', () => {
}
});

it('both logger instances should have the same persistent key/value', async () => {
it('both logger instances should have the same persistent key/value', () => {
const { PERSISTENT_KEY: persistentKey } = commonEnvironmentVars;

for (let i = 0; i < invocationCount; i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ describe('Logger E2E tests, log event via env var setting with middy', () => {
});

describe('Log event', () => {
it('should log the event as the first log of each invocation only', async () => {
it('should log the event as the first log of each invocation only', () => {
for (let i = 0; i < invocationCount; i++) {
// Get log messages of the invocation
const logMessages = invocationLogs[i].getFunctionLogs();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { setTimeout } from 'node:timers/promises';
import type { LambdaInterface } from '@aws-lambda-powertools/commons/types';
import type { Context } from 'aws-lambda';
import { Logger } from '../../src/index.js';
Expand All @@ -16,6 +17,7 @@ class Lambda implements LambdaInterface {
@logger.injectLambdaContext()
public async handler(_event: TestEvent, context: Context): TestOutput {
this.printLogInAllLevels();
await setTimeout(1); // simulate some async work

return {
requestId: context.awsRequestId,
Expand Down
4 changes: 2 additions & 2 deletions packages/logger/tests/e2e/sampleRate.decorator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ describe('Logger E2E tests, sample rate and injectLambdaContext()', () => {
});

describe('Enabling sample rate', () => {
it('should log all levels based on given sample rate, not just ERROR', async () => {
it('should log all levels based on given sample rate, not just ERROR', () => {
// Fetch log streams from all invocations
let countSampled = 0;
let countNotSampled = 0;
Expand Down Expand Up @@ -94,7 +94,7 @@ describe('Logger E2E tests, sample rate and injectLambdaContext()', () => {
});

describe('Decorator injectLambdaContext()', () => {
it('should inject Lambda context into every log emitted', async () => {
it('should inject Lambda context into every log emitted', () => {
for (let i = 0; i < invocationCount; i++) {
// Get log messages of the invocation
const logMessages = invocationLogs[i].getFunctionLogs('ERROR');
Expand Down
2 changes: 1 addition & 1 deletion packages/logger/tests/helpers/resources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type {
TestNodejsFunctionProps,
} from '@aws-lambda-powertools/testing-utils/types';
import { CfnOutput } from 'aws-cdk-lib';
import { commonEnvironmentVars } from '../e2e/constants';
import { commonEnvironmentVars } from '../e2e/constants.js';

interface LoggerExtraTestProps extends ExtraTestProps {
logGroupOutputKey?: string;
Expand Down
23 changes: 15 additions & 8 deletions packages/logger/tests/unit/injectLambdaContext.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { setTimeout } from 'node:timers/promises';
import context from '@aws-lambda-powertools/testing-utils/context';
import middy from '@middy/core';
import type { Context } from 'aws-lambda';
Expand Down Expand Up @@ -78,7 +79,7 @@ describe('Inject Lambda Context', () => {
it('adds the context to log messages when the feature is enabled in the Middy.js middleware', async () => {
// Prepare
const logger = new Logger();
const handler = middy(async () => {
const handler = middy(() => {
logger.info('Hello, world!');
}).use(injectLambdaContext(logger));

Expand All @@ -100,7 +101,7 @@ describe('Inject Lambda Context', () => {
// Prepare
const logger1 = new Logger({ serviceName: 'parent' });
const logger2 = logger1.createChild({ serviceName: 'child' });
const handler = middy(async () => {
const handler = middy(() => {
logger1.info('Hello, world!');
logger2.info('Hello, world!');
}).use(injectLambdaContext([logger1, logger2]));
Expand Down Expand Up @@ -140,6 +141,7 @@ describe('Inject Lambda Context', () => {

@logger.injectLambdaContext()
async handler(_event: unknown, _context: Context) {
await setTimeout(1); // simulate some async operation
this.logGreeting();
}

Expand Down Expand Up @@ -189,7 +191,7 @@ describe('Inject Lambda Context', () => {
{
case: 'middleware',
getHandler: (logger: Logger) =>
middy(async () => {
middy(() => {
logger.info('Hello, world!');
}).use(injectLambdaContext(logger)),
},
Expand All @@ -202,6 +204,7 @@ describe('Inject Lambda Context', () => {
_event: unknown,
_context: Context
): Promise<void> {
await setTimeout(1); // simulate some async operation
logger.info('test');
}
}
Expand Down Expand Up @@ -245,6 +248,7 @@ describe('Inject Lambda Context', () => {
case: 'middleware',
getHandler: (logger: Logger) =>
middy(async () => {
await setTimeout(1); // simulate some async operation
logger.info('Hello, world!');
}).use(
injectLambdaContext(logger, {
Expand All @@ -263,6 +267,7 @@ describe('Inject Lambda Context', () => {
_event: unknown,
_context: Context
): Promise<void> {
await setTimeout(1); // simulate some async operation
logger.info('Hello, world!');
}
}
Expand Down Expand Up @@ -306,7 +311,7 @@ describe('Inject Lambda Context', () => {
},
};
// Act
const handler = middy(async () => {
const handler = middy(() => {
logger.info('Hello, world!');
}).use(
injectLambdaContext(logger, {
Expand All @@ -329,7 +334,7 @@ describe('Inject Lambda Context', () => {
const logger = new Logger({ correlationIdSearchFn: search });

// Act - Use middleware which will internally call setCorrelationIdFromPath
const handler = middy(async () => {
const handler = middy(() => {
logger.info('Hello, world!');
}).use(
injectLambdaContext(logger, {
Expand Down Expand Up @@ -388,7 +393,7 @@ describe('Inject Lambda Context', () => {
it('uses the API_GATEWAY_REST predefined path to extract correlation ID', async () => {
// Prepare
const logger = new Logger({ correlationIdSearchFn: search });
const handler = middy(async () => {
const handler = middy(() => {
logger.info('Using API Gateway request ID');
}).use(
injectLambdaContext(logger, {
Expand Down Expand Up @@ -419,7 +424,7 @@ describe('Inject Lambda Context', () => {
{
case: 'middleware',
getHandler: (logger: Logger) =>
middy(async (event: { id: number }) => {
middy((event: { id: number }) => {
logger.info('Processing event');
logger.appendKeys({ id: event.id });
throw new Error('Test error');
Expand All @@ -440,6 +445,7 @@ describe('Inject Lambda Context', () => {
event: { id: number },
_context: Context
): Promise<void> {
await setTimeout(1); // simulate some async operation
logger.info('Processing event');
logger.appendKeys({ id: event.id });
throw new Error('Test error');
Expand Down Expand Up @@ -480,7 +486,7 @@ describe('Inject Lambda Context', () => {
{
case: 'middleware',
getHandler: (logger: Logger) =>
middy(async (event: { id: number }) => {
middy((event: { id: number }) => {
logger.info('Processing event');
logger.appendKeys({ id: event.id });
return true;
Expand All @@ -498,6 +504,7 @@ describe('Inject Lambda Context', () => {
resetKeys: true,
})
public async handler(event: { id: number }, _context: Context) {
await setTimeout(1); // simulate some async operation
logger.info('Processing event');
logger.appendKeys({ id: event.id });
return true;
Expand Down
Loading
Loading