From cac674e27a03a8a506e5e816a31aaa6a8fbe73f5 Mon Sep 17 00:00:00 2001 From: Garrett Woodford Date: Fri, 22 Mar 2024 19:01:18 -0500 Subject: [PATCH 1/2] Adding new logging function --- api/controllers/dataController.js | 14 ++++++++-- api/server.js | 5 +++- api/utils/fetchEbayReadToken.js | 5 +++- api/utils/fetchEbayUserToken.js | 4 ++- api/utils/helper.js | 46 +++++++++++++++++++++++++++++++ docker-compose.yaml | 1 + 6 files changed, 69 insertions(+), 6 deletions(-) create mode 100644 api/utils/helper.js diff --git a/api/controllers/dataController.js b/api/controllers/dataController.js index aa97885..7105c9f 100644 --- a/api/controllers/dataController.js +++ b/api/controllers/dataController.js @@ -1,10 +1,18 @@ import fetch from "node-fetch"; 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) => { + smartLogging(LoggingLevel.Debug, ``) const productCode = req.query.productCode; - const oauthToken = await fetchEbayReadToken(); - console.log(productCode); + let oauthToken; + try{ + oauthToken = await fetchEbayReadToken(); + }catch(e){ + smartLogging(LoggingLevel.Error, `Error Getting eBay Token: ${e}`); + } + // console.log(productCode); + smartLogging(LoggingLevel.Logging, `Product Code: ${productCode}`); try { const response = await fetch( `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); } 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"); } }; diff --git a/api/server.js b/api/server.js index 5c8234d..8b9e329 100644 --- a/api/server.js +++ b/api/server.js @@ -4,6 +4,7 @@ import cors from "cors"; import dotenv from "dotenv"; import dataRoutes from "./routes/dataRoutes.js"; import inventoryRoutes from "./routes/inventoryRoutes.js"; +import { LoggingLevel, setLevel, smartLogging } from "./utils/helper.js"; dotenv.config(); @@ -16,5 +17,7 @@ app.use(express.json()); app.use("/api/data", dataRoutes); 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; -app.listen(PORT, () => console.log(`Server running on port ${PORT}`)); +app.listen(PORT, () => smartLogging(LoggingLevel.Logging, `Server running on port ${PORT}`)); diff --git a/api/utils/fetchEbayReadToken.js b/api/utils/fetchEbayReadToken.js index e7c18f6..cd38ce8 100644 --- a/api/utils/fetchEbayReadToken.js +++ b/api/utils/fetchEbayReadToken.js @@ -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 } }; diff --git a/api/utils/fetchEbayUserToken.js b/api/utils/fetchEbayUserToken.js index 216aa5e..6684b0b 100644 --- a/api/utils/fetchEbayUserToken.js +++ b/api/utils/fetchEbayUserToken.js @@ -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; } }; diff --git a/api/utils/helper.js b/api/utils/helper.js new file mode 100644 index 0000000..76089de --- /dev/null +++ b/api/utils/helper.js @@ -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" +} \ No newline at end of file diff --git a/docker-compose.yaml b/docker-compose.yaml index 67c4c6f..237636e 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -2,6 +2,7 @@ version: "3.8" services: api: build: ./api + name: eBayAPI ports: - "3000:3000" volumes: -- 2.49.1 From 12d4bb06f2e99855cbfed786d2ff1e1fba7f4d17 Mon Sep 17 00:00:00 2001 From: Garrett Woodford Date: Fri, 22 Mar 2024 19:19:46 -0500 Subject: [PATCH 2/2] Changed a few more logging statements --- api/controllers/dataController.js | 4 ++-- api/controllers/inventoryController.js | 6 ++++-- docker-compose.yaml | 2 +- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/api/controllers/dataController.js b/api/controllers/dataController.js index 7105c9f..179d53e 100644 --- a/api/controllers/dataController.js +++ b/api/controllers/dataController.js @@ -3,7 +3,7 @@ import fetchEbayReadToken from "../utils/fetchEbayReadToken.js"; // Adjust the i import { LoggingLevel, smartLogging } from "../utils/helper.js"; export const itemLookup = async (req, res) => { - smartLogging(LoggingLevel.Debug, ``) + smartLogging(LoggingLevel.Debug, `dataController.itemLookup()`); const productCode = req.query.productCode; let oauthToken; try{ @@ -11,7 +11,7 @@ export const itemLookup = async (req, res) => { }catch(e){ smartLogging(LoggingLevel.Error, `Error Getting eBay Token: ${e}`); } - // console.log(productCode); + smartLogging(LoggingLevel.Logging, `Product Code: ${productCode}`); try { const response = await fetch( diff --git a/api/controllers/inventoryController.js b/api/controllers/inventoryController.js index 8dbecd7..e5b1468 100644 --- a/api/controllers/inventoryController.js +++ b/api/controllers/inventoryController.js @@ -2,12 +2,14 @@ import buildAddFixedPriceItemRequestXML from "../utils/buildAddFixedPriceItemRequestXML.js"; import fetch from "node-fetch"; import fetchEbayUserToken from "../utils/fetchEbayUserToken.js"; // Adjust the import path according to your project structure +import { LoggingLevel, smartLogging } from "../utils/helper.js"; export const addItem = async (req, res) => { + smartLogging(LoggingLevel.AppTrace, `inventoryController.addItem`); const itemDetails = req.body; // Assuming item details are sent in the request body itemDetails.userToken = "v^1.1#i^1#r^0#I^3#f^0#p^3#t^H4sIAAAAAAAAAOVZf2wbVx2P86Nd1HWAViWsLZrntQMtPd+7O9vnO2ozJ3bidPnhxnaXBKHo3d075zX3q/fu4ngTKI22wIS6CaqhijIUxoSEJjQ2YCCtUHVD2h/AtE6CakKiILSu4w8KRQwmYOLO+VE3U3/EDqol/M/53n1/fb4/770D81s671/MLv5je2Br69I8mG8NBJhtoHNLR88dba07O1pADUFgaX7PfPtC28X9BOqaJY4hYpkGQcE5XTOIWF1MhFzbEE1IMBENqCMiOrKYTw0PiWwYiJZtOqZsaqHgYDoR4gQ1zsQERQE8w3AxwVs1VmUWzEQIMjwAQkyNCHxc5TjgPSfERYMGcaDhJEIsYCMU4CiWKTCCGAUiAGGBFyZDw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8rJKCVJMiktSY8PLarowb9ZYJv7urWFoK+/wmwfP5yeeAGjhsB/YsGzqtAldZ9pfmqpaHbwZIpp4u7/w8ubfkxy2EVRMQ6vUw7wBHmzMevtF067Uo3CNeQM8UJZN13DqUbfCugEO1dVUrGn+oUA9CmvYN2KmAbWKg2VSl0ps+NlGNsBiwUoVoIKJ5dfLTXF6azqyZRTGyvKRYz3G2shTWD0NqotpgyrXTDZMB6tYXpZBXInINrY+bIVf6/XLqscfxKuFDYVumWFNVWPba6RgG8nOlGvj5hoB1ck35Y0+gqh1U5DSkDKjV8yGkPuebcYjk1wqn39odCzdELg0mm22NxlV5RDgZECxKKZQkRirUt6LG0OpcZ6PA6hADjZ20rfxc6L2o7/8H4NmeIYBURAVhJuFtm6h5nj6Q98o6Ks/FyZbqj9mIfAKWAj8rDUQAPvBXuZecM+WtmJ72+07CXa8zg3VMMElAzqujcIzqGJBbLfe2XL5maeyfTszo1+7/5FC5Y2Tr7XcXvO1culz4ONr3ys725htNR8vwe4rTzqYj3RvZyOAYxlvtwUAmAT3XnnaznS170DfPVe66y8zj59/7zL9aHLg6HPHXimC7WtEgUBHS/tCoGXhAv3uyO/Gz8gf7DjPn7rjm7suJWInTle2HrvISn8MHbv75B9gl/DkF9+57e7vLQ088dzx9NP2qy+h0z+U9m6d6/70j8+d/cF7u5a+3fc6aQlkfv+VF8Lf+flPs/R/7jox0bP13d17U0v3DL5zZvGjv33xr2/96/BTn1/s7tzdsfTsSBeZ/0Tuto4dU5k9f+rdta+r+Frq/e7Pvn36+Y6Xf/Po+08kzh4dv/TMBz/ZfuLJb2Tf1sao7IL6z5dePv6jT77xq19MHy/NPuYKr3+r/yz/sS/8+q39X7Ze7DrX/7evX3h2T/bvb54ipx54IT+9T7tA/1u5RN/32Kcun7l47s/7WvJvSo+3fX9x29NffeAzrx7ofOT8l86c3Ne9HMv/AqyXZcRHHgAA"; - console.log(itemDetails); + smartLogging(LoggingLevel.Logging, `${itemDetails}`); const xmlRequest = buildAddFixedPriceItemRequestXML(itemDetails); try { @@ -35,7 +37,7 @@ export const addItem = async (req, res) => { res.json({ success: true, data: responseData }); // Send back a success response } catch (error) { - console.error("Error adding item to eBay:", error); + smartLogging(LoggingLevel.Error,`Error adding item to eBay: ${error}); res.status(500).json({ success: false, message: "Failed to add item to eBay", diff --git a/docker-compose.yaml b/docker-compose.yaml index 237636e..28412e3 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -2,7 +2,7 @@ version: "3.8" services: api: build: ./api - name: eBayAPI + container_name: eBayAPI ports: - "3000:3000" volumes: -- 2.49.1