| 8 | } |
| 9 | |
| 10 | export const setLogStream = (options: LogstreamOptions) => { |
| 11 | const { cwd = process.cwd() } = options |
| 12 | |
| 13 | let stream: fs.WriteStream | undefined |
| 14 | |
| 15 | if (options.logfile) { |
| 16 | const outPath = path.resolve(cwd, options.logfile) |
| 17 | |
| 18 | ensure(outPath) |
| 19 | logger.info('logfile', outPath) |
| 20 | |
| 21 | stream = fs.createWriteStream(outPath, { flags: 'a' }) |
| 22 | |
| 23 | logger.onLog = (entry) => { |
| 24 | stream?.write(JSON.stringify(entry) + '\n') |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | process.once('SIGINT', () => { |
| 29 | stream?.end() |
| 30 | }) |
| 31 | |
| 32 | return { |
| 33 | end() { |
| 34 | stream?.end() |
| 35 | }, |
| 36 | [Symbol.dispose]: () => { |
| 37 | stream?.end() |
| 38 | }, |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | const ensure = (outPath: string) => { |
| 43 | const dirname = path.dirname(outPath) |