]>
Commit | Line | Data |
---|---|---|
9f10b292 | 1 | // Thanks http://tostring.it/2014/06/23/advanced-logging-with-nodejs/ |
c9d5c64f | 2 | import { mkdirpSync } from 'fs-extra' |
4d4e5cd4 C |
3 | import * as path from 'path' |
4 | import * as winston from 'winston' | |
fcf4569f | 5 | import { FileTransportOptions } from 'winston/lib/winston/transports' |
6dd9de95 | 6 | import { CONFIG } from '../initializers/config' |
fd8710b8 | 7 | import { omit } from 'lodash' |
8c308c2b | 8 | |
65fcc311 | 9 | const label = CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT |
320d6275 C |
10 | |
11 | // Create the directory if it does not exist | |
fd8710b8 | 12 | // FIXME: use async |
c9d5c64f | 13 | mkdirpSync(CONFIG.STORAGE.LOG_DIR) |
320d6275 | 14 | |
e20015d7 C |
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 ]) | |
d5b7d911 | 20 | |
e20015d7 C |
21 | return error |
22 | } | |
d5b7d911 C |
23 | |
24 | return value | |
23e27dd5 C |
25 | } |
26 | ||
276d03ed | 27 | const consoleLoggerFormat = winston.format.printf(info => { |
fd8710b8 | 28 | const obj = omit(info, 'label', 'timestamp', 'level', 'message') |
328e607d C |
29 | |
30 | let additionalInfos = JSON.stringify(obj, loggerReplacer, 2) | |
fd8710b8 | 31 | |
e20015d7 | 32 | if (additionalInfos === undefined || additionalInfos === '{}') additionalInfos = '' |
2af4fa4d | 33 | else additionalInfos = ' ' + additionalInfos |
23e27dd5 | 34 | |
2af4fa4d | 35 | return `[${info.label}] ${info.timestamp} ${info.level}: ${info.message}${additionalInfos}` |
23e27dd5 C |
36 | }) |
37 | ||
e20015d7 C |
38 | const jsonLoggerFormat = winston.format.printf(info => { |
39 | return JSON.stringify(info, loggerReplacer) | |
276d03ed C |
40 | }) |
41 | ||
23e27dd5 | 42 | const timestampFormatter = winston.format.timestamp({ |
0647f472 | 43 | format: 'YYYY-MM-DD HH:mm:ss.SSS' |
23e27dd5 C |
44 | }) |
45 | const labelFormatter = winston.format.label({ | |
46 | label | |
47 | }) | |
48 | ||
fcf4569f NB |
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 | ||
85b4d9c5 | 64 | const logger = winston.createLogger({ |
23e27dd5 | 65 | level: CONFIG.LOG.LEVEL, |
e20015d7 C |
66 | format: winston.format.combine( |
67 | labelFormatter, | |
68 | winston.format.splat() | |
69 | ), | |
9f10b292 | 70 | transports: [ |
fcf4569f | 71 | new winston.transports.File(fileLoggerOptions), |
9f10b292 | 72 | new winston.transports.Console({ |
e8cb4409 | 73 | handleExceptions: true, |
23e27dd5 C |
74 | format: winston.format.combine( |
75 | timestampFormatter, | |
23e27dd5 | 76 | winston.format.colorize(), |
276d03ed | 77 | consoleLoggerFormat |
23e27dd5 | 78 | ) |
9f10b292 C |
79 | }) |
80 | ], | |
81 | exitOnError: true | |
82 | }) | |
8c308c2b | 83 | |
05e67d62 C |
84 | function bunyanLogFactory (level: string) { |
85 | return function () { | |
86 | let meta = null | |
c1e791ba RK |
87 | let args: any[] = [] |
88 | args.concat(arguments) | |
05e67d62 C |
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 | } | |
9f10b292 | 111 | // --------------------------------------------------------------------------- |
c45f7f84 | 112 | |
23e27dd5 C |
113 | export { |
114 | timestampFormatter, | |
115 | labelFormatter, | |
276d03ed | 116 | consoleLoggerFormat, |
59390818 | 117 | jsonLoggerFormat, |
05e67d62 C |
118 | logger, |
119 | bunyanLogger | |
23e27dd5 | 120 | } |