@graphql-hive/logger
Version:
118 lines (113 loc) • 4.51 kB
text/typescript
type MaybeLazy<T> = T | (() => T);
type AttributeValue = string | number | boolean | {
[key: string | number]: AttributeValue;
} | AttributeValue[] | Object | null | undefined | any;
type Attributes = AttributeValue[] | {
[key: string | number]: AttributeValue;
};
interface LogWriter {
write(level: LogLevel, attrs: Attributes | null | undefined, msg: string | null | undefined): void | Promise<void>;
flush(): void | Promise<void>;
}
declare class MemoryLogWriter implements LogWriter {
logs: {
level: LogLevel;
msg?: string;
attrs?: unknown;
}[];
write(level: LogLevel, attrs: Record<string, any>, msg: string | null | undefined): void;
flush(): void;
}
declare const asciMap: {
timestamp: string;
trace: string;
debug: string;
info: string;
warn: string;
error: string;
message: string;
reset: string;
};
declare class ConsoleLogWriter implements LogWriter {
#private;
color(style: keyof typeof asciMap, text: string | null | undefined): string | null | undefined;
write(level: LogLevel, attrs: Attributes | null | undefined, msg: string | null | undefined): void;
flush(): void;
}
declare class JSONLogWriter implements LogWriter {
write(level: LogLevel, attrs: Attributes | null | undefined, msg: string | null | undefined): void;
flush(): void;
}
type LogLevel = 'trace' | 'debug' | 'info' | 'warn' | 'error';
interface LoggerOptions {
/**
* The minimum log level to log.
*
* Providing `false` will disable all logging.
*
* @default env.LOG_LEVEL || env.DEBUG ? 'debug' : 'info'
*/
level?: LogLevel | false;
/** A prefix to include in every log's message. */
prefix?: string;
/**
* The attributes to include in all logs. Is mainly used to pass the parent
* attributes when creating {@link Logger.child child loggers}.
*/
attrs?: Attributes;
/**
* The log writers to use when writing logs.
*
* @default [new ConsoleLogWriter()]
*/
writers?: [LogWriter, ...LogWriter[]];
}
declare class Logger implements LogWriter {
#private;
constructor(opts?: LoggerOptions);
get prefix(): string | undefined;
get level(): false | LogLevel;
write(level: LogLevel, attrs: Attributes | null | undefined, msg: string | null | undefined): void;
flush(): Promise<void> | undefined;
child(prefix: string): Logger;
child(attrs: Attributes, prefix?: string): Logger;
log(level: LogLevel): void;
log(level: LogLevel, attrs: MaybeLazy<Attributes>): void;
log(level: LogLevel, msg: string, ...interpol: unknown[]): void;
log(level: LogLevel, attrs: MaybeLazy<Attributes>, msg: string, ...interpol: unknown[]): void;
trace(): void;
trace(attrs: MaybeLazy<Attributes>): void;
trace(msg: string, ...interpol: unknown[]): void;
trace(attrs: MaybeLazy<Attributes>, msg: string, ...interpol: unknown[]): void;
debug(): void;
debug(attrs: MaybeLazy<Attributes>): void;
debug(msg: string, ...interpol: unknown[]): void;
debug(attrs: MaybeLazy<Attributes>, msg: string, ...interpol: unknown[]): void;
info(): void;
info(attrs: MaybeLazy<Attributes>): void;
info(msg: string, ...interpol: unknown[]): void;
info(attrs: MaybeLazy<Attributes>, msg: string, ...interpol: unknown[]): void;
warn(): void;
warn(attrs: MaybeLazy<Attributes>): void;
warn(msg: string, ...interpol: unknown[]): void;
warn(attrs: MaybeLazy<Attributes>, msg: string, ...interpol: unknown[]): void;
error(): void;
error(attrs: MaybeLazy<Attributes>): void;
error(msg: string, ...interpol: unknown[]): void;
error(attrs: MaybeLazy<Attributes>, msg: string, ...interpol: unknown[]): void;
}
type LazyLoggerMessage = (() => any | any[]) | any;
/** @deprecated Please migrate to using the {@link Logger} instead.*/
declare class LegacyLogger {
#private;
constructor(logger: Logger);
static from(logger: Logger): LegacyLogger;
log(...args: any[]): void;
warn(...args: any[]): void;
info(...args: any[]): void;
error(...args: any[]): void;
debug(...lazyArgs: LazyLoggerMessage[]): void;
child(name: string | Record<string, string | number>): LegacyLogger;
addPrefix(prefix: string | Record<string, string | number>): LegacyLogger;
}
export { ConsoleLogWriter, JSONLogWriter, type LazyLoggerMessage, LegacyLogger, type LogLevel, type LogWriter, Logger, type LoggerOptions, MemoryLogWriter };