diff options
| author | iv-ar <i@oiee.no> | 2023-03-04 16:54:16 +0100 |
|---|---|---|
| committer | iv-ar <i@oiee.no> | 2023-03-04 16:54:16 +0100 |
| commit | cf546fd4b9a1fbf77bccf5f0b713e688d29a66ad (patch) | |
| tree | 8103d953afec0618a55dc957ba8a3222dac0e842 /code/app/src/utilities | |
| parent | b85164718d3bd6a4ad5fc06d04a3ce2dc028b1db (diff) | |
| download | greatoffice-cf546fd4b9a1fbf77bccf5f0b713e688d29a66ad.tar.xz greatoffice-cf546fd4b9a1fbf77bccf5f0b713e688d29a66ad.zip | |
feat: Use console to log
Diffstat (limited to 'code/app/src/utilities')
| -rw-r--r-- | code/app/src/utilities/_fetch.ts | 22 | ||||
| -rw-r--r-- | code/app/src/utilities/cache.ts | 11 | ||||
| -rw-r--r-- | code/app/src/utilities/logger.ts | 118 | ||||
| -rw-r--r-- | code/app/src/utilities/persistent-store.ts | 17 |
4 files changed, 24 insertions, 144 deletions
diff --git a/code/app/src/utilities/_fetch.ts b/code/app/src/utilities/_fetch.ts index 992c7f5..415e1c2 100644 --- a/code/app/src/utilities/_fetch.ts +++ b/code/app/src/utilities/_fetch.ts @@ -1,28 +1,28 @@ -import { Temporal } from "temporal-polyfill"; -import { redirect } from "@sveltejs/kit"; -import { browser } from "$app/environment"; -import { goto } from "$app/navigation"; -import { SignInPageMessage, signInPageMessageQueryKey } from "$routes/(main)/(public)/sign-in"; -import { log_error } from "$utilities/logger"; -import { AccountService } from "$services/account-service"; +import {Temporal} from "temporal-polyfill"; +import {redirect} from "@sveltejs/kit"; +import {browser} from "$app/environment"; +import {goto} from "$app/navigation"; +import {SignInPageMessage, signInPageMessageQueryKey} from "$routes/(main)/(public)/sign-in"; +import {AccountService} from "$services/account-service"; + export async function http_post_async(url: string, body?: object | string, timeout = -1, skip_401_check = false, abort_signal?: AbortSignal): Promise<Response> { const init = make_request_init("post", body, abort_signal); - const response = await internal_fetch_async({ url, init, timeout }); + const response = await internal_fetch_async({url, init, timeout}); if (!skip_401_check && await redirect_if_401_async(response)) throw new Error("Server returned 401"); return response; } export async function http_get_async(url: string, timeout = -1, skip_401_check = false, abort_signal?: AbortSignal): Promise<Response> { const init = make_request_init("get", undefined, abort_signal); - const response = await internal_fetch_async({ url, init, timeout }); + const response = await internal_fetch_async({url, init, timeout}); if (!skip_401_check && await redirect_if_401_async(response)) throw new Error("Server returned 401"); return response; } export async function http_delete_async(url: string, body?: object | string, timeout = -1, skip_401_check = false, abort_signal?: AbortSignal): Promise<Response> { const init = make_request_init("delete", body, abort_signal); - const response = await internal_fetch_async({ url, init, timeout }); + const response = await internal_fetch_async({url, init, timeout}); if (!skip_401_check && await redirect_if_401_async(response)) throw new Error("Server returned 401"); return response; } @@ -42,7 +42,7 @@ async function internal_fetch_async(request: InternalFetchRequest): Promise<Resp response = await fetch(fetch_request); } } catch (error: any) { - log_error(error); + console.error(error); if (error.message === "Timeout") { console.error("Request timed out"); } else if (error.message === "Network request failed") { diff --git a/code/app/src/utilities/cache.ts b/code/app/src/utilities/cache.ts index db9be9a..101f192 100644 --- a/code/app/src/utilities/cache.ts +++ b/code/app/src/utilities/cache.ts @@ -1,11 +1,10 @@ -import { Temporal } from "temporal-polyfill"; -import { log_debug } from "$utilities/logger"; +import {Temporal} from "temporal-polyfill"; let cache = {}; export const CacheKeys = { - isAuthenticated: "isAuthenticated" -} + isAuthenticated: "isAuthenticated", +}; export async function cached_result_async<T>(key: string, staleAfterSeconds: number, get_result: any, forceRefresh: boolean = false) { if (!cache[key]) { @@ -21,7 +20,7 @@ export async function cached_result_async<T>(key: string, staleAfterSeconds: num cache[key].l = Temporal.Now.instant().epochSeconds; } - log_debug("Ran cached_result_async", { + console.debug("Ran cached_result_async", { cacheKey: key, isStale, cache: cache[key], @@ -34,5 +33,5 @@ export async function cached_result_async<T>(key: string, staleAfterSeconds: num export function clear_cache_key(key: string) { if (!key) throw new Error("No key was specified"); cache[key].c = undefined; - log_debug("Cleared cache with key: " + key); + console.debug("Cleared cache with key: " + key); }
\ No newline at end of file diff --git a/code/app/src/utilities/logger.ts b/code/app/src/utilities/logger.ts deleted file mode 100644 index c21bd76..0000000 --- a/code/app/src/utilities/logger.ts +++ /dev/null @@ -1,118 +0,0 @@ -import { browser, dev } from "$app/environment"; -import { env } from '$env/dynamic/private'; -import { StorageKeys } from "$configuration"; -import pino, { type Logger, type LoggerOptions } from "pino"; -import { createStream } from "pino-seq"; -import type { SeqConfig } from "pino-seq"; - -function get_pino_logger(): Logger { - const config = { - name: "greatoffice-app", - level: LogLevel.current().as_string(), - customLevels: { - "INFO": LogLevel.INFO, - "WARNING": LogLevel.WARNING, - "ERROR": LogLevel.ERROR, - "DEBUG": LogLevel.DEBUG, - "SILENT": LogLevel.SILENT, - } - } as LoggerOptions; - - const seq = { - config: { - apiKey: browser ? env.SEQ_API_KEY : "", - serverUrl: browser ? env.SEQ_SERVER_URL : "" - } as SeqConfig, - streams: [{ - level: LogLevel.to_string(LogLevel.DEBUG), - }], - enabled: () => ( - !browser - && !dev - && seq.config.apiKey.length > 0 - && seq.config.serverUrl.length > 0 - ) - }; - - return seq.enabled() ? pino(config, createStream(seq.config)) : pino(config); -} - -type LogLevelString = "DEBUG" | "INFO" | "WARNING" | "ERROR" | "SILENT"; - -export const LogLevel = { - DEBUG: 0, - INFO: 1, - WARNING: 2, - ERROR: 3, - SILENT: 4, - current(): { as_string: Function, as_number: Function } { - const logLevelString = (browser ? window.sessionStorage.getItem(StorageKeys.logLevel) : env.LOG_LEVEL) as LogLevelString; - return { - as_number(): number { - return LogLevel.to_number_or_default(logLevelString, LogLevel.INFO) - }, - as_string(): LogLevelString { - return logLevelString.length > 3 ? logLevelString : LogLevel.to_string(LogLevel.INFO); - } - } - }, - to_string(levelInt: number): LogLevelString { - switch (levelInt) { - case 0: - return "DEBUG"; - case 1: - return "INFO"; - case 2: - return "WARNING"; - case 3: - return "ERROR"; - case 4: - return "SILENT"; - default: - throw new Error("Unknown LogLevel number " + levelInt); - } - }, - to_number_or_default(levelString?: string | null, defaultValue?: number): number { - if (!levelString && defaultValue) return defaultValue; - else if (!levelString && !defaultValue) throw new Error("levelString was empty, and no default value was specified"); - switch (levelString?.toUpperCase()) { - case "DEBUG": - return 0; - case "INFO": - return 1; - case "WARNING": - return 2; - case "ERROR": - return 3; - case "SILENT": - return 4; - default: - if (!defaultValue) throw new Error("Unknown LogLevel string " + levelString + ", and no defaultValue"); - else return defaultValue; - } - }, -}; - -export function log_warning(message: string, ...additional: any[]): void { - if (LogLevel.current().as_number() <= LogLevel.WARNING) { - get_pino_logger().warn(message, additional); - } -} - -export function log_debug(message: string, ...additional: any[]): void { - if (LogLevel.current().as_number() <= LogLevel.DEBUG) { - get_pino_logger().debug(message, additional); - } -} - -export function log_info(message: string, ...additional: any[]): void { - if (LogLevel.current().as_number() <= LogLevel.INFO) { - get_pino_logger().info(message, additional); - } -} - -export function log_error(message: any, ...additional: any[]): void { - if (LogLevel.current().as_number() <= LogLevel.ERROR) { - get_pino_logger().error(message, additional); - } -}
\ No newline at end of file diff --git a/code/app/src/utilities/persistent-store.ts b/code/app/src/utilities/persistent-store.ts index 3f56312..d880464 100644 --- a/code/app/src/utilities/persistent-store.ts +++ b/code/app/src/utilities/persistent-store.ts @@ -1,7 +1,6 @@ -import { browser } from "$app/environment"; -import { writable as _writable, readable as _readable } from "svelte/store"; -import type { Writable, Readable, StartStopNotifier } from "svelte/store"; -import { log_debug, log_info } from "./logger"; +import {browser} from "$app/environment"; +import {writable as _writable, readable as _readable} from "svelte/store"; +import type {Writable, Readable, StartStopNotifier} from "svelte/store"; enum StoreType { SESSION = 0, @@ -53,7 +52,7 @@ function get_store_value<T>(init: WritableStoreInit<T> | ReadableStoreInit<T>): return JSON.parse(value); } catch (e) { console.error(e); - return { __INVALID__: true }; + return {__INVALID__: true}; } } @@ -73,11 +72,11 @@ function subscribe<T>(store: Writable<T> | Readable<T>, init: WritableStoreInit< function create_writable_persistent<T>(init: WritableStoreInit<T>): Writable<T> { if (!browser) { - log_info("WARN: Persistent store is only available in the browser"); + console.warn("Persistent store is only available in the browser"); return; } if (init.options === undefined) throw new Error("init is a required parameter"); - log_debug("creating writable store with options: ", init); + console.debug("Creating writable store with options: ", init); const store = _writable<T>(init.initialState); hydrate(store, init); subscribe(store, init); @@ -86,11 +85,11 @@ function create_writable_persistent<T>(init: WritableStoreInit<T>): Writable<T> function create_readable_persistent<T>(init: ReadableStoreInit<T>): Readable<T> { if (!browser) { - log_info("WARN: Persistent store is only available in the browser"); + console.warning("Persistent store is only available in the browser"); return; } if (init.options === undefined) throw new Error("init is a required parameter"); - log_debug("Creating readable store with options: ", init); + console.debug("Creating readable store with options: ", init); const store = _readable<T>(init.initialState, init.callback); // hydrate(store, options); subscribe(store, init); |
