| 4 | export type Instance = ReturnType<typeof create>; |
| 5 | |
| 6 | export function create(prefix?: string) { |
| 7 | const format = (...messages: any[]) => { |
| 8 | const formatted = messages.map((msg) => { |
| 9 | if (msg instanceof Error) { |
| 10 | return msg.stack || msg.toString(); |
| 11 | } |
| 12 | if (typeof msg === "object") { |
| 13 | return inspect(msg, { depth: null, colors: false }); |
| 14 | } |
| 15 | return msg; |
| 16 | }); |
| 17 | return `${prefix} ${formatted.join(" ")}`; |
| 18 | }; |
| 19 | const date = () => new Date().toISOString(); |
| 20 | return { |
| 21 | debug: (...messages: any[]) => { |
| 22 | if (process.env.DEBUG !== "true") return; |
| 23 | console.debug(date(), format(...messages)); |
| 24 | }, |
| 25 | log: (...messages: any[]) => { |
| 26 | console.log(date(), format(...messages)); |
| 27 | }, |
| 28 | error: (...messages: any[]) => { |
| 29 | console.error(date(), format(...messages)); |
| 30 | }, |
| 31 | format, |
| 32 | child: (childPrefix: string) => |
| 33 | create(prefix ? `${prefix} ${childPrefix}` : childPrefix), |
| 34 | }; |
| 35 | } |
| 36 | } |