aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/helpers
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2019-11-05 11:08:51 +0100
committerChocobozzz <me@florianbigard.com>2019-11-05 11:08:51 +0100
commit959dbbd7bf655e9a36a310838dae75b9909f0096 (patch)
treeb7801ddcb78098fc51ecc4678074d251453bbfa3 /server/helpers
parent5d9e4eaabe87ff1b115114b01075b9fabd2b0c5e (diff)
downloadPeerTube-959dbbd7bf655e9a36a310838dae75b9909f0096.tar.gz
PeerTube-959dbbd7bf655e9a36a310838dae75b9909f0096.tar.zst
PeerTube-959dbbd7bf655e9a36a310838dae75b9909f0096.zip
Avoid circular error in logger
Diffstat (limited to 'server/helpers')
-rw-r--r--server/helpers/logger.ts30
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
13mkdirpSync(CONFIG.STORAGE.LOG_DIR) 13mkdirpSync(CONFIG.STORAGE.LOG_DIR)
14 14
15function loggerReplacer (key: string, value: any) { 15function 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
27const consoleLoggerFormat = winston.format.printf(info => { 38const 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
38const jsonLoggerFormat = winston.format.printf(info => { 49const jsonLoggerFormat = winston.format.printf(info => {
39 return JSON.stringify(info, loggerReplacer) 50 return JSON.stringify(info, getLoggerReplacer())
40}) 51})
41 52
42const timestampFormatter = winston.format.timestamp({ 53const timestampFormatter = winston.format.timestamp({
@@ -47,7 +58,6 @@ const labelFormatter = winston.format.label({
47}) 58})
48 59
49const fileLoggerOptions: FileTransportOptions = { 60const 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(