feat: use logger
This commit is contained in:
9
lib/cache/cache.ts
vendored
9
lib/cache/cache.ts
vendored
@ -5,11 +5,14 @@ import {
|
||||
RedisConnectOptions,
|
||||
RedisValue,
|
||||
} from "https://deno.land/x/redis@v0.31.0/mod.ts";
|
||||
import { createLogger } from "@lib/log.ts";
|
||||
|
||||
const REDIS_HOST = Deno.env.get("REDIS_HOST");
|
||||
const REDIS_PASS = Deno.env.get("REDIS_PASS") || "";
|
||||
const REDIS_PORT = Deno.env.get("REDIS_PORT");
|
||||
|
||||
const log = createLogger("cache");
|
||||
|
||||
async function createCache<T>(): Promise<Redis> {
|
||||
if (REDIS_HOST) {
|
||||
const conf: RedisConnectOptions = {
|
||||
@ -22,10 +25,10 @@ async function createCache<T>(): Promise<Redis> {
|
||||
}
|
||||
try {
|
||||
const client = await connect(conf);
|
||||
console.log("[redis] connected");
|
||||
log.info("redis connected");
|
||||
return client;
|
||||
} catch (_err) {
|
||||
console.log("[cache] cant connect to redis, falling back to mock");
|
||||
log.info("cant connect to redis, falling back to mock");
|
||||
}
|
||||
}
|
||||
|
||||
@ -94,7 +97,7 @@ export async function set<T extends RedisValue>(
|
||||
content: T,
|
||||
options?: RedisOptions,
|
||||
) {
|
||||
console.log("[cache] storing ", { id });
|
||||
log.debug("storing ", { id });
|
||||
const res = await cache.set(id, content);
|
||||
if (options?.expires) {
|
||||
await expire(id, options.expires);
|
||||
|
4
lib/cache/image.ts
vendored
4
lib/cache/image.ts
vendored
@ -1,6 +1,7 @@
|
||||
import { hash } from "@lib/string.ts";
|
||||
import * as cache from "@lib/cache/cache.ts";
|
||||
import { ImageMagick } from "https://deno.land/x/imagemagick_deno@0.0.25/mod.ts";
|
||||
import { createLogger } from "@lib/log.ts";
|
||||
|
||||
type ImageCacheOptions = {
|
||||
url: string;
|
||||
@ -10,6 +11,7 @@ type ImageCacheOptions = {
|
||||
};
|
||||
|
||||
const CACHE_KEY = "images";
|
||||
const log = createLogger("cache/image");
|
||||
|
||||
function getCacheKey({ url: _url, width, height }: ImageCacheOptions) {
|
||||
const url = new URL(_url);
|
||||
@ -63,7 +65,7 @@ export async function setImage(
|
||||
|
||||
const imageCorrect = await verifyImage(clone);
|
||||
if (!imageCorrect) {
|
||||
console.log("[cache/image] failed to store image", { url });
|
||||
log.info("failed to store image", { url });
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -11,6 +11,7 @@ import remarkFrontmatter, {
|
||||
import * as cache from "@lib/cache/documents.ts";
|
||||
import { SILVERBULLET_SERVER } from "@lib/env.ts";
|
||||
import { fixRenderedMarkdown } from "@lib/helpers.ts";
|
||||
import { createLogger } from "@lib/log.ts";
|
||||
|
||||
export type Document = {
|
||||
name: string;
|
||||
@ -20,13 +21,15 @@ export type Document = {
|
||||
perm: string;
|
||||
};
|
||||
|
||||
const log = createLogger("documents");
|
||||
|
||||
export async function getDocuments(): Promise<Document[]> {
|
||||
const cachedDocuments = await cache.getDocuments();
|
||||
if (cachedDocuments) return cachedDocuments;
|
||||
|
||||
const headers = new Headers();
|
||||
headers.append("Accept", "application/json");
|
||||
console.log("[documents] fetching all documents");
|
||||
log.debug("fetching all documents");
|
||||
const response = await fetch(`${SILVERBULLET_SERVER}/index.json`, {
|
||||
headers: headers,
|
||||
});
|
||||
@ -48,7 +51,7 @@ export function createDocument(
|
||||
headers.append("Content-Type", mediaType);
|
||||
}
|
||||
|
||||
console.log("[documents] creating document", { name });
|
||||
log.info("creating document", { name });
|
||||
|
||||
return fetch(SILVERBULLET_SERVER + "/" + name, {
|
||||
body: content,
|
||||
@ -61,7 +64,7 @@ export async function getDocument(name: string): Promise<string> {
|
||||
const cachedDocument = await cache.getDocument(name);
|
||||
if (cachedDocument) return cachedDocument;
|
||||
|
||||
console.log("[documents] fetching document", { name });
|
||||
log.debug("fetching document", { name });
|
||||
const response = await fetch(SILVERBULLET_SERVER + "/" + name);
|
||||
const text = await response.text();
|
||||
|
||||
|
53
lib/log.ts
Normal file
53
lib/log.ts
Normal file
@ -0,0 +1,53 @@
|
||||
enum LOG_LEVEL {
|
||||
DEBUG,
|
||||
INFO,
|
||||
WARN,
|
||||
ERROR,
|
||||
}
|
||||
let longestScope = 0;
|
||||
let logLevel = LOG_LEVEL.WARN;
|
||||
|
||||
export function setLogLevel(level: LOG_LEVEL) {
|
||||
logLevel = level;
|
||||
}
|
||||
|
||||
const getPrefix = (scope: string) => `[${scope.padEnd(longestScope, " ")}]`;
|
||||
|
||||
type LoggerOptions = {
|
||||
enabled?: boolean;
|
||||
};
|
||||
|
||||
export function createLogger(scope: string, _options?: LoggerOptions) {
|
||||
longestScope = Math.max(scope.length, longestScope);
|
||||
|
||||
function debug(...data: unknown[]) {
|
||||
if (logLevel !== LOG_LEVEL.DEBUG) return;
|
||||
console.debug(getPrefix(scope), ...data);
|
||||
}
|
||||
|
||||
function info(...data: unknown[]) {
|
||||
if (logLevel !== LOG_LEVEL.DEBUG && logLevel !== LOG_LEVEL.INFO) return;
|
||||
console.info(getPrefix(scope), ...data);
|
||||
}
|
||||
|
||||
function warn(...data: unknown[]) {
|
||||
if (
|
||||
logLevel !== LOG_LEVEL.DEBUG && logLevel !== LOG_LEVEL.INFO &&
|
||||
logLevel !== LOG_LEVEL.WARN
|
||||
) return;
|
||||
console.warn(getPrefix(scope), ...data);
|
||||
}
|
||||
|
||||
function error(...data: unknown[]) {
|
||||
console.error(getPrefix(scope), ...data);
|
||||
}
|
||||
|
||||
return {
|
||||
debug,
|
||||
info,
|
||||
error,
|
||||
warn,
|
||||
};
|
||||
}
|
||||
|
||||
const log = createLogger("");
|
@ -100,7 +100,6 @@ function parseArticle(original: string, id: string): Article {
|
||||
export const getAllArticles = crud.readAll;
|
||||
export const getArticle = crud.read;
|
||||
export const createArticle = (article: Article) => {
|
||||
console.log("creating article", { article });
|
||||
const content = renderArticle(article);
|
||||
return crud.create(article.id, content);
|
||||
};
|
||||
|
@ -103,7 +103,6 @@ const crud = createCrud<Movie>({
|
||||
export const getMovie = crud.read;
|
||||
export const getAllMovies = crud.readAll;
|
||||
export const createMovie = (movie: Movie) => {
|
||||
console.log("creating movie", { movie });
|
||||
const content = renderMovie(movie);
|
||||
return crud.create(movie.id, content);
|
||||
};
|
||||
|
@ -3,6 +3,9 @@ import { TYPESENSE_API_KEY, TYPESENSE_URL } from "@lib/env.ts";
|
||||
import { getAllMovies } from "@lib/resource/movies.ts";
|
||||
import { getAllRecipes } from "@lib/resource/recipes.ts";
|
||||
import { getAllArticles } from "@lib/resource/articles.ts";
|
||||
import { createLogger } from "@lib/log.ts";
|
||||
|
||||
const log = createLogger("typesense");
|
||||
|
||||
function sanitizeStringForTypesense(input: string) {
|
||||
// Remove backslashes
|
||||
@ -82,12 +85,12 @@ async function initializeTypesense() {
|
||||
],
|
||||
default_sorting_field: "rating", // Default field for sorting
|
||||
});
|
||||
console.log('[typesense] created "resources" collection');
|
||||
log.info('created "resources" collection');
|
||||
} else {
|
||||
console.log('[typesense] collection "resources" already exists.');
|
||||
log.info('collection "resources" already exists.');
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("[typesense] error initializing", error);
|
||||
log.error("error initializing", error);
|
||||
}
|
||||
}
|
||||
|
||||
@ -143,9 +146,9 @@ async function synchronizeWithTypesense() {
|
||||
// ),
|
||||
// );
|
||||
|
||||
console.log("Data synchronized with Typesense.");
|
||||
log.info("data synchronized");
|
||||
} catch (error) {
|
||||
console.error("Error synchronizing data with Typesense:", error);
|
||||
log.error("error synchronizing", error);
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user