From ccd3276f454340d9e84ab5ec97dbded08aa36325 Mon Sep 17 00:00:00 2001 From: Jonatan Witoszek Date: Fri, 18 Aug 2023 13:57:33 +0200 Subject: [PATCH] Add logger --- deno.lock | 10 ++++++++++ import_map.json | 1 + invariant.ts | 3 ++- logger.ts | 13 +++++++++++++ main.ts | 16 +++++++++++++++- utils.ts | 3 +++ 6 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 logger.ts diff --git a/deno.lock b/deno.lock index a7f5814..3040f81 100644 --- a/deno.lock +++ b/deno.lock @@ -11,6 +11,16 @@ "https://deno.land/std@0.160.0/async/pool.ts": "ef9eb97b388543acbf0ac32647121e4dbe629236899586c4d4311a8770fbb239", "https://deno.land/std@0.160.0/async/tee.ts": "9af3a3e7612af75861308b52249e167f5ebc3dcfc8a1a4d45462d96606ee2b70", "https://deno.land/std@0.160.0/http/server.ts": "e99c1bee8a3f6571ee4cdeb2966efad465b8f6fe62bec1bdb59c1f007cc4d155", + "https://deno.land/std@0.198.0/assert/assert.ts": "9a97dad6d98c238938e7540736b826440ad8c1c1e54430ca4c4e623e585607ee", + "https://deno.land/std@0.198.0/assert/assertion_error.ts": "4d0bde9b374dfbcbe8ac23f54f567b77024fb67dbb1906a852d67fe050d42f56", + "https://deno.land/std@0.198.0/bytes/copy.ts": "939d89e302a9761dcf1d9c937c7711174ed74c59eef40a1e4569a05c9de88219", + "https://deno.land/std@0.198.0/fmt/colors.ts": "a7eecffdf3d1d54db890723b303847b6e0a1ab4b528ba6958b8f2e754cf1b3bc", + "https://deno.land/std@0.198.0/fs/exists.ts": "29c26bca8584a22876be7cb8844f1b6c8fc35e9af514576b78f5c6884d7ed02d", + "https://deno.land/std@0.198.0/io/buf_writer.ts": "48c33c8f00b61dcbc7958706741cec8e59810bd307bc6a326cbd474fe8346dfd", + "https://deno.land/std@0.198.0/log/handlers.ts": "38871ecbfa67b0d39dc3384210439ac9a13cba6118b912236f9011b5989b9a4d", + "https://deno.land/std@0.198.0/log/levels.ts": "6309147664e9e008cd6671610f2505c4c95f181f6bae4816a84b33e0aec66859", + "https://deno.land/std@0.198.0/log/logger.ts": "257ceb47e3f5f872068073de9809b015a7400e8d86dd40563c1d80169e578f71", + "https://deno.land/std@0.198.0/log/mod.ts": "cc1989f3290b792271f41f4cba392a367520564efbc892be1e00dee423ccd3d9", "https://deno.land/x/wren@0.8.0/mod.ts": "4516d9ea9393e466c94bcc525768a74af9efb7799e5df7e4b0e45474251d5f81", "https://deno.land/x/wren@0.8.0/response.ts": "83bf67e3a39cfd61c38b912916f4a3cb98446320b0254c62d34c897c15daa31e", "https://deno.land/x/wren@0.8.0/route.ts": "b1acd71c4664799edcf216d0d37c6ebab1d57ea674f5e8a72baa4bf28aaf3383", diff --git a/import_map.json b/import_map.json index 2321025..9ff83a7 100644 --- a/import_map.json +++ b/import_map.json @@ -1,6 +1,7 @@ { "imports": { "http/": "https://deno.land/std@0.160.0/http/", + "log/": "https://deno.land/std@0.198.0/log/", "wren/": "https://deno.land/x/wren@0.8.0/" } } diff --git a/invariant.ts b/invariant.ts index b2b08b0..8944f27 100644 --- a/invariant.ts +++ b/invariant.ts @@ -1,6 +1,7 @@ // From https://github.com/alexreardon/tiny-invariant/blob/master/src/tiny-invariant.ts -const isDenoDeploy: boolean = Deno.env.get("DENO_DEPLOYMENT_ID") !== undefined; +import { isDenoDeploy } from "./utils.ts"; + const prefix: string = "Invariant failed"; // Throw an error if the condition fails diff --git a/logger.ts b/logger.ts new file mode 100644 index 0000000..7a93168 --- /dev/null +++ b/logger.ts @@ -0,0 +1,13 @@ +import * as log from "log/mod.ts"; +import { isDenoDeploy } from "./utils.ts"; + +log.setup({ + handlers: { + console: new log.handlers.ConsoleHandler("DEBUG"), + }, + loggers: { + default: { + level: isDenoDeploy ? "INFO" : "DEBUG", + }, + }, +}); diff --git a/main.ts b/main.ts index 9c7bb99..d5d03c9 100644 --- a/main.ts +++ b/main.ts @@ -19,6 +19,8 @@ import { import { DenoAPL } from "./deno-apl.ts"; import { fetchRemoteJwks, getAppId } from "./utils.ts"; import { AuthData } from "npm:@saleor/app-sdk@0.43.0/APL"; +import "./logger.ts"; +import * as log from "log/mod.ts"; const apl = new DenoAPL(); @@ -59,7 +61,10 @@ function getUrl(req: Request) { const routes = [ GET("/", () => Response.OK("Hello, Root")), GET("/manifest", (req) => { + log.debug("Requesting manifest", req.headers.get("HOST")); const URL = getUrl(req); + log.debug("Determined app URL", URL); + return Response.OK({ id: "witoszekdev.dummy-payment-app", name: "Dummy Payment App", @@ -117,8 +122,11 @@ const routes = [ const authToken = json.auth_token; const saleorDomain = req.headers.get(SALEOR_DOMAIN_HEADER); const saleorApiUrl = req.headers.get(SALEOR_API_URL_HEADER); + log.info("Running installation", { saleorDomain, saleorApiUrl }); + log.debug("Installation token", { authToken }); if (!authToken || !saleorDomain || !saleorApiUrl) { + log.warning("Missing headers"); return Response.BadRequest({ code: "MISSING_HEADER", message: "One of requried headers is missing", @@ -126,8 +134,10 @@ const routes = [ } const appId = await getAppId({ saleorApiUrl, token: authToken }); + log.debug("Got appId", appId); if (!appId) { + log.error("Missing app id", { saleorDomain, saleorApiUrl }); return Response.BadRequest({ code: "UNKNOWN_APP_ID", message: `The auth data given during registration request could not be used to fetch app ID. @@ -136,7 +146,9 @@ const routes = [ } const jwks = await fetchRemoteJwks(saleorApiUrl); + log.debug("Got jwks", jwks); if (!jwks) { + log.error("Missing jwks", { saleorDomain, saleorApiUrl }); return Response.BadRequest({ code: "JWKS_NOT_AVAILABLE", message: "Can't fetch the remote JWKS.", @@ -153,7 +165,9 @@ const routes = [ try { apl.set(authData); - } catch (_e) { + log.debug("Auth data saved"); + } catch (error) { + log.critical("Cannot save auth data", error); return Response.InternalServerError({ code: "APL_SAVE_ERROR", message: "Cannot save APL", diff --git a/utils.ts b/utils.ts index ea101fb..7de3bf4 100644 --- a/utils.ts +++ b/utils.ts @@ -53,3 +53,6 @@ export const getAppId = async ({ return undefined; } }; + +export const isDenoDeploy: boolean = + Deno.env.get("DENO_DEPLOYMENT_ID") !== undefined;