LoggingUtils: {
    defaultOptions: {
        level: LogLevel;
        timestamp: boolean;
        prefix: string;
        group: boolean;
        groupCollapsed: boolean;
        stackTrace: boolean;
    };
    logEntries: LogEntry[];
    maxLogEntries: number;
    clearLogEntries(): void;
    debug(message, options?): void;
    error(message, options?): void;
    exportLogEntries(): string;
    formatBytes(bytes): string;
    getLogEntries(): LogEntry[];
    getLogEntriesByLevel(level): LogEntry[];
    getLogEntriesByTimeRange(startTime, endTime): LogEntry[];
    info(message, options?): void;
    importLogEntries(json): void;
    log(message, options?): void;
    measureMemory(label): void;
    measurePerformance(label): (() => void);
    measureAsyncPerformance<T>(label, fn): Promise<T>;
    warn(message, options?): void;
} = ...

Type declaration

  • defaultOptions: {
        level: LogLevel;
        timestamp: boolean;
        prefix: string;
        group: boolean;
        groupCollapsed: boolean;
        stackTrace: boolean;
    }

    Default configuration options for logging.

    Default

    {
    level: "info",
    timestamp: true,
    prefix: "",
    group: false,
    groupCollapsed: false,
    stackTrace: false
    }
    • level: LogLevel
    • timestamp: boolean
    • prefix: string
    • group: boolean
    • groupCollapsed: boolean
    • stackTrace: boolean
  • logEntries: LogEntry[]

    Internal storage for all log entries.

    Default

    []
    
  • maxLogEntries: number

    Maximum number of log entries to store before removing oldest entries.

    Default

    1000
    
  • clearLogEntries:function
    • Clears all stored log entries from memory.

      Returns void

      Example

      // Clear all stored logs
      LoggingUtils.clearLogEntries();
      console.log("Logs cleared");

      // Clear logs after exporting
      const logs = LoggingUtils.exportLogEntries();
      LoggingUtils.clearLogEntries();
  • debug:function
    • Logs a debug message with optional metadata.

      Parameters

      • message: string

        The debug message to log

      • options: Omit<LogOptions, "level"> = {}

        Optional configuration for the log entry

      Returns void

      Example

      // Simple debug message
      LoggingUtils.debug("Processing started");

      // Debug with data and prefix
      LoggingUtils.debug("Data processed", {
      prefix: "DataService",
      data: { count: 100, status: "success" }
      });

      // Debug with grouping
      LoggingUtils.debug("Complex operation", {
      group: true,
      groupCollapsed: true,
      data: { steps: ["step1", "step2"] }
      });
  • error:function
    • Logs an error message with optional stack trace and metadata.

      Parameters

      • message: string

        The error message to log

      • options: Omit<LogOptions, "level"> = {}

        Optional configuration for the log entry

      Returns void

      Example

      try {
      // ... some code that might throw
      } catch (error) {
      LoggingUtils.error("Failed to process data", {
      data: error,
      prefix: "DataService",
      stackTrace: true
      });
      }

      // Error with custom data
      LoggingUtils.error("API request failed", {
      data: {
      status: 500,
      endpoint: "/api/data",
      response: "Internal Server Error"
      }
      });
  • exportLogEntries:function
    • Exports all stored log entries as a formatted JSON string.

      Returns string

      A JSON string containing all log entries

      Example

      // Export logs to file
      const logs = LoggingUtils.exportLogEntries();
      const blob = new Blob([logs], { type: 'application/json' });
      const url = URL.createObjectURL(blob);

      // Export logs to server
      const logs = LoggingUtils.exportLogEntries();
      await fetch('/api/logs', {
      method: 'POST',
      body: logs,
      headers: { 'Content-Type': 'application/json' }
      });
  • formatBytes:function
    • Formats a number of bytes into a human-readable string with appropriate unit.

      Parameters

      • bytes: number

        The number of bytes to format

      Returns string

      A formatted string with appropriate unit (B, KB, MB, GB, TB)

      Example

      const size = LoggingUtils.formatBytes(1024 * 1024);
      console.log(size); // "1.00 MB"

      const smallSize = LoggingUtils.formatBytes(500);
      console.log(smallSize); // "500.00 B"

      const largeSize = LoggingUtils.formatBytes(1024 * 1024 * 1024 * 2);
      console.log(largeSize); // "2.00 GB"
  • getLogEntries:function
    • Gets a copy of all stored log entries.

      Returns LogEntry[]

      An array of log entries

      Example

      // Get all logs
      const allLogs = LoggingUtils.getLogEntries();
      console.log(`Total logs: ${allLogs.length}`);

      // Filter logs in memory
      const errorLogs = LoggingUtils.getLogEntries()
      .filter(log => log.level === 'error');
  • getLogEntriesByLevel:function
    • Gets log entries filtered by a specific log level.

      Parameters

      • level: LogLevel

        The log level to filter by

      Returns LogEntry[]

      An array of filtered log entries

      Example

      // Get all error logs
      const errorLogs = LoggingUtils.getLogEntriesByLevel("error");
      console.log(`Error count: ${errorLogs.length}`);

      // Get all debug logs
      const debugLogs = LoggingUtils.getLogEntriesByLevel("debug");
      console.log(`Debug count: ${debugLogs.length}`);
  • getLogEntriesByTimeRange:function
    • Gets log entries within a specific time range.

      Parameters

      • startTime: Date

        The start time of the range

      • endTime: Date

        The end time of the range

      Returns LogEntry[]

      An array of filtered log entries

      Example

      // Get logs from today
      const today = new Date();
      const yesterday = new Date(today);
      yesterday.setDate(yesterday.getDate() - 1);

      const todayLogs = LoggingUtils.getLogEntriesByTimeRange(yesterday, today);
      console.log(`Logs in last 24 hours: ${todayLogs.length}`);

      // Get logs from specific time period
      const startDate = new Date("2024-01-01");
      const endDate = new Date("2024-01-02");
      const periodLogs = LoggingUtils.getLogEntriesByTimeRange(startDate, endDate);
  • info:function
    • Logs an informational message with optional metadata.

      Parameters

      • message: string

        The info message to log

      • options: Omit<LogOptions, "level"> = {}

        Optional configuration for the log entry

      Returns void

      Example

      // Simple info message
      LoggingUtils.info("Application started");

      // Info with data and prefix
      LoggingUtils.info("User logged in", {
      prefix: "Auth",
      data: { userId: "123", role: "admin" }
      });

      // Info with grouping
      LoggingUtils.info("Configuration loaded", {
      group: true,
      data: { settings: { theme: "dark", language: "en" } }
      });
  • importLogEntries:function
    • Imports log entries from a JSON string.

      Parameters

      • json: string

        The JSON string containing log entries

      Returns void

      Throws

      If the JSON string is invalid or cannot be parsed

      Example

      // Import logs from file
      const json = await fetch('/logs.json').then(r => r.text());
      LoggingUtils.importLogEntries(json);

      // Import logs from server
      const response = await fetch('/api/logs');
      const json = await response.text();
      LoggingUtils.importLogEntries(json);
  • log:function
    • Logs a message with the specified options.

      Parameters

      • message: string

        The message to log

      • options: LogOptions = {}

        Configuration options for the log entry

      Returns void

      Example

      // Custom log with all options
      LoggingUtils.log("Custom message", {
      level: "info",
      prefix: "Custom",
      group: true,
      groupCollapsed: true,
      stackTrace: true,
      data: { custom: "data" }
      });

      // Simple log with default options
      LoggingUtils.log("Simple message");
  • measureMemory:function
    • Logs memory usage information if available in the browser.

      Parameters

      • label: string

        The label for the memory measurement

      Returns void

      Example

      // Log memory usage
      LoggingUtils.measureMemory("After data load");
      // Output: Memory: After data load { used: "50.2 MB", total: "100.0 MB", limit: "500.0 MB" }

      // Track memory usage over time
      setInterval(() => {
      LoggingUtils.measureMemory("Periodic check");
      }, 60000);
  • measurePerformance:function
    • Creates a performance measurement for synchronous operations.

      Parameters

      • label: string

        The label for the performance measurement

      Returns (() => void)

      A function to end the measurement and log the duration

        • (): void
        • Returns void

      Example

      // Measure a synchronous operation
      const endMeasurement = LoggingUtils.measurePerformance("Data processing");
      // ... do some work ...
      endMeasurement();
      // Output: Performance: Data processing { duration: "123.45ms" }

      // Measure multiple operations
      const endTotal = LoggingUtils.measurePerformance("Total processing");

      const endStep1 = LoggingUtils.measurePerformance("Step 1");
      // ... do step 1 ...
      endStep1();

      const endStep2 = LoggingUtils.measurePerformance("Step 2");
      // ... do step 2 ...
      endStep2();

      endTotal();
  • measureAsyncPerformance:function
    • Measures the performance of an asynchronous function.

      Type Parameters

      • T

      Parameters

      • label: string

        The label for the performance measurement

      • fn: (() => Promise<T>)

        The async function to measure

          • (): Promise<T>
          • Returns Promise<T>

      Returns Promise<T>

      A Promise with the function result

      Example

      // Measure an API call
      const result = await LoggingUtils.measureAsyncPerformance(
      "API call",
      async () => await fetchData()
      );

      // Measure multiple async operations
      const results = await Promise.all([
      LoggingUtils.measureAsyncPerformance("API 1", () => fetchData1()),
      LoggingUtils.measureAsyncPerformance("API 2", () => fetchData2())
      ]);
  • warn:function
    • Logs a warning message with optional metadata.

      Parameters

      • message: string

        The warning message to log

      • options: Omit<LogOptions, "level"> = {}

        Optional configuration for the log entry

      Returns void

      Example

      // Simple warning
      LoggingUtils.warn("Resource usage high");

      // Warning with data and prefix
      LoggingUtils.warn("High memory usage", {
      prefix: "System",
      data: { cpu: "80%", memory: "75%" }
      });

      // Warning with grouping
      LoggingUtils.warn("Multiple issues detected", {
      group: true,
      data: { issues: ["CPU", "Memory", "Disk"] }
      });