Skip to content

Commit

Permalink
Add option to change log level for websocket logs
Browse files Browse the repository at this point in the history
  • Loading branch information
tinexw committed Dec 2, 2019
1 parent cd93739 commit 781cf79
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 4 deletions.
13 changes: 13 additions & 0 deletions docs/providers/aws/events/websocket.md
Original file line number Diff line number Diff line change
Expand Up @@ -212,3 +212,16 @@ provider:
```

The log streams will be generated in a dedicated log group which follows the naming schema `/aws/websocket/{service}-{stage}`.

The default log level will be INFO. You can change this to error with the following:

```yml
# serverless.yml
provider:
name: aws
logs:
websocket:
level: ERROR
```

Valid values are INFO, ERROR.
22 changes: 19 additions & 3 deletions lib/plugins/aws/package/compile/events/websockets/lib/stage.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

const BbPromise = require('bluebird');
const ensureApiGatewayCloudWatchRole = require('../../lib/ensureApiGatewayCloudWatchRole');
const ServerlessError = require('../../../../../../../classes/Error').ServerlessError;

const defaultLogLevel = 'INFO';
const validLogLevels = new Set(['INFO', 'ERROR']);

module.exports = {
compileStage() {
Expand All @@ -15,7 +19,7 @@ module.exports = {
if (provider.apiGateway && provider.apiGateway.websocketApiId) return null;

// logs
const logsEnabled = provider.logs && provider.logs.websocket;
const logs = provider.logs && provider.logs.websocket;

const stageLogicalId = this.provider.naming.getWebsocketsStageLogicalId();
const logGroupLogicalId = this.provider.naming.getWebsocketsLogGroupLogicalId();
Expand All @@ -35,7 +39,19 @@ module.exports = {

Object.assign(cfTemplate.Resources, { [stageLogicalId]: stageResource });

if (!logsEnabled) return null;
if (!logs) return null;

let level = defaultLogLevel;
if (logs.level) {
level = logs.level;
if (!validLogLevels.has(level)) {
throw new ServerlessError(
`provider.logs.websocket.level is set to an invalid value. Support values are ${Array.from(
validLogLevels
).join(', ')}, got ${level}.`
);
}
}

// create log-specific resources
Object.assign(stageResource.Properties, {
Expand All @@ -54,7 +70,7 @@ module.exports = {
},
DefaultRouteSettings: {
DataTraceEnabled: true,
LoggingLevel: 'INFO',
LoggingLevel: level,
},
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
'use strict';

const expect = require('chai').expect;
const chai = require('chai');

const expect = chai.expect;
const sinon = require('sinon');
const BbPromise = require('bluebird');
const _ = require('lodash');
Expand All @@ -9,6 +11,8 @@ const AwsCompileWebsocketsEvents = require('../index');
const Serverless = require('../../../../../../../Serverless');
const AwsProvider = require('../../../../../provider/awsProvider');

chai.use(require('chai-as-promised'));

describe('#compileStage()', () => {
let awsCompileWebsocketsEvents;
let stageLogicalId;
Expand Down Expand Up @@ -151,6 +155,34 @@ describe('#compileStage()', () => {
});
});

it('should use valid logging level', () => {
awsCompileWebsocketsEvents.serverless.service.provider.logs = {
websocket: {
level: 'ERROR',
},
};

return awsCompileWebsocketsEvents.compileStage().then(() => {
const resources =
awsCompileWebsocketsEvents.serverless.service.provider.compiledCloudFormationTemplate
.Resources;

expect(resources[stageLogicalId].Properties.DefaultRouteSettings.LoggingLevel).equal(
'ERROR'
);
});
});

it('should reject invalid logging level', () => {
awsCompileWebsocketsEvents.serverless.service.provider.logs = {
websocket: {
level: 'FOOBAR',
},
};

expect(awsCompileWebsocketsEvents.compileStage()).to.be.rejectedWith('invalid value');
});

it('should ensure ClousWatch role custom resource', () => {
return awsCompileWebsocketsEvents.compileStage().then(() => {
const resources =
Expand Down

0 comments on commit 781cf79

Please sign in to comment.