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