All files / shared logger.ts

80.76% Statements 21/26
78.94% Branches 15/19
100% Functions 8/8
78.26% Lines 18/23

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74                591x       1214x   1214x   1214x     549x     1214x   548x   548x                   548x 548x 1x   547x 547x                         1x       401x       114x       31x       667x      
// UnoSim/shared/logger.ts
 
export type LogLevel = "TEST" | "INFO" | "WARN" | "ERROR" | "DEBUG";
 
export class Logger {
  private sender: string;
 
  constructor(sender: string) {
    this.sender = sender;
  }
 
  private log(level: LogLevel, ...args: any[]) {
    const isBrowser = typeof window !== "undefined";
    const nodeEnv =
      (typeof process !== "undefined" && process.env?.NODE_ENV) || undefined;
    // Suppress DEBUG logs in test environment to prevent console spam in CI/CD
    if (level === "DEBUG" && nodeEnv === "test") return;
 
    const allowDebug =
      !isBrowser || nodeEnv === "development";
 
    // Suppress DEBUG logs in browser when not in development
    if (level === "DEBUG" && !allowDebug) return;
 
    const message = args
      .map((a) => {
        Eif (typeof a === "string") return a;
        try {
          return JSON.stringify(a);
        } catch {
          return String(a);
        }
      })
      .join(" ");
 
    // Test-Guard: Catch errors if console stream is closed (happens after tests finish)
    try {
      if (level === "TEST") {
        console.log(message);
      } else {
        const timestamp = new Date().toISOString();
        console.log(`[${timestamp}][${level}][${this.sender}] ${message}`);
      }
    } catch (err) {
      // Silently ignore logging errors in test environment
      // This prevents "Cannot log after tests are done" errors
      if (nodeEnv !== "test") {
        // In non-test environments, we still want to see the error
        console.error("Logger error:", err);
      }
    }
  }
 
  test(message: string) {
    this.log("TEST", message);
  }
 
  info(message: string) {
    this.log("INFO", message);
  }
 
  warn(message: string) {
    this.log("WARN", message);
  }
 
  error(message: string) {
    this.log("ERROR", message);
  }
 
  debug(message: string) {
    this.log("DEBUG", message);
  }
}