]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - scripts/parse-log.ts
Merge branch 'release/3.1.0' into develop
[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 console.log('Opening %s.', file)
89
90 const stream = createReadStream(file)
91
92 const rl = createInterface({
93 input: stream
94 })
95
96 rl.on('line', line => {
97 try {
98 const log = JSON.parse(line)
99 if (options.tags && !containsTags(log.tags, options.tags)) {
100 return
101 }
102
103 if (options.notTags && containsTags(log.tags, options.notTags)) {
104 return
105 }
106
107 // Don't know why but loggerFormat does not remove splat key
108 Object.assign(log, { splat: undefined })
109
110 logLevels[log.level](log)
111 } catch (err) {
112 console.error('Cannot parse line.', inspect(line))
113 throw err
114 }
115 })
116
117 stream.once('close', () => res())
118 }
119 })
120 }
121
122 // Thanks: https://stackoverflow.com/a/37014317
123 async function getNewestFile (files: string[], basePath: string) {
124 const sorted = await mtimeSortFilesDesc(files, basePath)
125
126 return (sorted.length > 0) ? sorted[0].file : ''
127 }
128
129 async function getFiles () {
130 if (options.files) return options.files
131
132 const logFiles = await readdir(CONFIG.STORAGE.LOG_DIR)
133
134 const filename = await getNewestFile(logFiles, CONFIG.STORAGE.LOG_DIR)
135 return [ join(CONFIG.STORAGE.LOG_DIR, filename) ]
136 }
137
138 function toTimeFormat (time: string) {
139 const timestamp = Date.parse(time)
140
141 if (isNaN(timestamp) === true) return 'Unknown date'
142
143 return new Date(timestamp).toISOString()
144 }
145
146 function containsTags (loggerTags: string[], optionsTags: string[]) {
147 if (!loggerTags) return false
148
149 for (const lt of loggerTags) {
150 for (const ot of optionsTags) {
151 if (lt === ot) return true
152 }
153 }
154
155 return false
156 }