dump console errors to file 'debug.log'

Massimo Melina committed Apr 26, 2022 at 00:10 UTC cdc814553e195b4b4e321787f6491f0268f8c2c4
1 file changed +14 -1
server/src/log.ts
+14 -1
@@ -3,7 +3,7 @@
3 import Koa from 'koa'
4 import { Writable } from 'stream'
5 import { defineConfig, getConfig, subscribeConfig } from './config'
6 -import { createWriteStream, renameSync } from 'fs'
6 +import { createWriteStream, existsSync, renameSync, WriteStream } from 'fs'
7 import * as util from 'util'
8 import { stat } from 'fs/promises'
9 import { DAY } from './const'
@@ -100,3 +100,16 @@ export function log(): Koa.Middleware {
100 function doubleDigit(n: number) {
101 return n > 9 ? n : '0'+n
102 }
103 +
104 +{ // dump console.error to file
105 + const was = console.error
106 + let log: WriteStream
107 + console.error = function(...args: any[]) {
108 + was.apply(this, args)
109 + if (!log || !existsSync(log.path))
110 + log = createWriteStream('debug.log', { flags: 'a' })
111 + const params = args.map(x =>
112 + typeof x === 'string' ? x : JSON.stringify(x)).join(' ')
113 + log.write(new Date().toJSON() + ': ' + params + '\n')
114 + }
115 +}