UNPKG

@scayle/storefront-core

Version:

Collection of essential utilities to work with the Storefront API

113 lines (112 loc) 3.16 kB
/** * Represents a log level. */ export type LogLevel = 'debug' | 'info' | 'warn' | 'error'; /** * Represents a single log entry. */ export interface LogEntry { level: LogLevel; space: string; message: string; data?: object; } /** * A function that handles log entries. * * @param entry The log entry to handle. */ export type LogHandler = (entry: LogEntry) => void; /** * Options for configuring the logger. */ export interface LogOptions { handler?: LogHandler; space: string; } /** * A logger that provides structured logging with namespaces. * Supports custom log handlers or if no handler is provided, logging to the console. */ export declare class Log { /** * The default log instance, logging to the console with the `global` namespace. */ static readonly default: Log; _handler?: LogHandler; _space: string; _type: string; /** * Creates a new Log instance. * * @param options Options for the logger. */ constructor(options?: LogOptions); /** * Logs a debug message. * * @param message The debug message. * @param data Optional additional data. */ debug(message: string, data?: any): void; /** * Logs an informational message. * * @param message The informational message. * @param data Optional additional data. */ info(message: string, data?: any): void; /** * Logs a warning message. * * @param message The warning message. * @param data Optional additional data. */ warn(message: string, data?: any): void; /** * Logs an error message. * * @param message The error message or an Error object. * @param data Optional additional data. */ error(message: string | Error, data?: any): void; /** * Writes a log entry. * * @param level The log level. * @param message The log message. * @param data Optional additional data. */ write(level: LogLevel, message: string, data?: any): void; /** * Creates a new Log instance with a sub-namespace. * Namespaces are separated by dots (e.g., "global.api.getUser"). * * @param subSpace The sub-namespace to add in camel-case (e.g. `api` and `api.getUser`) * * @returns A new Log instance with the updated namespace. */ space(subSpace: string): Log; /** * Measures the execution time of an asynchronous function and logs a debug message. * * @param message A message to include in the log output. * @param func The asynchronous function to time. * * @returns A Promise that resolves with the result of the timed function. */ time<T = any>(message: string, func: () => T): Promise<T>; /** * Attaches the log instance to a request object. * This is typically used in server middleware. * * @param req The request object. * @param log The Log instance to attach. * * @example * Log.attachToRequest(req, new Log({ space: 'myModule' })) * * @deprecated */ static attachToRequest(req: any, log: Log): void; }