feat: use logger

This commit is contained in:
2023-08-05 22:16:14 +02:00
parent 46cd823b6c
commit 32a7f89309
15 changed files with 99 additions and 37 deletions

View File

@ -11,6 +11,7 @@ import * as cache from "@lib/cache/image.ts";
import { SILVERBULLET_SERVER } from "@lib/env.ts";
import { PromiseQueue } from "@lib/promise.ts";
import { BadRequestError } from "@lib/errors.ts";
import { createLogger } from "@lib/log.ts";
await initialize();
@ -19,8 +20,10 @@ type ImageParams = {
height: number;
};
const log = createLogger("api/image");
async function getRemoteImage(image: string) {
console.log("[api/image] fetching", { image });
log.debug("[api/image] fetching", { image });
const sourceRes = await fetch(image);
if (!sourceRes.ok) {
return "Error retrieving image from URL.";
@ -107,7 +110,7 @@ const queue = new PromiseQueue();
async function processImage(imageUrl: string, params: ImageParams) {
const remoteImage = await getRemoteImage(imageUrl);
if (typeof remoteImage === "string") {
console.log("[api/image] ERROR " + remoteImage);
log.warn("error fetching ", { remoteImage });
throw new BadRequestError();
}
@ -117,7 +120,7 @@ async function processImage(imageUrl: string, params: ImageParams) {
mode: "resize",
});
console.log("[api/image] resized image", {
log.debug("resized image", {
imageUrl,
length: modifiedImage.length,
});
@ -144,14 +147,14 @@ const GET = async (
height: params.height,
});
if (cachedResponse) {
console.log("[api/image] cached: " + imageUrl);
log.debug("cached", { imageUrl });
return new Response(cachedResponse.buffer.slice(), {
headers: {
"Content-Type": cachedResponse.mediaType,
},
});
} else {
console.log("[api/image] no image in cache");
log.debug("no image in cache");
}
const [resizedImage, mediaType] = await queue.enqueue(() =>
@ -165,8 +168,7 @@ const GET = async (
mediaType: mediaType,
});
console.log("[api/image] not-cached: " + imageUrl);
console.log({ imageUrl, resizedImage });
log.debug("not-cached", { imageUrl, resizedImage });
return new Response(new Uint8Array(resizedImage), {
headers: {