import { LogLevel, createCatalogDiagnostics, type DiagnosticCatalog, type DiagnosticEvent, type Diagnostics, type Logger } from '$libs/logger'; import type { CacheKey } from '$libs/cache'; import { CACHE_ACTIVE_DIAGNOSTIC_EVENTS, CACHE_ACTIVE_LOG_MESSAGE_OPERATION_FAILED, CACHE_MODULE } from './consts.ts'; export type ActiveCacheDiagnosticType = (typeof CACHE_ACTIVE_DIAGNOSTIC_EVENTS)[keyof typeof CACHE_ACTIVE_DIAGNOSTIC_EVENTS]; export interface ActiveCacheDiagnosticMeta { readonly method: string; readonly key?: CacheKey; readonly error?: unknown; } export type ActiveCacheDiagnosticEvent = DiagnosticEvent< ActiveCacheDiagnosticType, ActiveCacheDiagnosticMeta >; export type ActiveCacheDiagnostics = Diagnostics; const ACTIVE_CACHE_DIAGNOSTIC_LOGS: DiagnosticCatalog = { [CACHE_ACTIVE_DIAGNOSTIC_EVENTS.OPERATION_FAILED]: { level: LogLevel.WARN, message: CACHE_ACTIVE_LOG_MESSAGE_OPERATION_FAILED } }; export function createActiveCacheDiagnostics(logger?: Logger): ActiveCacheDiagnostics { return createCatalogDiagnostics({ logger, defaultCategory: CACHE_MODULE, catalog: ACTIVE_CACHE_DIAGNOSTIC_LOGS }); } export function emitActiveCacheDiagnostic( diagnostics: ActiveCacheDiagnostics, type: ActiveCacheDiagnosticType, meta: ActiveCacheDiagnosticMeta ): void { diagnostics.emit({ artifact: CACHE_MODULE, type, meta }); }