@ceeblue/web-utils
    Preparing search index...

    Interface ILog

    Logging interface used throughout the library:

    • filter logs independently of the browser
    • subscribe to logs to run some job on them
    • observe every log before level filtering
    • intercept logs to change the default behavior
    • redirect logs to another logging engine
    • redefine log messages, for example by adding a prefix

    Four LogLevel values are available: 'error', 'warn', 'info', and 'debug', matching the browser console methods. Use ILog.level, ILog.on, and ILog.observe to customize log handling.

    // filter log level globally, independently of the browser
    import { log } from '@ceeblue/web-utils';
    log.level = LogLevel.WARN; // displays errors and warnings

    // filter log level only for the Player component
    player.log.level = false; // no logs at all for the Player component

    // Intercept and redirect all logs to the console, like the default behavior
    // but without calling the local log.on handlers
    import { log } from '@ceeblue/web-utils';
    log.on = (level:LogLevel,args:unknown[]) => {
    console[level](...args.splice(0)); // args is empty after this call = final interception
    }

    // Intercept logs from the Player component and display only WARN logs
    player.log.on = (level:LogLevel,args:unknown[]) => {
    if (level !== LogLevel.WARN) {
    args.length = 0; // args is empty after this call = final interception
    }
    }

    // Subscribe and redirect all logs to a file logger
    import { log } from '@ceeblue/web-utils';
    log.on = (level:LogLevel,args:unknown[]) => {
    fileLogger[level](...args); // unchanged args allow the default behavior to continue
    }

    // Observe every log of the Player component, regardless of the configured log levels
    player.log.observe = (level:LogLevel,args:readonly unknown[]) => {
    // read-only: the console and the log.on handlers are unaffected
    // format the args rather than retain the live objects they hold,
    // noBin skips the binary data (media samples)
    telemetry.push(level, Util.stringify(args, { noBin: true }));
    }

    // Redefine the log to add some prefix indication
    class Player {
    connector = new Connector();
    constructor() {
    connector.log = this.log.bind(this,"Connector log:");
    }
    }
    interface ILog {
        level?: boolean | LogLevel;
        observe?: (level: LogLevel, args: readonly unknown[]) => void;
        on: (level: LogLevel, args: unknown[]) => void;
        (...args: unknown[]): Log;
    }
    • Build a log

      Parameters

      • ...args: unknown[]

      Returns Log

    Index
    level?: boolean | LogLevel

    Change log level, default log level is LogLevel.INFO. A boolean can be used to let every log pass with true or disable every log with false.

    To debug production code without modifying it you can use a special query parameter called !cb-override-log-level to override this configuration.

    observe?: (level: LogLevel, args: readonly unknown[]) => void

    Observe every log before any level filter or ILog.on handler, without being able to change it: the console and the ILog.on handlers behave as without observer. On the global log, the observer receives logs from every component; otherwise it receives the logs emitted through this logger, including those of the components bound to it.

    Logs emitted synchronously while an observer runs are not observed, to prevent recursion.

    Type Declaration

      • (level: LogLevel, args: readonly unknown[]): void
      • Parameters

        • level: LogLevel

          log level

        • args: readonly unknown[]

          frozen shallow copy of the arguments. Referenced objects are not cloned or frozen; avoid retaining them when they may contain large or mutable values such as media samples.

        Returns void

    on: (level: LogLevel, args: unknown[]) => void

    Intercept, redefine, or redirect any log. Clearing args intercepts the log, preventing subsequent handlers and console output. An exception thrown by the handler does not reach the caller: it is rethrown in a microtask, where it is reported as an uncaught error. Only the first failure of a handler is reported.

    Type Declaration

      • (level: LogLevel, args: unknown[]): void
      • Parameters

        • level: LogLevel

          log level

        • args: unknown[]

          mutable args of the log: clear them to intercept the log

        Returns void