]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - scripts/parse-log.ts
Refactor live manager
[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 import { format as sqlFormat } from 'sql-formatter'
14
15 program
16 .option('-l, --level [level]', 'Level log (debug/info/warn/error)')
17 .option('-f, --files [file...]', 'Files to parse. If not provided, the script will parse the latest log file from config)')
18 .option('-t, --tags [tags...]', 'Display only lines with these tags')
19 .option('-nt, --not-tags [tags...]', 'Donrt display lines containing these tags')
20 .parse(process.argv)
21
22 const options = program.opts()
23
24 const excludedKeys = {
25 level: true,
26 message: true,
27 splat: true,
28 timestamp: true,
29 tags: true,
30 label: true,
31 sql: true
32 }
33 function keysExcluder (key, value) {
34 return excludedKeys[key] === true ? undefined : value
35 }
36
37 const loggerFormat = winston.format.printf((info) => {
38 let additionalInfos = JSON.stringify(info, keysExcluder, 2)
39 if (additionalInfos === '{}') additionalInfos = ''
40 else additionalInfos = ' ' + additionalInfos
41
42 if (info.sql) {
43 if (CONFIG.LOG.PRETTIFY_SQL) {
44 additionalInfos += '\n' + sqlFormat(info.sql, {
45 language: 'sql',
46 indent: ' '
47 })
48 } else {
49 additionalInfos += ' - ' + info.sql
50 }
51 }
52
53 return `[${info.label}] ${toTimeFormat(info.timestamp)} ${info.level}: ${info.message}${additionalInfos}`
54 })
55
56 const logger = winston.createLogger({
57 transports: [
58 new winston.transports.Console({
59 level: options.level || 'debug',
60 stderrLevels: [],
61 format: winston.format.combine(
62 winston.format.splat(),
63 labelFormatter(),
64 winston.format.colorize(),
65 loggerFormat
66 )
67 })
68 ],
69 exitOnError: true
70 })
71
72 const logLevels = {
73 error: logger.error.bind(logger),
74 warn: logger.warn.bind(logger),
75 info: logger.info.bind(logger),
76 debug: logger.debug.bind(logger)
77 }
78
79 run()
80 .then(() => process.exit(0))
81 .catch(err => console.error(err))
82
83 function run () {
84 return new Promise<void>(async res => {
85 const files = await getFiles()
86
87 for (const file of files) {
88 if (file === 'peertube-audit.log') continue
89
90 console.log('Opening %s.', file)
91
92 const stream = createReadStream(file)
93
94 const rl = createInterface({
95 input: stream
96 })
97
98 rl.on('line', line => {
99 try {
100 const log = JSON.parse(line)
101 if (options.tags && !containsTags(log.tags, options.tags)) {
102 return
103 }
104
105 if (options.notTags && containsTags(log.tags, options.notTags)) {
106 return
107 }
108
109 // Don't know why but loggerFormat does not remove splat key
110 Object.assign(log, { splat: undefined })
111
112 logLevels[log.level](log)
113 } catch (err) {
114 console.error('Cannot parse line.', inspect(line))
115 throw err
116 }
117 })
118
119 stream.once('close', () => res())
120 }
121 })
122 }
123
124 // Thanks: https://stackoverflow.com/a/37014317
125 async function getNewestFile (files: string[], basePath: string) {
126 const sorted = await mtimeSortFilesDesc(files, basePath)
127
128 return (sorted.length > 0) ? sorted[0].file : ''
129 }
130
131 async function getFiles () {
132 if (options.files) return options.files
133
134 const logFiles = await readdir(CONFIG.STORAGE.LOG_DIR)
135
136 const filename = await getNewestFile(logFiles, CONFIG.STORAGE.LOG_DIR)
137 return [ join(CONFIG.STORAGE.LOG_DIR, filename) ]
138 }
139
140 function toTimeFormat (time: string) {
141 const timestamp = Date.parse(time)
142
143 if (isNaN(timestamp) === true) return 'Unknown date'
144
145 return new Date(timestamp).toLocaleString()
146 }
147
148 function containsTags (loggerTags: string[], optionsTags: string[]) {
149 if (!loggerTags) return false
150
151 for (const lt of loggerTags) {
152 for (const ot of optionsTags) {
153 if (lt === ot) return true
154 }
155 }
156
157 return false
158 }