]>
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' | |
6dd9de95 | 5 | import { CONFIG } from '../initializers/config' |
fd8710b8 | 6 | import { omit } from 'lodash' |
8c308c2b | 7 | |
65fcc311 | 8 | const label = CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT |
320d6275 C |
9 | |
10 | // Create the directory if it does not exist | |
fd8710b8 | 11 | // FIXME: use async |
c9d5c64f | 12 | mkdirpSync(CONFIG.STORAGE.LOG_DIR) |
320d6275 | 13 | |
e20015d7 C |
14 | function loggerReplacer (key: string, value: any) { |
15 | if (value instanceof Error) { | |
16 | const error = {} | |
17 | ||
18 | Object.getOwnPropertyNames(value).forEach(key => error[ key ] = value[ key ]) | |
d5b7d911 | 19 | |
e20015d7 C |
20 | return error |
21 | } | |
d5b7d911 C |
22 | |
23 | return value | |
23e27dd5 C |
24 | } |
25 | ||
276d03ed | 26 | const consoleLoggerFormat = winston.format.printf(info => { |
fd8710b8 | 27 | const obj = omit(info, 'label', 'timestamp', 'level', 'message') |
328e607d C |
28 | |
29 | let additionalInfos = JSON.stringify(obj, loggerReplacer, 2) | |
fd8710b8 | 30 | |
e20015d7 | 31 | if (additionalInfos === undefined || additionalInfos === '{}') additionalInfos = '' |
2af4fa4d | 32 | else additionalInfos = ' ' + additionalInfos |
23e27dd5 | 33 | |
2af4fa4d | 34 | return `[${info.label}] ${info.timestamp} ${info.level}: ${info.message}${additionalInfos}` |
23e27dd5 C |
35 | }) |
36 | ||
e20015d7 C |
37 | const jsonLoggerFormat = winston.format.printf(info => { |
38 | return JSON.stringify(info, loggerReplacer) | |
276d03ed C |
39 | }) |
40 | ||
23e27dd5 | 41 | const timestampFormatter = winston.format.timestamp({ |
0647f472 | 42 | format: 'YYYY-MM-DD HH:mm:ss.SSS' |
23e27dd5 C |
43 | }) |
44 | const labelFormatter = winston.format.label({ | |
45 | label | |
46 | }) | |
47 | ||
85b4d9c5 | 48 | const logger = winston.createLogger({ |
23e27dd5 | 49 | level: CONFIG.LOG.LEVEL, |
e20015d7 C |
50 | format: winston.format.combine( |
51 | labelFormatter, | |
52 | winston.format.splat() | |
53 | ), | |
9f10b292 C |
54 | transports: [ |
55 | new winston.transports.File({ | |
23e27dd5 | 56 | filename: path.join(CONFIG.STORAGE.LOG_DIR, 'peertube.log'), |
9f10b292 | 57 | handleExceptions: true, |
7361c401 | 58 | maxsize: 1024 * 1024 * 12, |
fd8710b8 | 59 | maxFiles: 20, |
23e27dd5 | 60 | format: winston.format.combine( |
0647f472 | 61 | winston.format.timestamp(), |
276d03ed | 62 | jsonLoggerFormat |
23e27dd5 | 63 | ) |
9f10b292 C |
64 | }), |
65 | new winston.transports.Console({ | |
e8cb4409 | 66 | handleExceptions: true, |
23e27dd5 C |
67 | format: winston.format.combine( |
68 | timestampFormatter, | |
23e27dd5 | 69 | winston.format.colorize(), |
276d03ed | 70 | consoleLoggerFormat |
23e27dd5 | 71 | ) |
9f10b292 C |
72 | }) |
73 | ], | |
74 | exitOnError: true | |
75 | }) | |
8c308c2b | 76 | |
05e67d62 C |
77 | function bunyanLogFactory (level: string) { |
78 | return function () { | |
79 | let meta = null | |
c1e791ba RK |
80 | let args: any[] = [] |
81 | args.concat(arguments) | |
05e67d62 C |
82 | |
83 | if (arguments[ 0 ] instanceof Error) { | |
84 | meta = arguments[ 0 ].toString() | |
85 | args = Array.prototype.slice.call(arguments, 1) | |
86 | args.push(meta) | |
87 | } else if (typeof (args[ 0 ]) !== 'string') { | |
88 | meta = arguments[ 0 ] | |
89 | args = Array.prototype.slice.call(arguments, 1) | |
90 | args.push(meta) | |
91 | } | |
92 | ||
93 | logger[ level ].apply(logger, args) | |
94 | } | |
95 | } | |
96 | const bunyanLogger = { | |
97 | trace: bunyanLogFactory('debug'), | |
98 | debug: bunyanLogFactory('debug'), | |
99 | info: bunyanLogFactory('info'), | |
100 | warn: bunyanLogFactory('warn'), | |
101 | error: bunyanLogFactory('error'), | |
102 | fatal: bunyanLogFactory('error') | |
103 | } | |
9f10b292 | 104 | // --------------------------------------------------------------------------- |
c45f7f84 | 105 | |
23e27dd5 C |
106 | export { |
107 | timestampFormatter, | |
108 | labelFormatter, | |
276d03ed | 109 | consoleLoggerFormat, |
59390818 | 110 | jsonLoggerFormat, |
05e67d62 C |
111 | logger, |
112 | bunyanLogger | |
23e27dd5 | 113 | } |