Skip to content

Commit 24dd7e0

Browse files
committed
refactor: implement _safeLog method for consistent logging in S3 and SQS
refactor: simplify S3 constructor by removing destructuring of log parameter
1 parent 5b33037 commit 24dd7e0

File tree

8 files changed

+82
-14
lines changed

8 files changed

+82
-14
lines changed

packages/serverless-offline-s3/README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,22 @@ plugins:
2626
2727
[See example](../../tests/serverless-plugins-integration/README.md#s3)
2828
29+
## Migrating from v7 to v8
30+
31+
This version requires Serverless Framework v4 and serverless-offline v14.4.0+.
32+
33+
### Breaking Changes
34+
35+
- Requires `serverless-offline: "^14.4.0 || >=14"`
36+
- Uses new Serverless v4 plugin API for logging
37+
- Node.js 18+ required
38+
39+
### Upgrade Steps
40+
41+
1. Update serverless to v4: `npm install serverless@^4.0.0`
42+
2. Update serverless-offline to v14.4.0+: `npm install serverless-offline@^14.4.0`
43+
3. Update this plugin: `npm install serverless-offline-s3@^8.0.0`
44+
2945
## How it works?
3046

3147
To be able to emulate AWS S3 Bucket on local machine there should be some bucket system actually running. One of the existing implementations suitable for the task is [Minio](https://github.com/minio/minio).

packages/serverless-offline-s3/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,4 @@
4444
"serverless-offline",
4545
"lambda"
4646
]
47-
}
47+
}

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const defaultOptions = {
1515
const omitUndefined = omitBy(isUndefined);
1616

1717
class ServerlessOfflineS3 {
18-
constructor(serverless, cliOptions, {log}) {
18+
constructor(serverless, cliOptions, {log} = {}) {
1919
this.cliOptions = null;
2020
this.options = null;
2121
this.s3 = null;
@@ -25,7 +25,11 @@ class ServerlessOfflineS3 {
2525

2626
this.cliOptions = cliOptions;
2727
this.serverless = serverless;
28-
this.log = log;
28+
this.log = log || {
29+
debug: console.debug.bind(console),
30+
notice: console.log.bind(console),
31+
warning: console.warn.bind(console)
32+
};
2933

3034
this.hooks = {
3135
'offline:start:init': this.start.bind(this),

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

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,14 @@ const delay = timeout =>
99
setTimeout(resolve, timeout);
1010
});
1111

12+
const defaultLog = {
13+
debug: console.debug.bind(console),
14+
notice: console.log.bind(console),
15+
warning: console.warn.bind(console)
16+
};
17+
1218
class S3 {
13-
constructor(lambda, resources, options, {log}) {
19+
constructor(lambda, resources, options, log = defaultLog) {
1420
this.lambda = null;
1521
this.resources = null;
1622
this.options = null;
@@ -19,7 +25,7 @@ class S3 {
1925
this.lambda = lambda;
2026
this.resources = resources;
2127
this.options = options;
22-
this.log = log;
28+
this.log = log || defaultLog;
2329

2430
const s3Endpoint = this.options.endpoint ? new URL(this.options.endpoint) : {};
2531
this.client = new Minio.Client(
@@ -34,6 +40,14 @@ class S3 {
3440
this.listeners = [];
3541
}
3642

43+
_safeLog(level, message) {
44+
if (this.log && typeof this.log[level] === 'function') {
45+
this.log[level](message);
46+
} else if (console[level]) {
47+
console[level](message);
48+
}
49+
}
50+
3751
create(events) {
3852
this.events = events;
3953
return Promise.all(
@@ -86,7 +100,7 @@ class S3 {
86100
);
87101
}
88102

89-
stop(timeout) {
103+
stop(_timeout) {
90104
this.listeners.forEach(listener => listener.stop());
91105
this.listeners = [];
92106
}
@@ -124,7 +138,7 @@ class S3 {
124138

125139
await lambdaFunction.runHandler();
126140
} catch (err) {
127-
this.log.warning(err.stack);
141+
this._safeLog('warning', err.stack);
128142
}
129143
}
130144
});

packages/serverless-offline-sqs/README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,22 @@ plugins:
2626
2727
[See example](../../tests/serverless-plugins-integration/README.md#sqs)
2828
29+
## Migrating from v8 to v9
30+
31+
This version requires Serverless Framework v4 and serverless-offline v14.4.0+.
32+
33+
### Breaking Changes
34+
35+
- Requires `serverless-offline: "^14.4.0 || >=14"`
36+
- Uses new Serverless v4 plugin API for logging
37+
- Node.js 18+ required
38+
39+
### Upgrade Steps
40+
41+
1. Update serverless to v4: `npm install serverless@^4.0.0`
42+
2. Update serverless-offline to v14.4.0+: `npm install serverless-offline@^14.4.0`
43+
3. Update this plugin: `npm install serverless-offline-sqs@^9.0.0`
44+
2945
## How it works?
3046

3147
To be able to emulate AWS SQS queue on local machine there should be some queue system actually running. One of the existing implementations suitable for the task is [ElasticMQ](https://github.com/adamw/elasticmq).

packages/serverless-offline-sqs/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,4 @@
4444
"serverless-offline",
4545
"lambda"
4646
]
47-
}
47+
}

packages/serverless-offline-sqs/src/index.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ const defaultOptions = {
3030
const omitUndefined = omitBy(isUndefined);
3131

3232
class ServerlessOfflineSQS {
33-
constructor(serverless, cliOptions, {log}) {
33+
constructor(serverless, cliOptions, {log} = {}) {
3434
this.cliOptions = null;
3535
this.options = null;
3636
this.log = null;
@@ -40,7 +40,11 @@ class ServerlessOfflineSQS {
4040

4141
this.cliOptions = cliOptions;
4242
this.serverless = serverless;
43-
this.log = log;
43+
this.log = log || {
44+
debug: console.debug.bind(console),
45+
notice: console.log.bind(console),
46+
warning: console.warn.bind(console)
47+
};
4448

4549
this.hooks = {
4650
'offline:start:init': this.start.bind(this),

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

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,14 @@ const delay = timeout =>
2020
setTimeout(resolve, timeout);
2121
});
2222

23+
const defaultLog = {
24+
debug: console.debug.bind(console),
25+
notice: console.log.bind(console),
26+
warning: console.warn.bind(console)
27+
};
28+
2329
class SQS {
24-
constructor(lambda, resources, options, log) {
30+
constructor(lambda, resources, options, log = defaultLog) {
2531
this.lambda = null;
2632
this.resources = null;
2733
this.options = null;
@@ -30,13 +36,21 @@ class SQS {
3036
this.lambda = lambda;
3137
this.resources = resources;
3238
this.options = options;
33-
this.log = log;
39+
this.log = log || defaultLog;
3440

3541
this.client = new SQSClient(this.options);
3642

3743
this.queue = new PQueue({autoStart: false});
3844
}
3945

46+
_safeLog(level, message) {
47+
if (this.log && typeof this.log[level] === 'function') {
48+
this.log[level](message);
49+
} else if (console[level]) {
50+
console[level](message);
51+
}
52+
}
53+
4054
create(events) {
4155
return Promise.all(events.map(({functionKey, sqs}) => this._create(functionKey, sqs)));
4256
}
@@ -139,7 +153,7 @@ class SQS {
139153
)
140154
);
141155
} catch (err) {
142-
this.log.warning(err.stack);
156+
this._safeLog('warning', err.stack);
143157
}
144158
}
145159

@@ -171,7 +185,7 @@ class SQS {
171185
} catch (err) {
172186
if (remainingTry > 0 && err.name === 'AWS.SimpleQueueService.NonExistentQueue')
173187
return this._createQueue({queueName}, remainingTry - 1);
174-
this.log.warning(err.stack);
188+
this._safeLog('warning', err.stack);
175189
}
176190
}
177191
}

0 commit comments

Comments
 (0)