base.ts
1 import type { Level, Logger } from './types'; 2 3 export abstract class BaseLogger<Args extends unknown[] = unknown[]> 4 implements Logger<Args> 5 { 6 constructor(protected readonly name: string) {} 7 8 /** 9 * Log a debug level message. 10 * Appropriate for verbose logging that explains steps/details of the inner state of 11 * a code unit. 12 * 13 * Example uses include in a size-constrain datastructure, logging when the size 14 * exceeds the threshold and elements are removed, or in a virtual scrolling 15 * component logging when a scroll event causes a new page of elements to be loaded. 16 * 17 * @param args Arguments to log (same as console.debug) 18 * @return empty string (for use in brackets {} in svelte components) 19 */ 20 debug(...args: Args): string { 21 return this.log('debug', ...args); 22 } 23 24 /** 25 * Log an info level message. 26 * Appropriate for informational messages that may be relevant to consumers of a code 27 * unit. 28 * 29 * Example uses include a router logging when transitions occur or a button logging 30 * clicks. 31 * 32 * @param args Arguments to log (same as console.info) 33 * @return empty string (for use in brackets {} in svelte components) 34 */ 35 info(...args: Args): string { 36 return this.log('info', ...args); 37 } 38 39 /** 40 * Log a warn level message. 41 * Appropriate for situations where state has been (or likely will be) corrupted or 42 * invariants have been broken. 43 * 44 * Example uses include a data structure warning when it is used before being fully 45 * initialized. 46 * 47 * @param args Arguments to log (same as console.warn) 48 * @return empty string (for use in brackets {} in svelte components) 49 */ 50 warn(...args: Args): string { 51 return this.log('warn', ...args); 52 } 53 54 /** 55 * Log an error message. 56 * Appropriate for thrown errors or situations where the apps breaks or has to 57 * engage in fallback behavior to avoid a more catastrophic failure. 58 * 59 * @param args Arguments to log (same as console.error) 60 * @return empty string (for use in brackets {} in svelte components) 61 */ 62 error(...args: Args): string { 63 return this.log('error', ...args); 64 } 65 66 protected abstract log(method: Level, ...args: Args): string; 67 }