diff options
author | Chocobozzz <me@florianbigard.com> | 2019-11-05 11:08:51 +0100 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2019-11-05 11:08:51 +0100 |
commit | 959dbbd7bf655e9a36a310838dae75b9909f0096 (patch) | |
tree | b7801ddcb78098fc51ecc4678074d251453bbfa3 /server/helpers/logger.ts | |
parent | 5d9e4eaabe87ff1b115114b01075b9fabd2b0c5e (diff) | |
download | PeerTube-959dbbd7bf655e9a36a310838dae75b9909f0096.tar.gz PeerTube-959dbbd7bf655e9a36a310838dae75b9909f0096.tar.zst PeerTube-959dbbd7bf655e9a36a310838dae75b9909f0096.zip |
Avoid circular error in logger
Diffstat (limited to 'server/helpers/logger.ts')
-rw-r--r-- | server/helpers/logger.ts | 30 |
1 files changed, 20 insertions, 10 deletions
diff --git a/server/helpers/logger.ts b/server/helpers/logger.ts index 8603dd761..d21746963 100644 --- a/server/helpers/logger.ts +++ b/server/helpers/logger.ts | |||
@@ -12,22 +12,33 @@ const label = CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT | |||
12 | // FIXME: use async | 12 | // FIXME: use async |
13 | mkdirpSync(CONFIG.STORAGE.LOG_DIR) | 13 | mkdirpSync(CONFIG.STORAGE.LOG_DIR) |
14 | 14 | ||
15 | function loggerReplacer (key: string, value: any) { | 15 | function getLoggerReplacer () { |
16 | if (value instanceof Error) { | 16 | const seen = new WeakSet() |
17 | const error = {} | ||
18 | 17 | ||
19 | Object.getOwnPropertyNames(value).forEach(key => error[ key ] = value[ key ]) | 18 | // Thanks: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Cyclic_object_value#Examples |
19 | return (key: string, value: any) => { | ||
20 | if (typeof value === 'object' && value !== null) { | ||
21 | if (seen.has(value)) return | ||
20 | 22 | ||
21 | return error | 23 | seen.add(value) |
22 | } | 24 | } |
25 | |||
26 | if (value instanceof Error) { | ||
27 | const error = {} | ||
28 | |||
29 | Object.getOwnPropertyNames(value).forEach(key => error[ key ] = value[ key ]) | ||
23 | 30 | ||
24 | return value | 31 | return error |
32 | } | ||
33 | |||
34 | return value | ||
35 | } | ||
25 | } | 36 | } |
26 | 37 | ||
27 | const consoleLoggerFormat = winston.format.printf(info => { | 38 | const consoleLoggerFormat = winston.format.printf(info => { |
28 | const obj = omit(info, 'label', 'timestamp', 'level', 'message') | 39 | const obj = omit(info, 'label', 'timestamp', 'level', 'message') |
29 | 40 | ||
30 | let additionalInfos = JSON.stringify(obj, loggerReplacer, 2) | 41 | let additionalInfos = JSON.stringify(obj, getLoggerReplacer(), 2) |
31 | 42 | ||
32 | if (additionalInfos === undefined || additionalInfos === '{}') additionalInfos = '' | 43 | if (additionalInfos === undefined || additionalInfos === '{}') additionalInfos = '' |
33 | else additionalInfos = ' ' + additionalInfos | 44 | else additionalInfos = ' ' + additionalInfos |
@@ -36,7 +47,7 @@ const consoleLoggerFormat = winston.format.printf(info => { | |||
36 | }) | 47 | }) |
37 | 48 | ||
38 | const jsonLoggerFormat = winston.format.printf(info => { | 49 | const jsonLoggerFormat = winston.format.printf(info => { |
39 | return JSON.stringify(info, loggerReplacer) | 50 | return JSON.stringify(info, getLoggerReplacer()) |
40 | }) | 51 | }) |
41 | 52 | ||
42 | const timestampFormatter = winston.format.timestamp({ | 53 | const timestampFormatter = winston.format.timestamp({ |
@@ -47,7 +58,6 @@ const labelFormatter = winston.format.label({ | |||
47 | }) | 58 | }) |
48 | 59 | ||
49 | const fileLoggerOptions: FileTransportOptions = { | 60 | const fileLoggerOptions: FileTransportOptions = { |
50 | |||
51 | filename: path.join(CONFIG.STORAGE.LOG_DIR, 'peertube.log'), | 61 | filename: path.join(CONFIG.STORAGE.LOG_DIR, 'peertube.log'), |
52 | handleExceptions: true, | 62 | handleExceptions: true, |
53 | format: winston.format.combine( | 63 | format: winston.format.combine( |