mirror of
https://github.com/vercel/chat.git
synced 2026-09-14 18:32:29 +08:00
62 lines
1.7 KiB
TypeScript
62 lines
1.7 KiB
TypeScript
/**
|
|
* Logger types and implementations for chat-sdk
|
|
*/
|
|
|
|
export type LogLevel = "debug" | "info" | "warn" | "error" | "silent";
|
|
|
|
export interface Logger {
|
|
debug(message: string, ...args: unknown[]): void;
|
|
info(message: string, ...args: unknown[]): void;
|
|
warn(message: string, ...args: unknown[]): void;
|
|
error(message: string, ...args: unknown[]): void;
|
|
/** Create a sub-logger with a prefix */
|
|
child(prefix: string): Logger;
|
|
}
|
|
|
|
/**
|
|
* Default console logger implementation.
|
|
*/
|
|
export class ConsoleLogger implements Logger {
|
|
private prefix: string;
|
|
|
|
constructor(
|
|
private level: LogLevel = "info",
|
|
prefix = "chat-sdk",
|
|
) {
|
|
this.prefix = prefix;
|
|
}
|
|
|
|
private shouldLog(level: LogLevel): boolean {
|
|
const levels: LogLevel[] = ["debug", "info", "warn", "error", "silent"];
|
|
return levels.indexOf(level) >= levels.indexOf(this.level);
|
|
}
|
|
|
|
child(prefix: string): Logger {
|
|
return new ConsoleLogger(this.level, `${this.prefix}:${prefix}`);
|
|
}
|
|
|
|
// eslint-disable-next-line no-console
|
|
debug(message: string, ...args: unknown[]): void {
|
|
if (this.shouldLog("debug"))
|
|
console.debug(`[${this.prefix}] ${message}`, ...args);
|
|
}
|
|
|
|
// eslint-disable-next-line no-console
|
|
info(message: string, ...args: unknown[]): void {
|
|
if (this.shouldLog("info"))
|
|
console.info(`[${this.prefix}] ${message}`, ...args);
|
|
}
|
|
|
|
// eslint-disable-next-line no-console
|
|
warn(message: string, ...args: unknown[]): void {
|
|
if (this.shouldLog("warn"))
|
|
console.warn(`[${this.prefix}] ${message}`, ...args);
|
|
}
|
|
|
|
// eslint-disable-next-line no-console
|
|
error(message: string, ...args: unknown[]): void {
|
|
if (this.shouldLog("error"))
|
|
console.error(`[${this.prefix}] ${message}`, ...args);
|
|
}
|
|
}
|