@visulima/pail
Version:
Highly configurable Logger for Node.js, Edge and Browser.
177 lines (158 loc) • 6.53 kB
text/typescript
import { AnsiColors } from '@visulima/colorize';
/**
Matches any [primitive value](https://developer.mozilla.org/en-US/docs/Glossary/Primitive).
@category Type
*/
type Primitive =
| null
| undefined
| string
| number
| boolean
| symbol
| bigint;
declare global {
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions -- It has to be an `interface` so that it can be merged.
interface SymbolConstructor {
readonly observable: symbol;
}
}
/**
Allows creating a union type by combining primitive types and literal types without sacrificing auto-completion in IDEs for the literal type part of the union.
Currently, when a union type of a primitive type is combined with literal types, TypeScript loses all information about the combined literals. Thus, when such type is used in an IDE with autocompletion, no suggestions are made for the declared literals.
This type is a workaround for [Microsoft/TypeScript#29729](https://github.com/Microsoft/TypeScript/issues/29729). It will be removed as soon as it's not needed anymore.
@example
```
import type {LiteralUnion} from 'type-fest';
// Before
type Pet = 'dog' | 'cat' | string;
const pet: Pet = '';
// Start typing in your TypeScript-enabled IDE.
// You **will not** get auto-completion for `dog` and `cat` literals.
// After
type Pet2 = LiteralUnion<'dog' | 'cat', string>;
const pet: Pet2 = '';
// You **will** get auto-completion for `dog` and `cat` literals.
```
@category Type
*/
type LiteralUnion<
LiteralType,
BaseType extends Primitive,
> = LiteralType | (BaseType & Record<never, never>);
declare class InteractiveStreamHook {
#private;
static readonly DRAIN = true;
constructor(stream: NodeJS.WriteStream);
active(): void;
erase(count: number): void;
inactive(separateHistory?: boolean): void;
renew(): void;
write(message: string): void;
}
type StreamType = "stderr" | "stdout";
declare class InteractiveManager {
#private;
constructor(stdout: InteractiveStreamHook, stderr: InteractiveStreamHook);
get lastLength(): number;
get outside(): number;
get isHooked(): boolean;
get isSuspended(): boolean;
erase(stream: StreamType, count?: number): void;
hook(): boolean;
resume(stream: StreamType, eraseRowCount?: number): void;
suspend(stream: StreamType, erase?: boolean): void;
unhook(separateHistory?: boolean): boolean;
update(stream: StreamType, rows: string[], from?: number): void;
private _clear;
}
declare global {
namespace VisulimaPail {
interface CustomMeta<L> {
}
}
}
interface Meta<L> extends VisulimaPail.CustomMeta<L> {
badge: string | undefined;
context: any[] | undefined;
date: Date | string;
error: Error | undefined;
groups: string[];
label: string | undefined;
message: Primitive | ReadonlyArray<unknown> | Record<PropertyKey, unknown>;
prefix: string | undefined;
repeated?: number | undefined;
scope: string[] | undefined;
suffix: string | undefined;
traceError: Error | undefined;
type: {
level: ExtendedRfc5424LogLevels | L;
name: string;
};
}
type ExtendedRfc5424LogLevels = "alert" | "critical" | "debug" | "emergency" | "error" | "informational" | "notice" | "trace" | "warning";
type DefaultLogTypes = "alert" | "await" | "complete" | "critical" | "debug" | "emergency" | "error" | "info" | "log" | "notice" | "pending" | "start" | "stop" | "success" | "trace" | "wait" | "warn" | "watch";
interface LoggerFunction {
(message: Message): void;
(...message: any[]): void;
}
interface LoggerConfiguration<L extends string> {
badge?: string;
color?: AnsiColors | undefined;
label: string;
logLevel: LiteralUnion<ExtendedRfc5424LogLevels, L>;
}
type LoggerTypesConfig<T extends string, L extends string> = Record<T, Partial<LoggerConfiguration<L>>>;
type DefaultLoggerTypes<L extends string = string> = Record<DefaultLogTypes, LoggerConfiguration<L>>;
type ReadonlyMeta<L extends string> = Readonly<Meta<L>>;
interface Reporter<L extends string> {
log: (meta: ReadonlyMeta<L>) => void;
}
interface StreamAwareReporter<L extends string> extends Reporter<L> {
setStderr: (stderr: NodeJS.WriteStream) => void;
setStdout: (stdout: NodeJS.WriteStream) => void;
}
interface LoggerTypesAwareReporter<T extends string, L extends string> extends Reporter<L> {
setLoggerTypes: (types: LoggerTypesConfig<T, L> & Partial<LoggerTypesConfig<DefaultLogTypes, L>>) => void;
}
interface StringifyAwareReporter<L extends string> extends Reporter<L> {
setStringify: (stringify: typeof JSON.stringify) => void;
}
interface InteractiveStreamReporter<L extends string> extends StreamAwareReporter<L> {
setInteractiveManager: (manager?: InteractiveManager) => void;
setIsInteractive: (interactive: boolean) => void;
}
interface Processor<L extends string> {
process: (meta: Meta<L>) => Meta<L>;
}
interface StringifyAwareProcessor<L extends string> extends Processor<L> {
setStringify: (stringify: typeof JSON.stringify) => void;
}
interface ConstructorOptions<T extends string, L extends string> {
disabled?: boolean;
logLevel?: LiteralUnion<ExtendedRfc5424LogLevels, L>;
logLevels?: Partial<Record<ExtendedRfc5424LogLevels, number>> & Record<L, number>;
messages?: {
timerEnd?: string;
timerStart?: string;
};
processors?: Processor<L>[];
rawReporter?: Reporter<L>;
reporters?: Reporter<L>[];
scope?: string[] | string;
throttle?: number;
throttleMin?: number;
types?: LoggerTypesConfig<T, L> & Partial<LoggerTypesConfig<DefaultLogTypes, L>>;
}
interface ServerConstructorOptions<T extends string, L extends string> extends ConstructorOptions<T, L> {
interactive?: boolean;
stderr: NodeJS.WriteStream;
stdout: NodeJS.WriteStream;
}
type Message = {
context?: any[] | undefined;
message: any;
prefix?: string;
suffix?: string;
};
export { type ConstructorOptions as C, type DefaultLoggerTypes as D, type ExtendedRfc5424LogLevels as E, InteractiveManager as I, type LoggerConfiguration as L, type Meta as M, type Processor as P, type Reporter as R, type StreamAwareReporter as S, type DefaultLogTypes as a, type LoggerFunction as b, type LoggerTypesAwareReporter as c, type LoggerTypesConfig as d, type LiteralUnion as e, type ServerConstructorOptions as f, type ReadonlyMeta as g, type StringifyAwareReporter as h, type InteractiveStreamReporter as i, type StringifyAwareProcessor as j };