Skip to content

Commit

Permalink
Log refactor (#28)
Browse files Browse the repository at this point in the history
* Refactors logging to make it easier to turn up and down logging without having to redploy, standardizes on single quotes over double.

Signed-off-by: Dan Cunningham <dan@digitaldan.com>

* change entry to debug

Signed-off-by: Dan Cunningham <dan@digitaldan.com>

* Fix order of log levels

Signed-off-by: Dan Cunningham <dan@digitaldan.com>

* set default to debug

Signed-off-by: Dan Cunningham <dan@digitaldan.com>
  • Loading branch information
digitaldan authored May 13, 2017
1 parent dd00c1a commit 3c3d0df
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 114 deletions.
19 changes: 9 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* http://www.eclipse.org/legal/epl-v10.html
*/

var log = require('./log.js');
var utils = require('./utils.js');
var oh2 = require('./oh2.js');

Expand All @@ -15,12 +16,11 @@ var oh2 = require('./oh2.js');
* Incoming events from Alexa Lighting APIs are processed via this method.
*/
exports.handler = function (event, context) {
// DEBUG
//utils.log('Input', JSON.stringify(event));
log.debug('Input: ' + JSON.stringify(event));

switch (event.header.namespace) {
/**
* The namespace of "Discovery" indicates a request is being made to the lambda for
* The namespace of 'Discovery' indicates a request is being made to the lambda for
* discovering all appliances associated with the customer's appliance cloud account.
* can use the accessToken that is made available as part of the payload to determine
* the customer.
Expand All @@ -30,8 +30,8 @@ exports.handler = function (event, context) {
break;

/**
* The namespace of "Control" indicates a request is being made to us to turn a
* given device on, off or brighten. This message comes with the "appliance"
* The namespace of 'Control' indicates a request is being made to us to turn a
* given device on, off or brighten. This message comes with the 'appliance'
* parameter which indicates the appliance that needs to be acted on.
*/
case 'Alexa.ConnectedHome.Control':
Expand All @@ -45,15 +45,15 @@ exports.handler = function (event, context) {
*/
case 'Alexa.ConnectedHome.System':
// TODO - handle unhealthy device responses
if (event.header.name === "HealthCheckRequest") {
if (event.header.name === 'HealthCheckRequest') {
var headers = {
messageId: event.header.messageId,
name: event.header.name.replace("Request", "Response"),
name: event.header.name.replace('Request', 'Response'),
namespace: event.header.namespace,
payloadVersion: event.header.payloadVersion
};
var payloads = {
description: "The system is currently healthy",
description: 'The system is currently healthy',
isHealthy: true
};
var result = {
Expand All @@ -69,8 +69,7 @@ exports.handler = function (event, context) {
* We received an unexpected message
*/
default:
// DEBUG
utils.log('Err', 'No supported namespace: ' + event.header.namespace);
log.error('No supported namespace: ' + event.header.namespace);
context.done(null, utils.generateControlError(event.header.messageId, event.header.name, 'DependentServiceUnavailableError', 'Something went wrong...'));
break;
}
Expand Down
52 changes: 52 additions & 0 deletions log.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
* Logging helpers for lambda
* Set enviroment variable LOG_LEVEL to one of the following LEVELS to control
* verbosity of logging.
*/

var LEVELS = [
'TRACE',
'DEBUG',
'INFO',
'WARN',
'ERROR',
];

var DEFAULT = 'DEBUG';

function log(level, message) {
var setLevel = LEVELS.indexOf(process.env.LOG_LEVEL ?
process.env.LOG_LEVEL : DEFAULT);
if (LEVELS.indexOf(level) < setLevel) {
return;
}
switch (level) {
case 'INFO':
console.info(message);
break;
case 'WARN':
console.warn(message);
break;
case 'ERROR':
console.error(message);
break;
default: // debug, trace
console.log(message);
break;
}
}
module.exports.trace = function (message) {
log('TRACE', message);
};
module.exports.debug = function (message) {
log('DEBUG', message);
};
module.exports.info = function (message) {
log('INFO', message);
};
module.exports.warn = function (message) {
log('WARN', message);
};
module.exports.error = function (message) {
log('ERROR', message);
};
Loading

0 comments on commit 3c3d0df

Please sign in to comment.