diff --git a/CHANGES_NEXT_RELEASE b/CHANGES_NEXT_RELEASE index dda1c8ec9..d1c9eaf04 100644 --- a/CHANGES_NEXT_RELEASE +++ b/CHANGES_NEXT_RELEASE @@ -1 +1,2 @@ +- Fix: ensure service and subservice from device in logs about error proccesing message - Remove: legacy code about unused parsedMessageError flag diff --git a/lib/commonBindings.js b/lib/commonBindings.js index 15d01c17d..2a4d920c7 100644 --- a/lib/commonBindings.js +++ b/lib/commonBindings.js @@ -36,7 +36,7 @@ const transportSelector = require('./transportSelector'); const async = require('async'); const iotaUtils = require('./iotaUtils'); const constants = require('./constants'); -const context = { +let context = { op: 'IoTAgentJSON.commonBinding' }; const config = require('./configService'); @@ -50,7 +50,7 @@ const config = require('./configService'); function parseMessage(message) { let parsedMessage; let messageArray; - + context = fillService(context, { service: 'n/a', subservice: 'n/a' }); const stringMessage = message.toString(); try { parsedMessage = JSON.parse(stringMessage); @@ -126,6 +126,7 @@ function sendConfigurationToDevice(device, apiKey, deviceId, results, callback) * @param {Object} objMessage Array of JSON object received. */ function manageConfigurationRequest(apiKey, deviceId, device, objMessage) { + const ctxt = fillService(context, device); for (let i = 0; i < objMessage.length; i++) { iotaUtils.manageConfiguration( apiKey, @@ -141,7 +142,7 @@ function manageConfigurationRequest(apiKey, deviceId, device, objMessage) { config .getLogger() .debug( - context, + ctxt, 'Configuration request finished for APIKey [%s] and Device [%s]', apiKey, deviceId @@ -164,6 +165,7 @@ function manageConfigurationRequest(apiKey, deviceId, device, objMessage) { * @param {Object} parsedMessage ParsedMessage (JSON or string) message coming from the client. */ function singleMeasure(apiKey, deviceId, attribute, device, parsedMessage) { + context = fillService(context, device); config.getLogger().debug(context, 'Processing single measure for device [%s] with apiKey [%s]', deviceId, apiKey); const values = [ @@ -175,8 +177,6 @@ function singleMeasure(apiKey, deviceId, attribute, device, parsedMessage) { ]; config.getLogger().debug(context, 'values updates [%s]', JSON.stringify(values)); iotAgentLib.update(device.name, device.type, '', values, device, function (error) { - context.service = device.service; - context.subservice = device.subservice; if (error) { config.getLogger().error( context, @@ -210,6 +210,7 @@ function singleMeasure(apiKey, deviceId, attribute, device, parsedMessage) { */ function multipleMeasures(apiKey, deviceId, device, messageObj) { let measure; + const ctxt = fillService(context, device); config .getLogger() .debug(context, 'Processing multiple measures for device [%s] with apiKey [%s]', deviceId, apiKey); @@ -228,11 +229,9 @@ function multipleMeasures(apiKey, deviceId, device, messageObj) { } iotAgentLib.update(device.name, device.type, '', values, device, function (error) { - context.service = device.service; - context.subservice = device.subservice; if (error) { config.getLogger().error( - context, + ctxt, /*jshint quotmark: double */ "MEASURES-002: Couldn't send the updated values to the Context Broker due to an error: %j", /*jshint quotmark: single */ @@ -242,7 +241,7 @@ function multipleMeasures(apiKey, deviceId, device, messageObj) { config .getLogger() .info( - context, + ctxt, 'Multiple measures for device [%s] with apiKey [%s] successfully updated', deviceId, apiKey @@ -285,21 +284,22 @@ function messageHandler(topic, message, protocol) { // it must be an array of object multipleMeasures(apiKey, deviceId, device, parsedMessage); } else { + context = fillService(context, device); config.getLogger().error( context, /*jshint quotmark: double */ - "Couldn't process message [%s] due to format issues.", + "Couldn't process message [%s] for device [%j] due to format issues.", /*jshint quotmark: single */ - message + message, + device ); } } function processDeviceMeasure(error, device) { if (error) { - let ctx = context; - ctx = fillService(ctx, { service: 'n/a', subservice: 'n/a' }); - config.getLogger().warn(ctx, 'MEASURES-004: Device not found for topic [%s]', topic); + context = fillService(context, { service: 'n/a', subservice: 'n/a' }); + config.getLogger().warn(context, 'MEASURES-004: Device not found for topic [%s]', topic); } else { const localContext = _.clone(context);