Skip to content

Commit ee75633

Browse files
author
Roy Hui
committed
Enhance alias logs function
* alias logs will now pull the latest version logs from “Lambda” service directly if --version flag is not specificed. * Added --version flag to alias logs, so that user can view logs of a specific version of function.
1 parent dde216e commit ee75633

File tree

3 files changed

+26
-7
lines changed

3 files changed

+26
-7
lines changed

index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,9 @@ class AwsAlias {
177177
logCommand.options.alias = {
178178
usage: 'Alias'
179179
};
180+
logCommand.options.version = {
181+
usage: 'Logs a specific version of the function'
182+
};
180183
logCommand.commands = _.assign({}, logCommand.commands, {
181184
api: {
182185
usage: 'Output the logs of a deployed APIG stage (alias)',

lib/logs.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,16 @@ module.exports = {
5555
orderBy: 'LastEventTime',
5656
};
5757

58+
let aliasGetAliasFunctionVersion
59+
// Check if --version is specified
60+
if (this._options.version) {
61+
aliasGetAliasFunctionVersion = Promise.resolve(this._options.version)
62+
} else {
63+
aliasGetAliasFunctionVersion = this.aliasGetAliasLatestFunctionVersionByFunctionName(this._alias, this._lambdaName)
64+
}
5865
// Get currently deployed function version for the alias to
5966
// setup the stream filter correctly
60-
return this.aliasGetAliasFunctionVersions(this._alias)
61-
.then(versions => {
62-
return _.map(
63-
_.filter(versions, [ 'functionName', this._lambdaName ]),
64-
version => version.functionVersion);
65-
})
67+
return aliasGetAliasFunctionVersion
6668
.then(version => {
6769
return this.provider
6870
.request('CloudWatchLogs',
@@ -75,7 +77,6 @@ module.exports = {
7577
throw new this.serverless.classes
7678
.Error('No existing streams for the function alias');
7779
}
78-
7980
return _.map(
8081
_.filter(reply.logStreams, stream => _.includes(stream.logStreamName, `[${version}]`)),
8182
stream => stream.logStreamName);
@@ -124,6 +125,10 @@ module.exports = {
124125
},
125126

126127
functionLogsShowLogs(logStreamNames) {
128+
if (logStreamNames.length === 0) {
129+
throw new this.serverless.classes
130+
.Error('No existing streams for the latest version of function alias. If you want to view logs of a specific function version, please use --version to specify');
131+
}
127132
const formatLambdaLogEvent = event => {
128133
const msgParam = event.message;
129134
let msg = msgParam;

lib/stackInformation.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,4 +181,15 @@ module.exports = {
181181
});
182182
},
183183

184+
aliasGetAliasLatestFunctionVersionByFunctionName(aliasName, functionName) {
185+
return this._provider.request('Lambda',
186+
'getAlias',
187+
{ FunctionName: functionName, Name: aliasName },
188+
this._options.stage,
189+
this._options.region)
190+
.then(result => {
191+
return result.FunctionVersion
192+
})
193+
},
194+
184195
};

0 commit comments

Comments
 (0)