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