@metreeca/tape - v0.10.0
    Preparing search index...

    Function log

    Retrieves loggers, configures LogTape, and guards functions.

    • Retrieves the root logger instance.

      Returns Logger

      The root logger with empty category

    • Retrieves a logger for the specified file path or URL.

      Extracts a hierarchical category array from the path for logger categorisation.

      Automatically configures LogTape on first use if the extracted category starts with "/" (local code) and LogTape is not yet configured. Default configuration includes console sink with visual severity prefixes, LogTape meta logger set to "warning" level, and local code logger at "info" level.

      Path resolution:

      • Local code (your project): Category starts with "/", followed by the package directory (the folder enclosing src/) and the segments after src/. If a URL is provided (for instance, import.meta.url), its pathname is parsed; when src/ is absent the filename only is used and the package directory is omitted. Extensions are removed; "index" is preserved as an explicit segment so that name.ts and name/index.ts remain distinguishable. The package directory keeps modules from sibling packages in a monorepo distinct.

      • Imported packages (from node_modules/): Non-scoped packages start with the bare package name (for instance, ["lodash", "index"] for a bare entry point); scoped packages use two segments (for instance, ["@metreeca", "post", "index"]). Build directories (dist, lib, build, out) and redundant package name folders are skipped. Extensions are removed from remaining paths; "index" is preserved. Hierarchical matching means a filter at lodash still applies to lodash/index.

      Parameters

      • url: string

        File path or URL to create logger category from

      Returns Logger

      Logger instance for the resolved category

    • Retrieves a logger for the specified category array.

      Automatically configures LogTape on first use if the category starts with "/" and LogTape is not yet configured. Default configuration includes console sink with visual severity prefixes, LogTape meta logger at "warning" level, and local code logger at "info" level.

      Parameters

      • category: readonly string[]

        Hierarchical logger category segments

      Returns Logger

      Logger instance for the specified category

    • Wraps a synchronous function with error handling and logging.

      Catches errors thrown by the function, logs them using the function's name as logger category, and returns undefined instead of propagating the error.

      Type Parameters

      • T extends unknown[]

        The tuple type of function arguments

      • R

        The return type of the function

      Parameters

      • f: (...args: T) => R

        Function to wrap with error handling

      Returns (...args: T) => R | undefined

      Wrapped function that returns the original result or undefined on error

    • Configures LogTape with a complete configuration object.

      Tip

      Repeated calls with a deep-equal configuration are silently accepted (the reset flag is ignored for comparison purposes). Applying a different configuration throws unless reset is set to true.

      Type Parameters

      • S extends string

        Sink identifier type

      • F extends string

        Filter identifier type

      Parameters

      • config: Config<S, F>

        LogTape configuration object with sinks, filters, and loggers

      Returns void

    • Configures LogTape with category-to-level mappings.

      Configures LogTape with a single console logger using visual severity prefixes and a configuration derived from a simplified representation mapping categories to minimum log levels:

      • Each key represents a LogTape category in label form: "/" (all internal code), "/utils" (internal module), "lodash" (non-scoped package), "@scope/pkg" (scoped package). A trailing / targets the index module (for instance, "/name/" matches only src/name/index.ts).

      • Each value specifies the minimum LogLevel for the category.

      Tip

      Repeated calls with a deep-equal mapping are silently accepted. Applying a different configuration throws unless a full Config object with reset set to true is used.

      Parameters

      • config: Record<string, LogLevel>

        Path-to-level mapping for logger configuration

      Returns void

    • Wraps an asynchronous function with error handling and logging.

      Catches errors thrown or rejected by the function, logs them using the function's name as logger category, and returns undefined instead of propagating the error.

      Type Parameters

      • T extends unknown[]

        The tuple type of function arguments

      • R

        The return type of the function

      Parameters

      • f: (...args: T) => Promise<R>

        Async function to wrap with error handling

      Returns (...args: T) => Promise<R | undefined>

      Wrapped function that returns a promise resolving to the original result or undefined on error