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