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