]>
Commit | Line | Data |
---|---|---|
9f10b292 | 1 | // Thanks http://tostring.it/2014/06/23/advanced-logging-with-nodejs/ |
4d4e5cd4 C |
2 | import * as mkdirp from 'mkdirp' |
3 | import * as path from 'path' | |
4 | import * as winston from 'winston' | |
3fd3ab2d | 5 | import { CONFIG } from '../initializers' |
8c308c2b | 6 | |
65fcc311 | 7 | const label = CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT |
320d6275 C |
8 | |
9 | // Create the directory if it does not exist | |
65fcc311 | 10 | mkdirp.sync(CONFIG.STORAGE.LOG_DIR) |
320d6275 | 11 | |
23e27dd5 C |
12 | // Use object for better performances (~ O(1)) |
13 | const excludedKeys = { | |
14 | level: true, | |
15 | message: true, | |
16 | splat: true, | |
17 | timestamp: true, | |
18 | label: true | |
19 | } | |
20 | function keysExcluder (key, value) { | |
d5b7d911 C |
21 | if (excludedKeys[key] === true) return undefined |
22 | ||
23 | if (key === 'err') return value.stack | |
24 | ||
25 | return value | |
23e27dd5 C |
26 | } |
27 | ||
276d03ed | 28 | const consoleLoggerFormat = winston.format.printf(info => { |
23e27dd5 C |
29 | let additionalInfos = JSON.stringify(info, keysExcluder, 2) |
30 | if (additionalInfos === '{}') additionalInfos = '' | |
2af4fa4d | 31 | else additionalInfos = ' ' + additionalInfos |
23e27dd5 | 32 | |
1e9d7b60 | 33 | if (info.message && info.message.stack !== undefined) info.message = info.message.stack |
2af4fa4d | 34 | return `[${info.label}] ${info.timestamp} ${info.level}: ${info.message}${additionalInfos}` |
23e27dd5 C |
35 | }) |
36 | ||
d5b7d911 C |
37 | const jsonLoggerFormat = winston.format.printf(infoArg => { |
38 | let info = infoArg.err | |
39 | ? Object.assign({}, infoArg, { err: infoArg.err.stack }) | |
40 | : infoArg | |
41 | ||
42 | if (infoArg.message && infoArg.message.stack !== undefined) { | |
43 | info = Object.assign({}, info, { message: infoArg.message.stack }) | |
44 | } | |
276d03ed C |
45 | |
46 | return JSON.stringify(info) | |
47 | }) | |
48 | ||
23e27dd5 | 49 | const timestampFormatter = winston.format.timestamp({ |
0647f472 | 50 | format: 'YYYY-MM-DD HH:mm:ss.SSS' |
23e27dd5 C |
51 | }) |
52 | const labelFormatter = winston.format.label({ | |
53 | label | |
54 | }) | |
55 | ||
56 | const logger = new winston.createLogger({ | |
57 | level: CONFIG.LOG.LEVEL, | |
9f10b292 C |
58 | transports: [ |
59 | new winston.transports.File({ | |
23e27dd5 | 60 | filename: path.join(CONFIG.STORAGE.LOG_DIR, 'peertube.log'), |
9f10b292 | 61 | handleExceptions: true, |
9f10b292 C |
62 | maxsize: 5242880, |
63 | maxFiles: 5, | |
23e27dd5 | 64 | format: winston.format.combine( |
0647f472 | 65 | winston.format.timestamp(), |
23e27dd5 C |
66 | labelFormatter, |
67 | winston.format.splat(), | |
276d03ed | 68 | jsonLoggerFormat |
23e27dd5 | 69 | ) |
9f10b292 C |
70 | }), |
71 | new winston.transports.Console({ | |
e8cb4409 | 72 | handleExceptions: true, |
9f10b292 | 73 | humanReadableUnhandledException: true, |
23e27dd5 C |
74 | format: winston.format.combine( |
75 | timestampFormatter, | |
76 | winston.format.splat(), | |
77 | labelFormatter, | |
78 | winston.format.colorize(), | |
276d03ed | 79 | consoleLoggerFormat |
23e27dd5 | 80 | ) |
9f10b292 C |
81 | }) |
82 | ], | |
83 | exitOnError: true | |
84 | }) | |
8c308c2b | 85 | |
05e67d62 C |
86 | function bunyanLogFactory (level: string) { |
87 | return function () { | |
88 | let meta = null | |
89 | let args = [].concat(arguments) | |
90 | ||
91 | if (arguments[ 0 ] instanceof Error) { | |
92 | meta = arguments[ 0 ].toString() | |
93 | args = Array.prototype.slice.call(arguments, 1) | |
94 | args.push(meta) | |
95 | } else if (typeof (args[ 0 ]) !== 'string') { | |
96 | meta = arguments[ 0 ] | |
97 | args = Array.prototype.slice.call(arguments, 1) | |
98 | args.push(meta) | |
99 | } | |
100 | ||
101 | logger[ level ].apply(logger, args) | |
102 | } | |
103 | } | |
104 | const bunyanLogger = { | |
105 | trace: bunyanLogFactory('debug'), | |
106 | debug: bunyanLogFactory('debug'), | |
107 | info: bunyanLogFactory('info'), | |
108 | warn: bunyanLogFactory('warn'), | |
109 | error: bunyanLogFactory('error'), | |
110 | fatal: bunyanLogFactory('error') | |
111 | } | |
112 | ||
9f10b292 | 113 | // --------------------------------------------------------------------------- |
c45f7f84 | 114 | |
23e27dd5 C |
115 | export { |
116 | timestampFormatter, | |
117 | labelFormatter, | |
276d03ed | 118 | consoleLoggerFormat, |
05e67d62 C |
119 | logger, |
120 | bunyanLogger | |
23e27dd5 | 121 | } |