snooplogg
Version:
Your mind on your logs and your logs on your mind
542 lines • 15.7 kB
text/typescript
//#region src/emitter.d.ts
/**
* Lightweight EventEmitter-like class used for the global SnoopLogg message
* bus.
*/
declare class SnoopEmitter {
private events;
addListener(event: string, listener: (...args: any[]) => void): this;
emit(event: string, ...args: any[]): boolean;
off(event: string, listenerToRemove: (...args: any[]) => void): this;
on(event: string, listener: (...args: any[]) => void): this;
removeListener(event: string, listener: (...args: any[]) => void): this;
}
//#endregion
//#region src/nanobuffer.d.ts
/**
* A lightweight, fixed-size value buffer.
*/
declare class NanoBuffer<T> {
/**
* The buffer where the values are stored.
*/
buffer: T[];
/**
* The index of the newest value in the buffer.
*/
_head: number;
/**
* The maximum number of values to store in the buffer.
*/
_maxSize: number;
/**
* The number of values in the buffer.
*/
_size: number;
/**
* Creates a `NanoBuffer` instance.
* @param maxSize The initial buffer size.
* @access public
*/
constructor(maxSize?: number);
/**
* Returns the index of the newest value in the buffer.
* @returns The index of the newest value in the buffer.
* @access public
*/
get head(): number;
/**
* Returns the maximum number of values in the buffer.
* @returns The max size of the buffer.
* @access public
*/
get maxSize(): number;
/**
* Changes the maximum number of values allowed in the buffer.
* @param new_maxSize The new max size of the buffer.
* @access public
*/
set maxSize(new_maxSize: number);
/**
* Returns the number of values in the buffer.
* @returns The size of the buffer.
* @access public
*/
get size(): number;
/**
* Inserts a new value into the buffer.
* @param value The value to store.
* @returns The NanoBuffer instance.
* @access public
*/
push(value: T): this;
/**
* Removes all values in the buffer.
* @returns The NanoBuffer instance.
* @access public
*/
clear(): this;
/**
* Creates an iterator function for this buffer.
* @return An iterator function.
* @access public
*/
[Symbol.iterator](): Iterator<T>;
}
//#endregion
//#region src/ns-to-rgb.d.ts
/**
* Deterministically generates a color from a string that isn't too dark to too
* light.
* @param text The string to generate a color from.
* @returns An RGB color object.
*/
declare function nsToRgb(text: string): Color;
//#endregion
//#region node_modules/.pnpm/ansi-styles@6.2.3/node_modules/ansi-styles/index.d.ts
type CSPair = {
// eslint-disable-line @typescript-eslint/naming-convention
/**
The ANSI terminal control sequence for starting this style.
*/
readonly open: string;
/**
The ANSI terminal control sequence for ending this style.
*/
readonly close: string;
};
type ColorBase = {
/**
The ANSI terminal control sequence for ending this color.
*/
readonly close: string;
ansi(code: number): string;
ansi256(code: number): string;
ansi16m(red: number, green: number, blue: number): string;
};
type Modifier = {
/**
Resets the current color chain.
*/
readonly reset: CSPair;
/**
Make text bold.
*/
readonly bold: CSPair;
/**
Emitting only a small amount of light.
*/
readonly dim: CSPair;
/**
Make text italic. (Not widely supported)
*/
readonly italic: CSPair;
/**
Make text underline. (Not widely supported)
*/
readonly underline: CSPair;
/**
Make text overline.
Supported on VTE-based terminals, the GNOME terminal, mintty, and Git Bash.
*/
readonly overline: CSPair;
/**
Inverse background and foreground colors.
*/
readonly inverse: CSPair;
/**
Prints the text, but makes it invisible.
*/
readonly hidden: CSPair;
/**
Puts a horizontal line through the center of the text. (Not widely supported)
*/
readonly strikethrough: CSPair;
};
type ForegroundColor = {
readonly black: CSPair;
readonly red: CSPair;
readonly green: CSPair;
readonly yellow: CSPair;
readonly blue: CSPair;
readonly cyan: CSPair;
readonly magenta: CSPair;
readonly white: CSPair;
/**
Alias for `blackBright`.
*/
readonly gray: CSPair;
/**
Alias for `blackBright`.
*/
readonly grey: CSPair;
readonly blackBright: CSPair;
readonly redBright: CSPair;
readonly greenBright: CSPair;
readonly yellowBright: CSPair;
readonly blueBright: CSPair;
readonly cyanBright: CSPair;
readonly magentaBright: CSPair;
readonly whiteBright: CSPair;
};
type BackgroundColor = {
readonly bgBlack: CSPair;
readonly bgRed: CSPair;
readonly bgGreen: CSPair;
readonly bgYellow: CSPair;
readonly bgBlue: CSPair;
readonly bgCyan: CSPair;
readonly bgMagenta: CSPair;
readonly bgWhite: CSPair;
/**
Alias for `bgBlackBright`.
*/
readonly bgGray: CSPair;
/**
Alias for `bgBlackBright`.
*/
readonly bgGrey: CSPair;
readonly bgBlackBright: CSPair;
readonly bgRedBright: CSPair;
readonly bgGreenBright: CSPair;
readonly bgYellowBright: CSPair;
readonly bgBlueBright: CSPair;
readonly bgCyanBright: CSPair;
readonly bgMagentaBright: CSPair;
readonly bgWhiteBright: CSPair;
};
type ConvertColor = {
/**
Convert from the RGB color space to the ANSI 256 color space.
@param red - (`0...255`)
@param green - (`0...255`)
@param blue - (`0...255`)
*/
rgbToAnsi256(red: number, green: number, blue: number): number;
/**
Convert from the RGB HEX color space to the RGB color space.
@param hex - A hexadecimal string containing RGB data.
*/
hexToRgb(hex: string): [red: number, green: number, blue: number];
/**
Convert from the RGB HEX color space to the ANSI 256 color space.
@param hex - A hexadecimal string containing RGB data.
*/
hexToAnsi256(hex: string): number;
/**
Convert from the ANSI 256 color space to the ANSI 16 color space.
@param code - A number representing the ANSI 256 color.
*/
ansi256ToAnsi(code: number): number;
/**
Convert from the RGB color space to the ANSI 16 color space.
@param red - (`0...255`)
@param green - (`0...255`)
@param blue - (`0...255`)
*/
rgbToAnsi(red: number, green: number, blue: number): number;
/**
Convert from the RGB HEX color space to the ANSI 16 color space.
@param hex - A hexadecimal string containing RGB data.
*/
hexToAnsi(hex: string): number;
};
declare const ansiStyles: {
readonly modifier: Modifier;
readonly color: ColorBase & ForegroundColor;
readonly bgColor: ColorBase & BackgroundColor;
readonly codes: ReadonlyMap<number, number>;
} & ForegroundColor & BackgroundColor & Modifier & ConvertColor;
//#endregion
//#region src/types.d.ts
interface WritableLike {
isTTY?: boolean;
on: (...args: any[]) => any;
removeListener: (...args: any[]) => any;
writableObjectMode?: boolean;
write: (...args: any[]) => any;
}
type LogFormatter = (msg: LogMessage, styles: StyleHelpers) => string;
type StyleHelpers = typeof ansiStyles & {
nsToRgb: typeof nsToRgb;
};
type FormatLogElements = {
error: (err: Error, styles: StyleHelpers) => string;
message: (msg: string, method: string, styles: StyleHelpers) => string;
method: (name: string, styles: StyleHelpers) => string;
namespace: (ns: string, styles: StyleHelpers) => string;
timestamp: (ts: Date, styles: StyleHelpers) => string;
uptime: (uptime: number, styles: StyleHelpers) => string;
};
type LogElements = Partial<FormatLogElements>;
declare const LogLevels: {
readonly trace: 10;
readonly debug: 20;
readonly log: 30;
readonly info: 40;
readonly warn: 50;
readonly error: 60;
readonly panic: 70;
};
type LogLevel = "trace" | "debug" | "log" | "info" | "warn" | "error" | "panic";
type LogLevelValue = (typeof LogLevels)[LogLevel];
interface SnoopLoggConfig {
colors?: boolean;
elements?: LogElements;
format?: LogFormatter | null;
historySize?: number;
logLevel?: LogLevel | LogLevelValue;
}
interface BaseLogMessage {
args: unknown[];
level: LogLevelValue;
method: string;
ns: string;
ts: Date;
uptime: number;
}
interface LogMessage extends BaseLogMessage {
colors: boolean;
elements: FormatLogElements;
}
interface StreamBase {
colors?: boolean;
elements?: LogElements;
format?: LogFormatter;
}
interface StreamConfig extends StreamBase {
onEnd?: () => void;
}
interface StreamOptions extends StreamBase {
flush?: boolean;
}
interface RawLogMessage extends BaseLogMessage {
id: number;
uptime: number;
}
type Color = {
r: number;
g: number;
b: number;
};
//#endregion
//#region src/snooplogg.d.ts
/**
* Describes the various log methods such as `info()`, `warn()`, etc.
*
* We have to export this type so that we can destructure and export the log
* methods in the index.ts file.
*/
type LogMethod = (...args: unknown[]) => Logger;
/**
* Default log element formatters.
*/
declare const defaultElements: FormatLogElements;
declare const stripRegExp: RegExp;
/**
* The secret sauce.
*/
declare class Functionator extends Function {
/**
* Initializes the base function used to create new namespaced child
* logger instances.
* @param fn A function that creates a child logger instance.
* @returns A function with the instantiated class's prototype.
* @access public
*/
constructor(fn: (namespace: string) => Logger);
}
/**
* The logger represents a namespace and can have a single parent and multiple
* child namespaces.
*/
declare class Logger extends Functionator {
#private;
ns: string;
nsPath: string[];
root: SnoopLogg;
subnamespaces: Record<string, Logger>;
/**
* Initializes a new logger instance by combining the parent namespace with
* this logger's namespace.
* @param root The root SnoogLogg instance.
* @param parent The parent logger instance used to construct the namespace.
* @param namespace The namespace of the logger.
* @access public
*/
constructor(root: SnoopLogg, parent?: Logger | null, namespace?: string);
/**
* Helper function to create a new child logger instance.
* @param namespace The namespace of the child logger.
* @returns A new child logger instance.
* @access public
*/
initChild(namespace: string): Logger;
/**
* Logs a message without a log method.
* @param args The log message arguments.
* @returns The logger instance.
* @access public
*/
get log(): LogMethod;
get trace(): LogMethod;
get debug(): LogMethod;
get info(): LogMethod;
get warn(): LogMethod;
get error(): LogMethod;
get panic(): LogMethod;
}
/**
* The public API for the SnoopLogg logger.
*
* The history buffer is disabled by default.
*/
declare class SnoopLogg extends Functionator {
allow: string | RegExp | null;
colors: boolean;
elements: LogElements;
format?: LogFormatter | null;
history: NanoBuffer<RawLogMessage>;
id: number;
ignore: RegExp | null;
logLevel: LogLevelValue;
onSnoopMessage: ((msg: RawLogMessage) => void) | null;
logger: Logger;
streams: Map<WritableLike, StreamConfig>;
/**
* Initializes the initial logger instance and configuration.
* @param conf The SnoogLogg configuration.
* @access public
*/
constructor(conf?: SnoopLoggConfig);
/**
* Validate and applies the SnoopLogg configuration.
* @param conf The SnoopLogg configuration.
* @param conf.colors When `true`, enables colors in the log messages.
* @param conf.elements A map of log element format functions.
* @param conf.format The log message formatter function.
* @param conf.historySize The maximum number of log messages to store in the history.
* @param conf.logLevel The minimum log level to log.
* @returns The SnoopLogg instance.
* @access private
*/
config(conf?: SnoopLoggConfig): this;
/**
* Internal function that dispatches a log message to all streams and
* SnoopLogg instances.
* @param msg The raw log message.
* @param msg.args The raw arguments passed to the log method.
* @param msg.id The unique identifier of the logger instance.
* @param msg.method The log method name.
* @param msg.ns The namespace of the log message's logger.
* @param msg.ts The timestamp for which the log message was created.
* @param msg.uptime The time the process has been running when the log
* message was created.
* @access private
*/
dispatch(params: {
args: unknown[];
id?: number;
level: LogLevelValue;
method: string;
ns: string;
ts: Date;
uptime: number;
}): void;
/**
* Sets the log level.
* @param logLevel The log level to set.
* @returns The SnoopLogg instance.
* @access private
*/
setLogLevel(logLevel: LogLevel | LogLevelValue): this;
/**
* Resets the current enabled and ignored namespaces.
* @param pattern The pattern(s) to enable or disable namespaces. When this
* value is `*`, all namespaces are enabled. Multiple namespaces can be
* separated by a comma or pipe character. Prefixing a namespace with a `-`
* character will disable the namespace. Wildcards can be used by prefixing
* the namespace with a `*` character.
* @returns The SnoopLogg instance.
* @access public
*/
enable(pattern?: string | RegExp): this;
/**
* Checks if a specific namespace is enabled.
* @param namespace The namespace to check.
* @returns `true` if the namespace is enabled, otherwise `false`.
* @access public
*/
isEnabled(namespace: string): boolean;
/**
* Adds a writable stream to pipe log messages to.
* @param stream The stream to pipe log messages to.
* @param opts The options object.
* @param opts.flush When `true`, immediately flushes the history to the stream.
* @returns The SnoopLogg instance.
* @access public
*/
pipe(stream: WritableLike, opts?: StreamOptions): this;
/**
* Removes a stream.
* @param stream The stream to remove.
* @returns The SnoopLogg instance.
* @access public
*/
unpipe(stream: WritableLike): this;
/**
* Listens for log messages from other SnoopLogg instances and optionally
* prepends a namespace prefix to the messages.
* @param nsPrefix The namespace prefix to prepend to the messages.
* @returns The SnoopLogg instance.
* @access public
*/
snoop(nsPrefix?: string): this;
/**
* Stops listening for log messages from other SnoopLogg instances.
* @returns The SnoopLogg instance.
* @access public
*/
unsnoop(): this;
/**
* Formats and writes a single log message to one stream.
* @param stream The target stream.
* @param config The stream-specific configuration.
* @param msg The raw log message.
* @access private
*/
writeToStream(stream: WritableLike, config: StreamConfig, msg: RawLogMessage): void;
/**
* Formats and writes the log message to all streams.
* @param msg The raw log message.
* @access private
*/
writeToStreams(msg: RawLogMessage): void;
/** Proxy log methods delegating to the root {@link Logger} instance. */
get trace(): LogMethod;
get debug(): LogMethod;
get log(): LogMethod;
get info(): LogMethod;
get warn(): LogMethod;
get error(): LogMethod;
get panic(): LogMethod;
}
//#endregion
//#region src/is-json.d.ts
/**
* Detects if a value is a plain object.
*/
declare function isJSON(it: unknown): boolean;
//#endregion
//#region src/index.d.ts
declare const snooplogg: SnoopLogg;
type LogMethod$1 = (...args: any[]) => void;
declare const log: LogMethod$1;
declare const trace: LogMethod$1;
declare const debug: LogMethod$1;
declare const info: LogMethod$1;
declare const warn: LogMethod$1;
declare const error: LogMethod$1;
declare const panic: LogMethod$1;
//#endregion
export { LogLevels, LogMethod, Logger, SnoopEmitter, SnoopLogg, debug, defaultElements, error, info, isJSON, log, nsToRgb, panic, snooplogg, stripRegExp, trace, warn };