]>
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) { | |
21 | return excludedKeys[key] === true ? undefined : value | |
22 | } | |
23 | ||
24 | const loggerFormat = winston.format.printf((info) => { | |
25 | let additionalInfos = JSON.stringify(info, keysExcluder, 2) | |
26 | if (additionalInfos === '{}') additionalInfos = '' | |
2af4fa4d | 27 | else additionalInfos = ' ' + additionalInfos |
23e27dd5 | 28 | |
2af4fa4d | 29 | return `[${info.label}] ${info.timestamp} ${info.level}: ${info.message}${additionalInfos}` |
23e27dd5 C |
30 | }) |
31 | ||
32 | const timestampFormatter = winston.format.timestamp({ | |
33 | format: 'YYYY-MM-dd HH:mm:ss.SSS' | |
34 | }) | |
35 | const labelFormatter = winston.format.label({ | |
36 | label | |
37 | }) | |
38 | ||
39 | const logger = new winston.createLogger({ | |
40 | level: CONFIG.LOG.LEVEL, | |
9f10b292 C |
41 | transports: [ |
42 | new winston.transports.File({ | |
23e27dd5 | 43 | filename: path.join(CONFIG.STORAGE.LOG_DIR, 'peertube.log'), |
9f10b292 | 44 | handleExceptions: true, |
9f10b292 C |
45 | maxsize: 5242880, |
46 | maxFiles: 5, | |
23e27dd5 C |
47 | format: winston.format.combine( |
48 | timestampFormatter, | |
49 | labelFormatter, | |
50 | winston.format.splat(), | |
51 | winston.format.json() | |
52 | ) | |
9f10b292 C |
53 | }), |
54 | new winston.transports.Console({ | |
9f10b292 C |
55 | handleExceptions: true, |
56 | humanReadableUnhandledException: true, | |
23e27dd5 C |
57 | format: winston.format.combine( |
58 | timestampFormatter, | |
59 | winston.format.splat(), | |
60 | labelFormatter, | |
61 | winston.format.colorize(), | |
62 | loggerFormat | |
63 | ) | |
9f10b292 C |
64 | }) |
65 | ], | |
66 | exitOnError: true | |
67 | }) | |
8c308c2b | 68 | |
9f10b292 | 69 | // --------------------------------------------------------------------------- |
c45f7f84 | 70 | |
23e27dd5 C |
71 | export { |
72 | timestampFormatter, | |
73 | labelFormatter, | |
74 | loggerFormat, | |
75 | logger | |
76 | } |