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