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,10 +1,18 @@
import fetch from "node-fetch"; import fetch from "node-fetch";
import fetchEbayReadToken from "../utils/fetchEbayReadToken.js"; // Adjust the import path according to your project structure import fetchEbayReadToken from "../utils/fetchEbayReadToken.js"; // Adjust the import path according to your project structure
import { LoggingLevel, smartLogging } from "../utils/helper.js";
export const itemLookup = async (req, res) => { export const itemLookup = async (req, res) => {
smartLogging(LoggingLevel.Debug, ``)
const productCode = req.query.productCode; const productCode = req.query.productCode;
const oauthToken = await fetchEbayReadToken(); let oauthToken;
console.log(productCode); try{
oauthToken = await fetchEbayReadToken();
}catch(e){
smartLogging(LoggingLevel.Error, `Error Getting eBay Token: ${e}`);
}
// console.log(productCode);
smartLogging(LoggingLevel.Logging, `Product Code: ${productCode}`);
try { try {
const response = await fetch( const response = await fetch(
`https://api.ebay.com/buy/browse/v1/item_summary/search?gtin=${productCode}`, `https://api.ebay.com/buy/browse/v1/item_summary/search?gtin=${productCode}`,
@@ -28,7 +36,7 @@ export const itemLookup = async (req, res) => {
res.status(200).send(price); res.status(200).send(price);
} catch (error) { } catch (error) {
console.error("Error fetching data from eBay Browse API:", error); smartLogging(LoggingLevel.Error, `Error fetching data from eBay Browse API: ${error}`);
res.status(500).send("Internal Server Error"); res.status(500).send("Internal Server Error");
} }
}; };

View File

@@ -4,6 +4,7 @@ import cors from "cors";
import dotenv from "dotenv"; import dotenv from "dotenv";
import dataRoutes from "./routes/dataRoutes.js"; import dataRoutes from "./routes/dataRoutes.js";
import inventoryRoutes from "./routes/inventoryRoutes.js"; import inventoryRoutes from "./routes/inventoryRoutes.js";
import { LoggingLevel, setLevel, smartLogging } from "./utils/helper.js";
dotenv.config(); dotenv.config();
@@ -16,5 +17,7 @@ app.use(express.json());
app.use("/api/data", dataRoutes); app.use("/api/data", dataRoutes);
app.use("/api/inventory", inventoryRoutes); app.use("/api/inventory", inventoryRoutes);
//Set Logging Level
setLevel(LoggingLevel.AppTrace); //This should get passed in from the Docker-Compose.
const PORT = process.env.PORT || 3000; const PORT = process.env.PORT || 3000;
app.listen(PORT, () => console.log(`Server running on port ${PORT}`)); app.listen(PORT, () => smartLogging(LoggingLevel.Logging, `Server running on port ${PORT}`));

View File

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

View File

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

View File

@@ -2,6 +2,7 @@ version: "3.8"
services: services:
api: api:
build: ./api build: ./api
name: eBayAPI
ports: ports:
- "3000:3000" - "3000:3000"
volumes: volumes: