Skip to content

Commit 76341ed

Browse files
committed
Added option for pretty-printing event logging
1 parent cec083e commit 76341ed

File tree

4 files changed

+47
-12
lines changed

4 files changed

+47
-12
lines changed

lib/smart-app.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ module.exports = class SmartApp {
1616
this._clientId = options.clientId;
1717
this._clientSecret = options.clientSecret;
1818
this._id = options.appId;
19-
this._log = new Log(options.logger, options.enableEventLogging);
19+
this._log = new Log(options.logger, options.jsonSpace, options.enableEventLogging);
2020
this._permissions = options.permissions ? options.permissions : [];
2121
this._disableCustomDisplayName = options.disableCustomDisplayName === undefined ? false : options.disableCustomDisplayName;
2222
this._disableRemoveApp = options.disableRemoveApp === undefined ? false : options.disableRemoveApp;
@@ -77,12 +77,12 @@ module.exports = class SmartApp {
7777
return this;
7878
}
7979

80-
configureLogger(logger, enableEvents = false) {
81-
this._log = new Log(logger, enableEvents);
80+
configureLogger(logger, jsonSpace=null, enableEvents = false) {
81+
this._log = new Log(logger, jsonSpace, enableEvents);
8282
}
8383

84-
enableEventLogging(enabled = true) {
85-
this._log.enableEvents(enabled);
84+
enableEventLogging(jsonSpace=null, enableEvents = true) {
85+
this._log.enableEvents(jsonSpace, enableEvents);
8686
return this;
8787
}
8888

lib/util/log.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ const winston = require('winston');
77
*/
88
module.exports = class Log {
99

10-
constructor(logger, enableEvents = false) {
10+
constructor(logger, jsonSpace = null, enableEvents = false) {
1111
this._eventsEnabled = enableEvents;
12+
this._jsonSpace = jsonSpace;
1213
if (!logger) {
1314
this._logger = winston.createLogger({
1415
level: 'debug',
@@ -24,7 +25,7 @@ module.exports = class Log {
2425
event (evt, suffix = '') {
2526
if ( this._eventsEnabled) {
2627
try {
27-
this._logger.log ('debug', `${evt.lifecycle}${suffix ? `/${suffix}` : ''}\nREQUEST: ${JSON.stringify(evt, null, 2)}`);
28+
this._logger.log ('debug', `${evt.lifecycle}${suffix ? `/${suffix}` : ''}\nREQUEST: ${JSON.stringify(evt, null, this._jsonSpace)}`);
2829
}
2930
catch (e) {
3031
this._logger.log ('error', `${evt.lifecycle}${suffix ? `/${suffix}` : ''}\nREQUEST: ${e}`);
@@ -35,7 +36,7 @@ module.exports = class Log {
3536
response (data) {
3637
if ( this._eventsEnabled) {
3738
try {
38-
this._logger.log('debug', `RESPONSE: ${JSON.stringify(data, null, 2)}`);
39+
this._logger.log('debug', `RESPONSE: ${JSON.stringify(data, null, this._jsonSpace)}`);
3940
}
4041
catch (e) {
4142
this._logger.log('debug', `RESPONSE: ${e}`);
@@ -63,7 +64,8 @@ module.exports = class Log {
6364
this._logger.log ('error', msg);
6465
}
6566

66-
enableEvents (value) {
67-
this._eventsEnabled = value;
67+
enableEvents (jsonSpace, enabled = true) {
68+
this._eventsEnabled = enabled;
69+
this._jsonSpace = jsonSpace;
6870
}
6971
};

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@smartthings/smartapp",
3-
"version": "1.0.0",
3+
"version": "1.0.1",
44
"description": "SDK for SmartApps using Node JS",
55
"author": "SmartThings",
66
"contributors": [

test/smartapp-page-spec.js

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ const {expect} = require('chai');
33
const SmartApp = require('../lib/smart-app');
44

55
describe ('smartapp-page-spec', () => {
6+
67
it('should set page ID', () => {
78
const app = new SmartApp();
89
app.appId('xxx');
9-
app.page('eaMainPage', (page) => {
10+
app.page('eaMainPage', (ctx, page) => {
1011
page.section('whenDoorOpensAndCloses', (section) => {
1112
section.deviceSetting('contactSensor')
1213
.capabilities(['contactSensor']);
@@ -63,4 +64,36 @@ describe ('smartapp-page-spec', () => {
6364
console.log(JSON.stringify(initResponse, null, 2));
6465
console.log(JSON.stringify(pageReponse, null, 2));
6566
});
67+
68+
it('should configure event logger', () => {
69+
const app = new SmartApp();
70+
app.appId('xxx');
71+
app.enableEventLogging(4);
72+
app.page('eaMainPage', (ctx, page) => {
73+
page.section('whenDoorOpensAndCloses', (section) => {
74+
section.deviceSetting('contactSensor')
75+
.capabilities(['contactSensor']);
76+
});
77+
});
78+
79+
app.handleMockCallback({
80+
'lifecycle': 'CONFIGURATION',
81+
'executionId': 'e6903fe6-f88f-da69-4c12-e2802606ccbc',
82+
'locale': 'en',
83+
'version': '0.1.0',
84+
'client': {
85+
'os': 'ios',
86+
'version': '0.0.0',
87+
'language': 'en-US'
88+
},
89+
'configurationData': {
90+
'installedAppId': '7d7fa36d-0ad9-4893-985c-6b75858e38e4',
91+
'phase': 'INITIALIZE',
92+
'pageId': '',
93+
'previousPageId': '',
94+
'config': {}
95+
},
96+
'settings': {}
97+
});
98+
});
6699
});

0 commit comments

Comments
 (0)