aboutsummaryrefslogtreecommitdiffhomepage
path: root/server
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2018-07-30 10:59:31 +0200
committerChocobozzz <me@florianbigard.com>2018-07-30 11:34:34 +0200
commite20015d744fe9d637bfa6924194f54eecbbd6722 (patch)
treec4dc061e78d7084b7f46e62c341f89ff4005e4d0 /server
parent655b549048a480edeedf265a018aac951af3a3c1 (diff)
downloadPeerTube-e20015d744fe9d637bfa6924194f54eecbbd6722.tar.gz
PeerTube-e20015d744fe9d637bfa6924194f54eecbbd6722.tar.zst
PeerTube-e20015d744fe9d637bfa6924194f54eecbbd6722.zip
Fiw winston meta log
Diffstat (limited to 'server')
-rw-r--r--server/helpers/logger.ts40
1 files changed, 16 insertions, 24 deletions
diff --git a/server/helpers/logger.ts b/server/helpers/logger.ts
index 7fdfe2125..6d369a8fb 100644
--- a/server/helpers/logger.ts
+++ b/server/helpers/logger.ts
@@ -9,36 +9,28 @@ const label = CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT
9// Create the directory if it does not exist 9// Create the directory if it does not exist
10mkdirp.sync(CONFIG.STORAGE.LOG_DIR) 10mkdirp.sync(CONFIG.STORAGE.LOG_DIR)
11 11
12// Use object for better performances (~ O(1)) 12function loggerReplacer (key: string, value: any) {
13const excludedKeys = { 13 if (value instanceof Error) {
14 level: true, 14 const error = {}
15 message: true, 15
16 splat: true, 16 Object.getOwnPropertyNames(value).forEach(key => error[ key ] = value[ key ])
17 timestamp: true,
18 label: true
19}
20function keysExcluder (key, value) {
21 if (excludedKeys[key] === true) return undefined
22 17
23 if (key === 'err') return value.stack 18 return error
19 }
24 20
25 return value 21 return value
26} 22}
27 23
28const consoleLoggerFormat = winston.format.printf(info => { 24const consoleLoggerFormat = winston.format.printf(info => {
29 let additionalInfos = JSON.stringify(info, keysExcluder, 2) 25 let additionalInfos = JSON.stringify(info.meta, loggerReplacer, 2)
30 if (additionalInfos === '{}') additionalInfos = '' 26 if (additionalInfos === undefined || additionalInfos === '{}') additionalInfos = ''
31 else additionalInfos = ' ' + additionalInfos 27 else additionalInfos = ' ' + additionalInfos
32 28
33 return `[${info.label}] ${info.timestamp} ${info.level}: ${info.message}${additionalInfos}` 29 return `[${info.label}] ${info.timestamp} ${info.level}: ${info.message}${additionalInfos}`
34}) 30})
35 31
36const jsonLoggerFormat = winston.format.printf(infoArg => { 32const jsonLoggerFormat = winston.format.printf(info => {
37 let info = infoArg.err 33 return JSON.stringify(info, loggerReplacer)
38 ? Object.assign({}, infoArg, { err: infoArg.err.stack })
39 : infoArg
40
41 return JSON.stringify(info)
42}) 34})
43 35
44const timestampFormatter = winston.format.timestamp({ 36const timestampFormatter = winston.format.timestamp({
@@ -50,16 +42,18 @@ const labelFormatter = winston.format.label({
50 42
51const logger = winston.createLogger({ 43const logger = winston.createLogger({
52 level: CONFIG.LOG.LEVEL, 44 level: CONFIG.LOG.LEVEL,
45 format: winston.format.combine(
46 labelFormatter,
47 winston.format.splat()
48 ),
53 transports: [ 49 transports: [
54 new winston.transports.File({ 50 new winston.transports.File({
55 filename: path.join(CONFIG.STORAGE.LOG_DIR, 'peertube.log'), 51 filename: path.join(CONFIG.STORAGE.LOG_DIR, 'peertube.log'),
56 handleExceptions: true, 52 handleExceptions: true,
57 maxsize: 5242880, 53 maxsize: 1024 * 1024 * 30,
58 maxFiles: 5, 54 maxFiles: 5,
59 format: winston.format.combine( 55 format: winston.format.combine(
60 winston.format.timestamp(), 56 winston.format.timestamp(),
61 labelFormatter,
62 winston.format.splat(),
63 jsonLoggerFormat 57 jsonLoggerFormat
64 ) 58 )
65 }), 59 }),
@@ -67,8 +61,6 @@ const logger = winston.createLogger({
67 handleExceptions: true, 61 handleExceptions: true,
68 format: winston.format.combine( 62 format: winston.format.combine(
69 timestampFormatter, 63 timestampFormatter,
70 winston.format.splat(),
71 labelFormatter,
72 winston.format.colorize(), 64 winston.format.colorize(),
73 consoleLoggerFormat 65 consoleLoggerFormat
74 ) 66 )