DigitalConsumer.FixMyGlass/src/helpers/logger.js
2024-08-27 16:18:22 -04:00

91 lines
3 KiB
JavaScript

import { applicationConfig } from "@/constants/application-config.js";
import axios from "axios";
import store from "@/store";
import loggingEndpointMethods from "@/constants/logging-endpoint-methods";
import { headerKeys } from "@/constants/header-keys";
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) {
return `Application: ${applicationConfig.APPLICATION_NAME}\n${message}\n${
details ? JSON.stringify(details, undefined, 2) : ""
}`;
}
writeLogEntry(endpoint, logEntry) {
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.getters.experimentSettings),
};
axios({
method: "POST",
url: `${url}/${endpoint}`,
data: { entry: logEntry },
crossDomain: true,
responseType: {},
headers: headers,
}).then(
(response) => {
return resolve(response);
},
(error) => {
return reject(error);
}
);
});
}
}
export default Logger;