import { yellow, gray, red, bold, blue,} from "https://deno.land/std@0.51.0/fmt/colors.ts";import { LogEntry, LogLevel, SinkFunction } from "./types.ts";
export type ColorFunction = (str: string) => string;
export interface ConsoleOptions { enableColors: boolean; colorOptions: Partial<ColorOptions>;}
export interface ColorOptions { debug: ColorFunction; info: ColorFunction; warn: ColorFunction; error: ColorFunction; critical: ColorFunction;}
export function consoleSink({ enableColors = true,colorOptions: { debug = gray, info = blue, warn = yellow, error = red, critical = (str: string) => bold(red(str)) } = {},}: Partial<ConsoleOptions> = {}): SinkFunction { return ({ level, formattedMessage }: LogEntry) => { let color; if (enableColors) { color = ({ [LogLevel.DEBUG]: debug, [LogLevel.INFO]: info, [LogLevel.WARN]: warn, [LogLevel.ERROR]: error, [LogLevel.CRITICAL]: critical, })[level] ?? ((x: string) => x); } else { color = (x: string) => x; }
console.log(color(formattedMessage)); };}
export function fileSink(filename: string): SinkFunction { const encoder = new TextEncoder();
return ({ formattedMessage }: LogEntry) => { Deno.writeFileSync( filename, encoder.encode(formattedMessage + "\n"), { create: true, append: true }, ); };}