]>
Commit | Line | Data |
---|---|---|
41dbdb8a C |
1 | import { createReadStream } from 'fs' |
2 | import { join } from 'path' | |
3 | import { createInterface } from 'readline' | |
4 | import * as winston from 'winston' | |
94a5ff8a | 5 | import { labelFormatter } from '../server/helpers/logger' |
41dbdb8a C |
6 | import { CONFIG } from '../server/initializers/constants' |
7 | ||
94a5ff8a C |
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}] ${new Date(info.timestamp).toISOString()} ${info.level}: ${info.message}${additionalInfos}` | |
25 | }) | |
26 | ||
23e27dd5 | 27 | const logger = new winston.createLogger({ |
41dbdb8a C |
28 | transports: [ |
29 | new winston.transports.Console({ | |
30 | level: 'debug', | |
23e27dd5 C |
31 | stderrLevels: [], |
32 | format: winston.format.combine( | |
23e27dd5 C |
33 | winston.format.splat(), |
34 | labelFormatter, | |
35 | winston.format.colorize(), | |
36 | loggerFormat | |
37 | ) | |
41dbdb8a C |
38 | }) |
39 | ], | |
40 | exitOnError: true | |
41 | }) | |
42 | ||
43 | const logLevels = { | |
23e27dd5 C |
44 | error: logger.error.bind(logger), |
45 | warn: logger.warn.bind(logger), | |
46 | info: logger.info.bind(logger), | |
47 | debug: logger.debug.bind(logger) | |
41dbdb8a C |
48 | } |
49 | ||
23e27dd5 | 50 | const path = join(CONFIG.STORAGE.LOG_DIR, 'peertube.log') |
41dbdb8a C |
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) | |
23e27dd5 C |
59 | // Don't know why but loggerFormat does not remove splat key |
60 | Object.assign(log, { splat: undefined }) | |
afffe988 | 61 | |
23e27dd5 | 62 | logLevels[log.level](log) |
41dbdb8a | 63 | }) |