]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - scripts/parse-log.ts
Update shebang and date to POSIX format for working on FreeBSD (not
[github/Chocobozzz/PeerTube.git] / scripts / parse-log.ts
CommitLineData
41dbdb8a
C
1import { createReadStream } from 'fs'
2import { join } from 'path'
3import { createInterface } from 'readline'
4import * as winston from 'winston'
94a5ff8a 5import { labelFormatter } from '../server/helpers/logger'
41dbdb8a
C
6import { CONFIG } from '../server/initializers/constants'
7
94a5ff8a
C
8const excludedKeys = {
9 level: true,
10 message: true,
11 splat: true,
12 timestamp: true,
13 label: true
14}
15function keysExcluder (key, value) {
16 return excludedKeys[key] === true ? undefined : value
17}
18
19const loggerFormat = winston.format.printf((info) => {
20 let additionalInfos = JSON.stringify(info, keysExcluder, 2)
21 if (additionalInfos === '{}') additionalInfos = ''
22 else additionalInfos = ' ' + additionalInfos
23
0647f472 24 return `[${info.label}] ${toTimeFormat(info.timestamp)} ${info.level}: ${info.message}${additionalInfos}`
94a5ff8a
C
25})
26
23e27dd5 27const 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
43const 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 50const path = join(CONFIG.STORAGE.LOG_DIR, 'peertube.log')
41dbdb8a
C
51console.log('Opening %s.', path)
52
53const rl = createInterface({
54 input: createReadStream(path)
55})
56
57rl.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})
0647f472
C
64
65function 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}