]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - scripts/parse-log.ts
Fix about follows page
[github/Chocobozzz/PeerTube.git] / scripts / parse-log.ts
1 import { registerTSPaths } from '../server/helpers/register-ts-paths'
2 registerTSPaths()
3
4 import * as program from 'commander'
5 import { createReadStream, readdir } from 'fs-extra'
6 import { join } from 'path'
7 import { createInterface } from 'readline'
8 import * as winston from 'winston'
9 import { labelFormatter } from '../server/helpers/logger'
10 import { CONFIG } from '../server/initializers/config'
11 import { mtimeSortFilesDesc } from '../shared/core-utils/logs/logs'
12
13 program
14 .option('-l, --level [level]', 'Level log (debug/info/warn/error)')
15 .option('-f, --files [file...]', 'Files to parse. If not provided, the script will parse the latest log file from config)')
16 .parse(process.argv)
17
18 const excludedKeys = {
19 level: true,
20 message: true,
21 splat: true,
22 timestamp: true,
23 label: true
24 }
25 function keysExcluder (key, value) {
26 return excludedKeys[key] === true ? undefined : value
27 }
28
29 const loggerFormat = winston.format.printf((info) => {
30 let additionalInfos = JSON.stringify(info, keysExcluder, 2)
31 if (additionalInfos === '{}') additionalInfos = ''
32 else additionalInfos = ' ' + additionalInfos
33
34 return `[${info.label}] ${toTimeFormat(info.timestamp)} ${info.level}: ${info.message}${additionalInfos}`
35 })
36
37 const logger = winston.createLogger({
38 transports: [
39 new winston.transports.Console({
40 level: program['level'] || 'debug',
41 stderrLevels: [],
42 format: winston.format.combine(
43 winston.format.splat(),
44 labelFormatter(),
45 winston.format.colorize(),
46 loggerFormat
47 )
48 })
49 ],
50 exitOnError: true
51 })
52
53 const logLevels = {
54 error: logger.error.bind(logger),
55 warn: logger.warn.bind(logger),
56 info: logger.info.bind(logger),
57 debug: logger.debug.bind(logger)
58 }
59
60 run()
61 .then(() => process.exit(0))
62 .catch(err => console.error(err))
63
64 function run () {
65 return new Promise(async res => {
66 const files = await getFiles()
67
68 for (const file of files) {
69 console.log('Opening %s.', file)
70
71 const stream = createReadStream(file)
72
73 const rl = createInterface({
74 input: stream
75 })
76
77 rl.on('line', line => {
78 const log = JSON.parse(line)
79 // Don't know why but loggerFormat does not remove splat key
80 Object.assign(log, { splat: undefined })
81
82 logLevels[log.level](log)
83 })
84
85 stream.once('close', () => res())
86 }
87 })
88 }
89
90 // Thanks: https://stackoverflow.com/a/37014317
91 async function getNewestFile (files: string[], basePath: string) {
92 const sorted = await mtimeSortFilesDesc(files, basePath)
93
94 return (sorted.length > 0) ? sorted[0].file : ''
95 }
96
97 async function getFiles () {
98 if (program['files']) return program['files']
99
100 const logFiles = await readdir(CONFIG.STORAGE.LOG_DIR)
101
102 const filename = await getNewestFile(logFiles, CONFIG.STORAGE.LOG_DIR)
103 return [ join(CONFIG.STORAGE.LOG_DIR, filename) ]
104 }
105
106 function toTimeFormat (time: string) {
107 const timestamp = Date.parse(time)
108
109 if (isNaN(timestamp) === true) return 'Unknown date'
110
111 return new Date(timestamp).toISOString()
112 }