snooplogg
Version:
Your mind on your logs and your logs on your mind
350 lines (306 loc) • 8.75 kB
TypeScript
declare class NanoBuffer<T> {
buffer: T[];
_head: number;
_maxSize: number;
_size: number;
constructor(maxSize?: number);
get head(): number;
get maxSize(): number;
set maxSize(new_maxSize: number);
get size(): number;
push(value: T): this;
clear(): this;
[Symbol.iterator](): {
next: () => {
value: T | undefined;
done: boolean;
};
};
}
interface 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;
}
interface 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;
}
interface 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;
}
interface 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;
}
interface 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;
}
interface 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;
declare function nsToRgb(text: string): Color;
type 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>;
interface SnoopLoggConfig {
colors?: boolean;
elements?: LogElements;
format?: LogFormatter | null;
historySize?: number;
}
interface BaseLogMessage {
args: unknown[];
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;
};
type LogMethod = (...args: unknown[]) => Logger;
declare const defaultElements: FormatLogElements;
declare const stripRegExp: RegExp;
declare class Functionator extends Function {
constructor(fn: (namespace: string) => Logger);
}
declare class Logger extends Functionator {
_log: LogMethod | undefined;
_trace: LogMethod | undefined;
_debug: LogMethod | undefined;
_info: LogMethod | undefined;
_warn: LogMethod | undefined;
_error: LogMethod | undefined;
_panic: LogMethod | undefined;
ns: string;
nsPath: string[];
root: SnoopLogg;
subnamespaces: Record<string, Logger>;
constructor(root: SnoopLogg, parent?: Logger | null, namespace?: string);
initChild(namespace: string): Logger;
initMethod(method: string): (...args: unknown[]) => this;
get log(): LogMethod;
get trace(): LogMethod;
get debug(): LogMethod;
get info(): LogMethod;
get warn(): LogMethod;
get error(): LogMethod;
get panic(): LogMethod;
}
declare class SnoopLogg extends Functionator {
allow: string | RegExp | null;
colors: boolean;
elements: LogElements;
format?: LogFormatter | null;
history: NanoBuffer<RawLogMessage>;
id: number;
ignore: RegExp | null;
onSnoopMessage: ((msg: RawLogMessage) => void) | null;
logger: Logger;
streams: Map<WritableLike, StreamConfig>;
constructor(conf?: SnoopLoggConfig);
config(conf?: SnoopLoggConfig): this;
dispatch({ args, id, method, ns, ts, uptime }: {
args: unknown[];
id?: number;
method: string;
ns: string;
ts: Date;
uptime: number;
}): void;
enable(pattern?: string | RegExp): this;
isEnabled(namespace: string): boolean;
pipe(stream: WritableLike, opts?: StreamOptions): this;
unpipe(stream: WritableLike): this;
snoop(nsPrefix?: string): this;
unsnoop(): this;
writeToStreams(msg: RawLogMessage): void;
get log(): LogMethod;
get trace(): LogMethod;
get debug(): LogMethod;
get info(): LogMethod;
get warn(): LogMethod;
get error(): LogMethod;
get panic(): LogMethod;
}
declare function defaultFormatter({ args, colors, elements, method, ns, uptime }: LogMessage, styles: StyleHelpers): string;
declare function isJSON(it: unknown): boolean;
declare const instance: SnoopLogg;
declare const log: LogMethod;
declare const trace: LogMethod;
declare const debug: LogMethod;
declare const info: LogMethod;
declare const warn: LogMethod;
declare const error: LogMethod;
declare const panic: LogMethod;
export { Logger, SnoopLogg, debug, instance as default, defaultElements, defaultFormatter, error, info, isJSON, log, nsToRgb, panic, stripRegExp, trace, warn };
export type { LogMethod };