Skip to content

Commit bd9d833

Browse files
committed
Added logger
1 parent fd5e899 commit bd9d833

File tree

3 files changed

+30
-6
lines changed

3 files changed

+30
-6
lines changed

packages/batch/src/BasePartialBatchProcessor.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
import { getStringFromEnv } from '@aws-lambda-powertools/commons/utils/env';
12
import type {
23
DynamoDBRecord,
34
KinesisStreamRecord,
45
SQSRecord,
56
} from 'aws-lambda';
7+
import type { GenericLogger } from '../../commons/lib/esm/types/GenericLogger.js';
68
import { BasePartialProcessor } from './BasePartialProcessor.js';
79
import {
810
DATA_CLASS_MAPPING,
@@ -43,6 +45,13 @@ abstract class BasePartialBatchProcessor extends BasePartialProcessor {
4345
*/
4446
public eventType: keyof typeof EventType;
4547

48+
/**
49+
* A logger instance to be used for logging debug, warning, and error messages.
50+
*
51+
* When no logger is provided, we'll only log warnings and errors using the global `console` object.
52+
*/
53+
protected readonly logger: Pick<GenericLogger, 'debug' | 'warn' | 'error'>;
54+
4655
/**
4756
* The configuration options for the parser integration
4857
*/
@@ -66,6 +75,15 @@ abstract class BasePartialBatchProcessor extends BasePartialProcessor {
6675
[EventType.DynamoDBStreams]: () => this.collectDynamoDBFailures(),
6776
};
6877
this.parserConfig = parserConfig;
78+
const alcLogLevel = getStringFromEnv({
79+
key: 'AWS_LAMBDA_LOG_LEVEL',
80+
defaultValue: '',
81+
});
82+
this.logger = parserConfig?.logger ?? {
83+
debug: alcLogLevel === 'DEBUG' ? console.debug : () => undefined,
84+
error: console.error,
85+
warn: console.warn,
86+
};
6987
}
7088

7189
/**

packages/batch/src/BatchProcessor.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -225,9 +225,11 @@ class BatchProcessor extends BasePartialBatchProcessor {
225225
return result.data as EventSourceDataClassTypes;
226226
}
227227
const issues = result.error.cause as ReadonlyArray<StandardSchemaV1.Issue>;
228-
throw new Error(
229-
`Failed to parse record: ${issues.map((issue) => `${issue?.path?.join('.')}: ${issue.message}`).join('; ')}`
230-
);
228+
const errorMessage = issues
229+
.map((issue) => `${issue?.path?.join('.')}: ${issue.message}`)
230+
.join('; ');
231+
this.logger.error(errorMessage);
232+
throw new Error(errorMessage);
231233
}
232234

233235
/**
@@ -256,7 +258,7 @@ class BatchProcessor extends BasePartialBatchProcessor {
256258
if (innerSchema != null) {
257259
// Only proceed with schema extension if it's a Zod schema
258260
if (innerSchema['~standard'].vendor !== SchemaVendor.Zod) {
259-
console.warn(
261+
this.logger.error(
260262
'The schema provided is not supported. Only Zod schemas are supported for extension.'
261263
);
262264
throw new Error('Unsupported schema type');
@@ -275,6 +277,9 @@ class BatchProcessor extends BasePartialBatchProcessor {
275277
});
276278
return this.#parseWithErrorHandling(record, schemaWithoutTransformers);
277279
}
280+
this.logger.error(
281+
'The schema provided is not supported. Only Zod schemas are supported for extension.'
282+
);
278283
throw new Error('Either schema or innerSchema is required for parsing');
279284
}
280285
}

packages/batch/src/types.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,8 @@ type PartialItemFailureResponse = { batchItemFailures: PartialItemFailures[] };
9494
/**
9595
* Type representing the parser configuration options passed to the BasePartialBatchProcessor class.
9696
*
97-
* @property schema - The schema to be used for parsing
98-
* @property innerSchema - The schema for the inner payload
97+
* @property schema - The full event schema to be used for parsing
98+
* @property innerSchema - The inner payload schema
9999
* @property transformer - The transformer to be used for parsing the payload
100100
* @property logger - The logger to be used for logging debug and warning messages.
101101
*/
@@ -113,6 +113,7 @@ type BasePartialBatchProcessorParserConfig = {
113113
innerSchema?: StandardSchemaV1;
114114
/**
115115
* The transformer to be used for parsing the payload.
116+
* No transformers will be used if this is not provided.
116117
* Supported transformers are:
117118
* 1. 'json': Uses JSONStringified helper
118119
* 2. 'base64': Uses Base64Encoded helper

0 commit comments

Comments
 (0)