Adding new logging function

This commit is contained in:
2024-03-22 19:01:18 -05:00
parent 6dddc5799f
commit cac674e27a
6 changed files with 69 additions and 6 deletions

View File

@@ -1,7 +1,9 @@
// Need to figoure out expiration and make sure to cycle this appropriately to avoid unnecessary calls
import fetch from "node-fetch";
import { LoggingLevel, smartLogging } from "./helper.js";
const fetchEbayReadToken = async () => {
smartLogging(LoggingLevel.AppTrace, `fetchEbayReadToken`);
const ebayClientId = process.env.EBAY_CLIENT_ID;
const ebayClientSecret = process.env.EBAY_CLIENT_SECRET;
const credentials = Buffer.from(
@@ -9,6 +11,7 @@ const fetchEbayReadToken = async () => {
).toString("base64");
try {
smartLogging(LoggingLevel.Debug, `Fetching Token from eBay`);
const response = await fetch(
"https://api.ebay.com/identity/v1/oauth2/token",
{
@@ -31,7 +34,7 @@ const fetchEbayReadToken = async () => {
const data = await response.json();
return data.access_token;
} catch (error) {
console.error("Error fetching eBay OAuth token:", error);
smartLogging(LoggingLevel.Error, `Error fetching eBay OAuth token: ${error}`);
throw error; // Throw the error to be handled by the caller
}
};

View File

@@ -1,6 +1,8 @@
import fetch from "node-fetch";
import { LoggingLevel, smartLogging } from "./helper.js";
const fetchEbayUserToken = async (authorizationCode) => {
smartLogging(LoggingLevel.AppTrace, `fetchEbayUserToken`);
const clientId = process.env.EBAY_CLIENT_ID;
const clientSecret = process.env.EBAY_CLIENT_SECRET;
const redirectUri = process.env.EBAY_REDIRECT_URI; // Make sure this matches the URI registered with eBay
@@ -33,7 +35,7 @@ const fetchEbayUserToken = async (authorizationCode) => {
const data = await response.json();
return data.access_token; // This is the User access token
} catch (error) {
console.error("Error fetching eBay user token:", error);
smartLogging(LoggingLevel.Error,`Error fetching eBay user token: ${JSON.stringify(error)}`);
throw error;
}
};

46
api/utils/helper.js Normal file
View File

@@ -0,0 +1,46 @@
export let setlvl = undefined;
export function setLevel(level){
console.log(`Setting logging level to: ${level}`);
setlvl = level;
}
export function smartLogging(level, msg){
if(setlvl == undefined){ //If no level is set treat it as console.log()
console.log(msg);
}else if(level <= setlvl){
switch (level) {
case LoggingLevel.Error:
console.error(msg);
break;
case LoggingLevel.Warning:
console.warn(msg);
break;
case LoggingLevel.Logging:
console.log(msg);
break;
case LoggingLevel.Debug:
console.debug(msg);
case LoggingLevel.AppTrace:
console.debug(msg);
break;
case LoggingLevel.Trace:
console.trace(msg);
break;
default:
console.log(msg);
}
}
}
export const LoggingLevel = {
Error: "10",
Warning: "9",
Logging: "8",
Debug: "7",
AppTrace: "6",
Trace: "5"
}