]>
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 | |
959dbbd7 C |
15 | function getLoggerReplacer () { |
16 | const seen = new WeakSet() | |
e20015d7 | 17 | |
959dbbd7 C |
18 | // Thanks: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Cyclic_object_value#Examples |
19 | return (key: string, value: any) => { | |
20 | if (typeof value === 'object' && value !== null) { | |
21 | if (seen.has(value)) return | |
d5b7d911 | 22 | |
959dbbd7 C |
23 | seen.add(value) |
24 | } | |
25 | ||
26 | if (value instanceof Error) { | |
27 | const error = {} | |
28 | ||
29 | Object.getOwnPropertyNames(value).forEach(key => error[ key ] = value[ key ]) | |
d5b7d911 | 30 | |
959dbbd7 C |
31 | return error |
32 | } | |
33 | ||
34 | return value | |
35 | } | |
23e27dd5 C |
36 | } |
37 | ||
276d03ed | 38 | const consoleLoggerFormat = winston.format.printf(info => { |
fd8710b8 | 39 | const obj = omit(info, 'label', 'timestamp', 'level', 'message') |
328e607d | 40 | |
959dbbd7 | 41 | let additionalInfos = JSON.stringify(obj, getLoggerReplacer(), 2) |
fd8710b8 | 42 | |
e20015d7 | 43 | if (additionalInfos === undefined || additionalInfos === '{}') additionalInfos = '' |
2af4fa4d | 44 | else additionalInfos = ' ' + additionalInfos |
23e27dd5 | 45 | |
2af4fa4d | 46 | return `[${info.label}] ${info.timestamp} ${info.level}: ${info.message}${additionalInfos}` |
23e27dd5 C |
47 | }) |
48 | ||
e20015d7 | 49 | const jsonLoggerFormat = winston.format.printf(info => { |
959dbbd7 | 50 | return JSON.stringify(info, getLoggerReplacer()) |
276d03ed C |
51 | }) |
52 | ||
23e27dd5 | 53 | const timestampFormatter = winston.format.timestamp({ |
0647f472 | 54 | format: 'YYYY-MM-DD HH:mm:ss.SSS' |
23e27dd5 C |
55 | }) |
56 | const labelFormatter = winston.format.label({ | |
57 | label | |
58 | }) | |
59 | ||
fcf4569f | 60 | const fileLoggerOptions: FileTransportOptions = { |
fcf4569f NB |
61 | filename: path.join(CONFIG.STORAGE.LOG_DIR, 'peertube.log'), |
62 | handleExceptions: true, | |
63 | format: winston.format.combine( | |
64 | winston.format.timestamp(), | |
65 | jsonLoggerFormat | |
66 | ) | |
67 | } | |
68 | ||
69 | if (CONFIG.LOG.ROTATION) { | |
70 | fileLoggerOptions.maxsize = 1024 * 1024 * 12 | |
71 | fileLoggerOptions.maxFiles = 20 | |
72 | } | |
73 | ||
85b4d9c5 | 74 | const logger = winston.createLogger({ |
23e27dd5 | 75 | level: CONFIG.LOG.LEVEL, |
e20015d7 C |
76 | format: winston.format.combine( |
77 | labelFormatter, | |
78 | winston.format.splat() | |
79 | ), | |
9f10b292 | 80 | transports: [ |
fcf4569f | 81 | new winston.transports.File(fileLoggerOptions), |
9f10b292 | 82 | new winston.transports.Console({ |
e8cb4409 | 83 | handleExceptions: true, |
23e27dd5 C |
84 | format: winston.format.combine( |
85 | timestampFormatter, | |
23e27dd5 | 86 | winston.format.colorize(), |
276d03ed | 87 | consoleLoggerFormat |
23e27dd5 | 88 | ) |
9f10b292 C |
89 | }) |
90 | ], | |
91 | exitOnError: true | |
92 | }) | |
8c308c2b | 93 | |
05e67d62 C |
94 | function bunyanLogFactory (level: string) { |
95 | return function () { | |
96 | let meta = null | |
c1e791ba RK |
97 | let args: any[] = [] |
98 | args.concat(arguments) | |
05e67d62 C |
99 | |
100 | if (arguments[ 0 ] instanceof Error) { | |
101 | meta = arguments[ 0 ].toString() | |
102 | args = Array.prototype.slice.call(arguments, 1) | |
103 | args.push(meta) | |
104 | } else if (typeof (args[ 0 ]) !== 'string') { | |
105 | meta = arguments[ 0 ] | |
106 | args = Array.prototype.slice.call(arguments, 1) | |
107 | args.push(meta) | |
108 | } | |
109 | ||
110 | logger[ level ].apply(logger, args) | |
111 | } | |
112 | } | |
113 | const bunyanLogger = { | |
114 | trace: bunyanLogFactory('debug'), | |
115 | debug: bunyanLogFactory('debug'), | |
116 | info: bunyanLogFactory('info'), | |
117 | warn: bunyanLogFactory('warn'), | |
118 | error: bunyanLogFactory('error'), | |
119 | fatal: bunyanLogFactory('error') | |
120 | } | |
9f10b292 | 121 | // --------------------------------------------------------------------------- |
c45f7f84 | 122 | |
23e27dd5 C |
123 | export { |
124 | timestampFormatter, | |
125 | labelFormatter, | |
276d03ed | 126 | consoleLoggerFormat, |
59390818 | 127 | jsonLoggerFormat, |
05e67d62 C |
128 | logger, |
129 | bunyanLogger | |
23e27dd5 | 130 | } |