Add logger

This commit is contained in:
Jonatan Witoszek 2023-08-18 13:57:33 +02:00
parent 2eea13fe81
commit ccd3276f45
No known key found for this signature in database
GPG key ID: D35056EA9CCE8472
6 changed files with 44 additions and 2 deletions

View file

@ -11,6 +11,16 @@
"https://deno.land/std@0.160.0/async/pool.ts": "ef9eb97b388543acbf0ac32647121e4dbe629236899586c4d4311a8770fbb239", "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/async/tee.ts": "9af3a3e7612af75861308b52249e167f5ebc3dcfc8a1a4d45462d96606ee2b70",
"https://deno.land/std@0.160.0/http/server.ts": "e99c1bee8a3f6571ee4cdeb2966efad465b8f6fe62bec1bdb59c1f007cc4d155", "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/mod.ts": "4516d9ea9393e466c94bcc525768a74af9efb7799e5df7e4b0e45474251d5f81",
"https://deno.land/x/wren@0.8.0/response.ts": "83bf67e3a39cfd61c38b912916f4a3cb98446320b0254c62d34c897c15daa31e", "https://deno.land/x/wren@0.8.0/response.ts": "83bf67e3a39cfd61c38b912916f4a3cb98446320b0254c62d34c897c15daa31e",
"https://deno.land/x/wren@0.8.0/route.ts": "b1acd71c4664799edcf216d0d37c6ebab1d57ea674f5e8a72baa4bf28aaf3383", "https://deno.land/x/wren@0.8.0/route.ts": "b1acd71c4664799edcf216d0d37c6ebab1d57ea674f5e8a72baa4bf28aaf3383",

View file

@ -1,6 +1,7 @@
{ {
"imports": { "imports": {
"http/": "https://deno.land/std@0.160.0/http/", "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/" "wren/": "https://deno.land/x/wren@0.8.0/"
} }
} }

View file

@ -1,6 +1,7 @@
// From https://github.com/alexreardon/tiny-invariant/blob/master/src/tiny-invariant.ts // 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"; const prefix: string = "Invariant failed";
// Throw an error if the condition fails // Throw an error if the condition fails

13
logger.ts Normal file
View file

@ -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",
},
},
});

16
main.ts
View file

@ -19,6 +19,8 @@ import {
import { DenoAPL } from "./deno-apl.ts"; import { DenoAPL } from "./deno-apl.ts";
import { fetchRemoteJwks, getAppId } from "./utils.ts"; import { fetchRemoteJwks, getAppId } from "./utils.ts";
import { AuthData } from "npm:@saleor/app-sdk@0.43.0/APL"; 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(); const apl = new DenoAPL();
@ -59,7 +61,10 @@ function getUrl(req: Request) {
const routes = [ const routes = [
GET("/", () => Response.OK("Hello, Root")), GET("/", () => Response.OK("Hello, Root")),
GET("/manifest", (req) => { GET("/manifest", (req) => {
log.debug("Requesting manifest", req.headers.get("HOST"));
const URL = getUrl(req); const URL = getUrl(req);
log.debug("Determined app URL", URL);
return Response.OK({ return Response.OK({
id: "witoszekdev.dummy-payment-app", id: "witoszekdev.dummy-payment-app",
name: "Dummy Payment App", name: "Dummy Payment App",
@ -117,8 +122,11 @@ const routes = [
const authToken = json.auth_token; const authToken = json.auth_token;
const saleorDomain = req.headers.get(SALEOR_DOMAIN_HEADER); const saleorDomain = req.headers.get(SALEOR_DOMAIN_HEADER);
const saleorApiUrl = req.headers.get(SALEOR_API_URL_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) { if (!authToken || !saleorDomain || !saleorApiUrl) {
log.warning("Missing headers");
return Response.BadRequest({ return Response.BadRequest({
code: "MISSING_HEADER", code: "MISSING_HEADER",
message: "One of requried headers is missing", message: "One of requried headers is missing",
@ -126,8 +134,10 @@ const routes = [
} }
const appId = await getAppId({ saleorApiUrl, token: authToken }); const appId = await getAppId({ saleorApiUrl, token: authToken });
log.debug("Got appId", appId);
if (!appId) { if (!appId) {
log.error("Missing app id", { saleorDomain, saleorApiUrl });
return Response.BadRequest({ return Response.BadRequest({
code: "UNKNOWN_APP_ID", code: "UNKNOWN_APP_ID",
message: `The auth data given during registration request could not be used to fetch 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); const jwks = await fetchRemoteJwks(saleorApiUrl);
log.debug("Got jwks", jwks);
if (!jwks) { if (!jwks) {
log.error("Missing jwks", { saleorDomain, saleorApiUrl });
return Response.BadRequest({ return Response.BadRequest({
code: "JWKS_NOT_AVAILABLE", code: "JWKS_NOT_AVAILABLE",
message: "Can't fetch the remote JWKS.", message: "Can't fetch the remote JWKS.",
@ -153,7 +165,9 @@ const routes = [
try { try {
apl.set(authData); apl.set(authData);
} catch (_e) { log.debug("Auth data saved");
} catch (error) {
log.critical("Cannot save auth data", error);
return Response.InternalServerError({ return Response.InternalServerError({
code: "APL_SAVE_ERROR", code: "APL_SAVE_ERROR",
message: "Cannot save APL", message: "Cannot save APL",

View file

@ -53,3 +53,6 @@ export const getAppId = async ({
return undefined; return undefined;
} }
}; };
export const isDenoDeploy: boolean =
Deno.env.get("DENO_DEPLOYMENT_ID") !== undefined;