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

@ -9,10 +9,11 @@ import tds from "https://cdn.skypack.dev/turndown@7.1.1";
import { Article, createArticle } from "@lib/resource/articles.ts";
import { getYoutubeVideoDetails } from "@lib/youtube.ts";
import { extractYoutubeId, isYoutubeLink } from "@lib/string.ts";
import { createLogger } from "@lib/log.ts";
const parser = new DOMParser();
//service.use(gfm);
const log = createLogger("api/article");
async function processCreateArticle(
{ fetchUrl, streamResponse }: {
@ -20,7 +21,7 @@ async function processCreateArticle(
streamResponse: ReturnType<typeof createStreamResponse>;
},
) {
console.log("[api/article] create article from url", { url: fetchUrl });
log.info("create article from url", { url: fetchUrl });
streamResponse.enqueue("downloading article");
@ -48,7 +49,7 @@ async function processCreateArticle(
const result = readable.parse();
console.log("[api/article] parsed ", {
log.debug("parsed", {
url: fetchUrl,
content: result.textContent,
});
@ -173,7 +174,7 @@ async function processCreateYoutubeVideo(
streamResponse: ReturnType<typeof createStreamResponse>;
},
) {
console.log("[api/article] create youtube article from url", {
log.info("create youtube article from url", {
url: fetchUrl,
});
@ -187,6 +188,7 @@ async function processCreateYoutubeVideo(
const newId = await openai.shortenTitle(video.snippet.title);
const newArticle: Article = {
type: "article",
name: video.snippet.title,
id: newId || video.snippet.title,
content: video.snippet.description,
@ -227,18 +229,18 @@ export const handler: Handlers = {
if (isYoutubeLink(fetchUrl)) {
processCreateYoutubeVideo({ fetchUrl, streamResponse }).then(
(article) => {
console.log({ article });
log.debug("created article from youtube", { article });
},
).catch((err) => {
console.log(err);
log.error(err);
}).finally(() => {
streamResponse.cancel();
});
} else {
processCreateArticle({ fetchUrl, streamResponse }).then((article) => {
console.log({ article });
log.debug("created article from link", { article });
}).catch((err) => {
console.log(err);
log.error(err);
}).finally(() => {
streamResponse.cancel();
});

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: {

View File

@ -37,7 +37,6 @@ export const handler: Handlers = {
const authors = url.searchParams?.get("author")?.split(",");
if (authors?.length) {
console.log({ authors });
resources = resources.filter((r) => {
return r?.meta?.author && authors.includes(r?.meta?.author);
});

View File

@ -2,6 +2,7 @@ import { HandlerContext } from "$fresh/server.ts";
import { getMovieCredits } from "@lib/tmdb.ts";
import * as cache from "@lib/cache/cache.ts";
import { json } from "@lib/helpers.ts";
import { createLogger } from "@lib/log.ts";
type CachedMovieCredits = {
lastUpdated: number;
@ -10,6 +11,8 @@ type CachedMovieCredits = {
const CACHE_INTERVAL = 1000 * 60 * 24 * 30;
const log = createLogger("api/tmdb");
export const handler = async (
_req: Request,
_ctx: HandlerContext,
@ -22,7 +25,7 @@ export const handler = async (
});
}
console.log("[api] getting movie credits");
log.debug("getting movie credits");
const cacheId = `/movie/credits/${id}`;