UNPKG

logging-n3xgen-cg-lib

Version:

Library to save logs into desire destiny, the current implementation is Opensearch.

101 lines (94 loc) 4.16 kB
const axios = require('axios'); const { constants, helpers, log } = require('utils-nxg-cg'); let { objectLESReq } = require('./les-credentials'); /** * Method to send data like a log type to be stored into elasticsearch * @param payload * @param level * @param uniquetransactionid * @returns {Promise<{headers: RawAxiosResponseHeaders | AxiosResponseHeaders, data: T, status: number}|any>} */ module.exports.logging_n3xgen = async function (payload, properties, level, uniquetransactionid, x_request_id, x_b3_traceid, workflowName, step_name) { try { objectLESReq.api_url = properties.api_url; objectLESReq.auth = properties.auth; const random = helpers.randomNum(); const time = new Date(); const timestamp = Date.now(); const newTime = time.getFullYear() + "/" + ((time.getMonth() + 1).toString().padStart(2, 0)) + "/" + (time.getDate().toString().padStart(2, 0)) + " " + (time.getHours().toString().padStart(2, 0)) + ":" + (time.getMinutes().toString().padStart(2, 0)) + ":" + (time.getSeconds().toString().padStart(2, 0)) + ":" + (time.getMilliseconds().toString().padStart(2, 0)); let status = 'Successful'; let integrationname = `flow-${random}:step_`; if (!uniquetransactionid || uniquetransactionid === '') { uniquetransactionid = `step_0:${time}`; } if (constants.ELASTICIO_LISTEN_MESSAGES_ON) { const componentProp = constants.ELASTICIO_LISTEN_MESSAGES_ON.split(':'); if (componentProp) { integrationname = `${componentProp[0]}:${componentProp[1]}`; if (!uniquetransactionid || uniquetransactionid === '') { uniquetransactionid = `${componentProp[1]}:${time}`; } } } if (level === 'error') { status = 'Unsuccessful'; } //log.info(integrationname); //log.info(uniquetransactionid); if (!payload) throw Error(`${constants.ERROR_PROPERTY} content`); if (typeof payload === "string") { payload = { payload }; } else { if (helpers.isObjectValid(payload)) { payload = { payload }; } else { payload = { "content": payload.toString() }; } } //log.debug('content =', JSON.stringify(contentOIH)); let buff = Buffer.from(JSON.stringify(payload)).toString("base64"); //log.debug('buff', buff); axios.defaults.headers.common['Authorization'] = `${objectLESReq.auth}`; const { data } = await axios( { method: objectLESReq.method, url: objectLESReq.api_url, data: { status, level, payload: buff, uniquetransactionid, integrationname, timestamp: timestamp, date: newTime, x_b3_traceid, x_request_id, workflowName, step_name } } ); return data; } catch (e) { //throw Error(e) if (e.response) { // The request was made and the server responded with a status code // that falls out of the range of 2xx return { data: e.response.data, status: e.response.status, headers: e.response.headers } } else if (e.request) { // The request was made but no response was received // `error.request` is an instance of XMLHttpRequest in the browser and an instance of // http.ClientRequest in node.js return e.request; //console.log(error.request); } else { // Something happened in setting up the request that triggered an Error //console.log('Error', error.message); return e.message; } } };