Skip to content

Commit a8012f1

Browse files
committed
refactor: enhance logging in S3 class
1 parent c6c458a commit a8012f1

File tree

1 file changed

+14
-4
lines changed
  • packages/serverless-offline-s3/src

1 file changed

+14
-4
lines changed

packages/serverless-offline-s3/src/s3.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const delay = timeout =>
1010
});
1111

1212
class S3 {
13-
constructor(lambda, resources, options, log) {
13+
constructor(lambda, resources, options, {log}) {
1414
this.lambda = null;
1515
this.resources = null;
1616
this.options = null;
@@ -48,30 +48,40 @@ class S3 {
4848
return Promise.all(
4949
this.events.map(async ({functionKey, s3}) => {
5050
const {event, bucket, rules} = s3;
51-
await this._waitFor(bucket);
51+
this.log.debug(`Setting up listener for bucket: ${bucket}, event: ${event}`);
5252

53+
await this._waitFor(bucket);
5354
const eventRules = rules || [];
5455
const prefix = (eventRules.find(rule => rule.prefix) || {prefix: '*'}).prefix;
5556
const suffix = (eventRules.find(rule => rule.suffix) || {suffix: '*'}).suffix;
56-
5757
const listener = this.client.listenBucketNotification(bucket, prefix, suffix, [event]);
5858

5959
listener.on('notification', async record => {
6060
if (record) {
6161
try {
62+
this.log.debug(
63+
`Received S3 notification for bucket ${bucket}: ${JSON.stringify(record)}`
64+
);
6265
const lambdaFunction = this.lambda.get(functionKey);
6366

6467
const s3Notification = new S3Event(record);
6568
lambdaFunction.setEvent(s3Notification);
6669

6770
await lambdaFunction.runHandler();
6871
} catch (err) {
69-
this.log.warning(err.stack);
72+
this.log.warning(
73+
`Error processing S3 notification for bucket ${bucket}: ${err.stack}`
74+
);
7075
}
7176
}
7277
});
7378

79+
listener.on('error', err => {
80+
this.log.warning(`Error in S3 listener for bucket ${bucket}: ${err.message}`);
81+
});
82+
7483
this.listeners = [...this.listeners, listener];
84+
this.log.debug(`Listener set up successfully for bucket: ${bucket}`);
7585
})
7686
);
7787
}

0 commit comments

Comments
 (0)