import { Writable } from 'stream' import output from './output' import { EOutputLevel, ORIGIN_STDOUT_WRITE, ORIGIN_STDERR_WRITE } from './constants' import type { EOutputPrefix } from './constants' function buildLoggerWithPrefix(level: EOutputLevel, prefix: EOutputPrefix) { const stdout = new Writable({ // eslint-disable-next-line @typescript-eslint/no-explicit-any write(chunk: any, encoding: BufferEncoding, callback: (error?: Error | null) => void) { output(level, chunk.toString(), prefix) callback() }, }) return stdout } function setOrginLogger() { process.stdout.write = ORIGIN_STDOUT_WRITE process.stderr.write = ORIGIN_STDERR_WRITE } function setPrefixLogger(prefix: EOutputPrefix) { process.stdout.write = function (str: string) { return output(EOutputLevel.info, str, prefix) } process.stderr.write = function (str: string) { return output(EOutputLevel.error, str, prefix) } } export { buildLoggerWithPrefix, setPrefixLogger, setOrginLogger }