Skip to content

Commit

Permalink
Issue #SB-21909 merge: Merge pull request #26 from JagadishPujari/sta…
Browse files Browse the repository at this point in the history
…ndard-log

Issue #SB-21909 feat: Change all logs to follow the standard log form at - Collaboration & Interaction
  • Loading branch information
vinukumar-vs authored Jan 8, 2021
2 parents 8b85daa + 92584ea commit 1732e16
Show file tree
Hide file tree
Showing 5 changed files with 189 additions and 7 deletions.
30 changes: 30 additions & 0 deletions router/api/telemetry/telemetry.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const _ = require('lodash')
var LOG = require('../../log/logger')
const Telemetry = require('sb_telemetry_util')
const TelemetryLogger = require('sb_logger_util')
var UUID = require('uuid')
const telemetry = new Telemetry()
module.exports = {
ver : '',
Expand Down Expand Up @@ -89,5 +91,33 @@ module.exports = {
} catch(e) {
LOG.error('Error while initilising telemetry')
}
},

/**
* for log event
* data object have these properties {'edata', context', 'object', 'tags'}
*/
telemetryLog : function (data, edata) {
try {
var channelId = data.channelId
var appId = data.appId
var env = data.env
var rollup = telemetry.getRollUpData([])
var did = data.deviceId
var mid = 'LOG:' + UUID();
let logData = {
eid: "LOG",
ets: new Date().getTime(),
ver: "3.0",
mid: mid,
actor: telemetry.getActorData(data.userId, 'user'),
context: telemetry.getContextData({ channel: channelId, env: env, pdata: {id: 'org.sunbird.chatbot.router'},cdata: [], rollup: rollup, did: did }),
object: {},
edata: edata
};
TelemetryLogger.info(logData);
} catch (e) {
LOG.error('Error while logger info event')
}
}
}
77 changes: 70 additions & 7 deletions router/appRest.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ appBot.post('/bot', function (req, res) {
env: req.body.appId + '.bot',
sessionId: '',
channelId: req.body.channel,
uaspec: getUserSpec(req)
uaspec: getUserSpec(req),
requestid: req.headers["x-request-id"] ? req.headers["x-request-id"] :"",
}
}
handler(req, res, data)
Expand All @@ -66,12 +67,23 @@ appBot.post('/whatsapp', function (req, res) {
env: config.TELEMETRY_DATA_ENV_WHATSAPP,
channelId: config.TELEMETRY_DATA_CHANNELID_WHATSAPP,
sessionId: '',
uaspec: getUserSpec(req)
uaspec: getUserSpec(req),
requestid: req.headers["x-request-id"] ? req.headers["x-request-id"] :"",
}
}
handler(req, res, data)
} else {
sendErrorResponse(res)
var customData= {
userId: crypto.createHash('sha256').update(req.body.incoming_message[0].from).digest("hex"),
deviceId: crypto.createHash('sha256').update(req.body.incoming_message[0].from).digest("hex"),
appId: config.TELEMETRY_DATA_PID_WHATSAPP,
env: config.TELEMETRY_DATA_ENV_WHATSAPP,
channelId: config.TELEMETRY_DATA_CHANNELID_WHATSAPP,
sessionId: '',
uaspec: getUserSpec(req),
requestid: req.headers["x-request-id"] ? req.headers["x-request-id"] :"",
}
sendErrorResponse(res, customData)
}

})
Expand All @@ -83,6 +95,13 @@ function handler(req, res, data) {
redisSessionData = {};

if (!data.customData.deviceId) {
var edata = {
type: "system",
level: "INFO",
requestid: data.customData.requestid,
message: "Attribute missing from request body"
}
telemetry.telemetryLog(data.customData, edata);
sendResponse(data.customData.deviceId, res, "From attribute missing", 400);
} else {
redisClient.get(REDIS_KEY_PREFIX + data.customData.deviceId, (err, redisValue) => {
Expand Down Expand Up @@ -133,7 +152,15 @@ function handler(req, res, data) {
function freeFlowLogic(data, res, chatflowConfig) {
RasaCoreController.processUserData(data, (err, resp) => {
var response = '';
var edata = {
type: "system",
level: "INFO",
requestid: data.customData.requestid,
message: ""
}
if (err) {
edata.message = "Sorry: Core RASA controller failed to load";
telemetry.telemetryLog(data.customData, edata);
sendChannelResponse(data.customData.deviceId, res, data, 'SORRY')
} else {
var responses = resp.res;
Expand All @@ -156,8 +183,12 @@ function freeFlowLogic(data, res, chatflowConfig) {
}
telemetry.logInteraction(telemetryData);
if (data.channel == config.WHATSAPP) {
edata.message = "Free flow whats app response";
telemetry.telemetryLog(data.customData, edata);
sendResponseWhatsapp(res, response, data.recipient, "freeFlow")
} else {
edata.message = "Free flow web poartal response";
telemetry.telemetryLog(data.customData, edata);
sendResponse(res, response)
}

Expand Down Expand Up @@ -198,6 +229,14 @@ function menuDrivenLogic(data, res, chatflowConfig) {
} else {
responseKey = getErrorMessageForInvalidInput(currentFlowStep, chatflowConfig, true)
menuIntentKnown = false
var edata = {
type: "system",
level: "INFO",
requestid: data.customData.requestid,
message: ""
}
edata.message = "UNKNOWN OPTION";
telemetry.telemetryLog(data.customData, edata);
// TODO : Don't call function inside each if/else if it should be called once.
telemetryData = createInteractionData({ currentStep: currentFlowStep + '_UNKNOWN_OPTION' }, data, false)
}
Expand Down Expand Up @@ -237,8 +276,16 @@ function consolidatedLog(data, responseKey, menuIntentKnown, isMenuDriven) {
intent = config.FREEFLOW_INTENT_NOT_DETECTED
}
}

LOG.info("UserId: " + data.customData.userId + "," + " DeviceId: " + data.customData.deviceId + "," + " UserQuery: " + data.message + "," + " Bot_Response_identifier: " + intent + "," + " BotResponse: " + responseKey)
var message = "UserId: " + data.customData.userId + "," + " DeviceId: " + data.customData.deviceId + "," + " UserQuery: " + data.message + "," + " Bot_Response_identifier: " + intent + "," + " BotResponse: " + responseKey;

var edata = {
type: "system",
level: "INFO",
requestid: data.customData.requestid,
message: message
}
telemetry.telemetryLog(data.customData, edata)
LOG.info(message)
}

function setRedisKeyValue(key, value) {
Expand Down Expand Up @@ -300,7 +347,14 @@ function sendResponse(response, responseBody, responseCode) {
response.send(responseBody)
}

function sendErrorResponse(response){
function sendErrorResponse(response, data){
var edata = {
type: "system",
level: "INFO",
requestid: data.customData.requestid,
message: "401 invalid request"
}
telemetry.telemetryLog(data, edata)
response.status(401);
response.send('invalid request');
}
Expand Down Expand Up @@ -342,7 +396,16 @@ function sendResponseWhatsapp(response,responseBody, recipient, textContent) {
}
function sendChannelResponse(response, responseKey, data, responseCode) {
response.set('Content-Type', 'application/json')
if (responseCode) response.status(responseCode)
if (responseCode){
response.status(responseCode)
var edata = {
type: "system",
level: "INFO",
requestid: data.customData.requestid,
message: "404 not found"
}
telemetry.telemetryLog(data.customData, edata)
}

//version check
var channelResponse = literals.message[responseKey + '_' + data.channel];
Expand Down
74 changes: 74 additions & 0 deletions router/libs/sb_logger_util/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* @file: index.js
* @author: Jagadish P
* @desc: using log4s, enables application wide logging.
*/
var log4js = require('log4js')
var fs = require('fs')
var dir = '../../log'
var configUtil = require('../sb-config-util')

if (!fs.existsSync(dir)) {
fs.mkdirSync(dir)
}

var options = {
'appenders': [{
'type': 'clustered',
'appenders': [
{
'type': 'console'
}, {
'type': 'logLevelFilter',
'level': 'TRACE',
'category': 'api',
'appender': {
'type': 'file',
'maxLogSize': 26214400,
'filename': '../../log/microService.log'
}
}
]
}]
}

log4js.configure(options)
var logger = log4js.getLogger('api')

var isLoggerEnabled = function () {
var loggerEnabled = configUtil.getConfig('ENABLE_LOGGING')
return (loggerEnabled !== undefined && loggerEnabled === 'true')
}

var info = function (data) {
if (data) {
logger.setLevel('INFO')
logger.info(JSON.stringify(data))
}
}

var error = function (data) {
if (data && isLoggerEnabled()) {
logger.setLevel('ERROR')
logger.error(JSON.stringify(data))
}
}

var warn = function (data) {
if (data && isLoggerEnabled()) {
logger.setLevel('WARN')
logger.warn(JSON.stringify(data))
}
}

var trace = function (data) {
if (data && isLoggerEnabled()) {
logger.setLevel('TRACE')
logger.trace(JSON.stringify(data))
}
}

module.exports.info = info
module.exports.error = error
module.exports.warn = warn
module.exports.trace = trace
14 changes: 14 additions & 0 deletions router/libs/sb_logger_util/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "sb_logger_util",
"version": "1.0.0",
"description": "this package serves as log",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Jagadish",
"license": "ISC",
"dependencies": {
"log4js": "^0.6.29"
}
}
1 change: 1 addition & 0 deletions router/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"ua-parser-js": "^0.7.13",
"sb_telemetry_util": "file:libs/sb_telemetry_util",
"sb_config_util": "file:libs/sb-config-util",
"sb_logger_util": "file:libs/sb_logger_util",
"redis": "^3.0.2",
"request": "^2.88.0",
"socket.io": "^2.3.0",
Expand Down

0 comments on commit 1732e16

Please sign in to comment.