]>
Commit | Line | Data |
---|---|---|
3aa5cea8 | 1 | import * as program from 'commander' |
41dbdb8a C |
2 | import { createReadStream } from 'fs' |
3 | import { join } from 'path' | |
4 | import { createInterface } from 'readline' | |
5 | import * as winston from 'winston' | |
94a5ff8a | 6 | import { labelFormatter } from '../server/helpers/logger' |
41dbdb8a C |
7 | import { CONFIG } from '../server/initializers/constants' |
8 | ||
3aa5cea8 C |
9 | program |
10 | .option('-l, --level [level]', 'Level log (debug/info/warn/error)') | |
11 | .parse(process.argv) | |
12 | ||
94a5ff8a C |
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 | ||
24 | const loggerFormat = winston.format.printf((info) => { | |
25 | let additionalInfos = JSON.stringify(info, keysExcluder, 2) | |
26 | if (additionalInfos === '{}') additionalInfos = '' | |
27 | else additionalInfos = ' ' + additionalInfos | |
28 | ||
0647f472 | 29 | return `[${info.label}] ${toTimeFormat(info.timestamp)} ${info.level}: ${info.message}${additionalInfos}` |
94a5ff8a C |
30 | }) |
31 | ||
23e27dd5 | 32 | const logger = new winston.createLogger({ |
41dbdb8a C |
33 | transports: [ |
34 | new winston.transports.Console({ | |
3aa5cea8 | 35 | level: program['level'] || 'debug', |
23e27dd5 C |
36 | stderrLevels: [], |
37 | format: winston.format.combine( | |
23e27dd5 C |
38 | winston.format.splat(), |
39 | labelFormatter, | |
40 | winston.format.colorize(), | |
41 | loggerFormat | |
42 | ) | |
41dbdb8a C |
43 | }) |
44 | ], | |
45 | exitOnError: true | |
46 | }) | |
47 | ||
48 | const logLevels = { | |
23e27dd5 C |
49 | error: logger.error.bind(logger), |
50 | warn: logger.warn.bind(logger), | |
51 | info: logger.info.bind(logger), | |
52 | debug: logger.debug.bind(logger) | |
41dbdb8a C |
53 | } |
54 | ||
23e27dd5 | 55 | const path = join(CONFIG.STORAGE.LOG_DIR, 'peertube.log') |
41dbdb8a C |
56 | console.log('Opening %s.', path) |
57 | ||
58 | const rl = createInterface({ | |
59 | input: createReadStream(path) | |
60 | }) | |
61 | ||
62 | rl.on('line', line => { | |
63 | const log = JSON.parse(line) | |
23e27dd5 C |
64 | // Don't know why but loggerFormat does not remove splat key |
65 | Object.assign(log, { splat: undefined }) | |
afffe988 | 66 | |
23e27dd5 | 67 | logLevels[log.level](log) |
41dbdb8a | 68 | }) |
0647f472 C |
69 | |
70 | function toTimeFormat (time: string) { | |
71 | const timestamp = Date.parse(time) | |
72 | ||
73 | if (isNaN(timestamp) === true) return 'Unknown date' | |
74 | ||
75 | return new Date(timestamp).toISOString() | |
76 | } |