DigitalConsumer.ISS/src/helpers/logger.js

124 lines
4.6 KiB
JavaScript

import applicationConfig from '@/constants/application-config.js';
import axios from 'axios';
import { useMainStore } from '@/store';
import loggingEndpointMethods from '@/constants/logging-endpoint-methods';
import headerKeys from '@/constants/header-keys';
import { getSessionKeyValue } from '@/helpers/cookie-helper';
export class Logger {
logInformation(message, details) {
this.writeLogEntry(
loggingEndpointMethods.LOG_INFORMATION,
this.formatLogEntry(message, details)
);
}
logWarning(message, details) {
this.writeLogEntry(
loggingEndpointMethods.LOG_WARNING,
this.formatLogEntry(message, details)
);
}
logError(message, details) {
this.writeLogEntry(loggingEndpointMethods.LOG_ERROR, this.formatLogEntry(message, details));
}
logCritical(message, details) {
this.writeLogEntry(
loggingEndpointMethods.LOG_CRITICAL,
this.formatLogEntry(message, details)
);
}
formatLogEntry(message, details) {
let json = '';
const store = useMainStore();
const sessionKey = getSessionKeyValue();
try
{
if (details) {
if (details instanceof Error) {
json = JSON.stringify(details, Object.getOwnPropertyNames(details), 2);
} else {
json = JSON.stringify(details, undefined, 2)
}
}
} catch (error) {
json = `Failed to serialize log details: ${error}`;
}
return [
`Application: ${applicationConfig.APPLICATION_NAME}`,
`Build Version: ${store.issConfig?.buildInfo?.versionString}`,
`Build Time: ${store.issConfig?.buildInfo?.buildTime}`,
`ClientTag: ${store.issConfig?.clientTag}`,
`ParentAccountNumber: ${store.issConfig?.parentAccountNumber}`,
`ClientName: ${store.issConfig?.clientName}`,
`LastPageVisited: ${store.applicationUser?.lastPageVisited}`,
`SessionLogSequenceNumber: ${sessionKey}`,
`ReferralSequenceNumber: ${store.order?.referralSequenceNumber}`,
`ReferralNumber: ${store.order?.referralNumber}`,
`EON: ${store.order?.eon}`,
`Message: ${message}`,
`JSON: ${json}`
].join('\n');
}
writeLogEntry(endpoint, logEntry) {
const store = useMainStore();
const sessionKey = getSessionKeyValue();
return new Promise((resolve, reject) => {
// If running locally or in the Dev environment show the log entries in the console.
if (
applicationConfig.CURRENT_ENVIRONMENT === 'Localhost'
|| applicationConfig.CURRENT_ENVIRONMENT === 'Dev'
|| applicationConfig.CURRENT_ENVIRONMENT === 'SysTest'
) {
switch (endpoint) {
case loggingEndpointMethods.LOG_INFORMATION:
console.info(logEntry);
break;
case loggingEndpointMethods.LOG_WARNING:
console.warn(logEntry);
break;
case loggingEndpointMethods.LOG_ERROR:
console.error(logEntry);
break;
case loggingEndpointMethods.LOG_CRITICAL:
console.error(logEntry);
break;
default:
console.log(logEntry);
}
}
const url =
applicationConfig.CONSUMER_CF_DISTRO + applicationConfig.FRONTEND_LOGGER_PATH;
const headers = {
[headerKeys.EXPERIMENT]: JSON.stringify(store.experimentSettings),
[headerKeys.APPLICATION_NAME]: applicationConfig.APPLICATION_NAME,
[headerKeys.TRANSACTION_ID]: crypto.randomUUID(),
[headerKeys.ENTERPRISE_ORDER_NUMBER]: store.order.eon,
[headerKeys.REFERRAL_SEQUENCE_NUMBER]: store.order.referralSequenceNumber,
[headerKeys.SESSION_SEQUENCE_NUMBER]: sessionKey
};
axios({
method: 'POST',
url: `${url}/${endpoint}`,
data: { entry: logEntry },
crossDomain: true,
responseType: {},
headers
}).then(
(response) => resolve(response),
(error) => reject(error)
);
});
}
}
export default Logger;