]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - scripts/parse-log.ts
Add /about/instance/contact contact-form route with prefilled queryParams subject...
[github/Chocobozzz/PeerTube.git] / scripts / parse-log.ts
CommitLineData
2aaa1a3f
C
1import { registerTSPaths } from '../server/helpers/register-ts-paths'
2registerTSPaths()
3
3aa5cea8 4import * as program from 'commander'
fd8710b8 5import { createReadStream, readdir } from 'fs-extra'
41dbdb8a
C
6import { join } from 'path'
7import { createInterface } from 'readline'
8import * as winston from 'winston'
94a5ff8a 9import { labelFormatter } from '../server/helpers/logger'
74dc3bca 10import { CONFIG } from '../server/initializers/config'
cda03765 11import { mtimeSortFilesDesc } from '../shared/core-utils/logs/logs'
1e743faa 12import { inspect } from 'util'
cb5c2abc 13import { format as sqlFormat } from 'sql-formatter'
41dbdb8a 14
3aa5cea8
C
15program
16 .option('-l, --level [level]', 'Level log (debug/info/warn/error)')
e0783718 17 .option('-f, --files [file...]', 'Files to parse. If not provided, the script will parse the latest log file from config)')
452b3bea
C
18 .option('-t, --tags [tags...]', 'Display only lines with these tags')
19 .option('-nt, --not-tags [tags...]', 'Donrt display lines containing these tags')
3aa5cea8
C
20 .parse(process.argv)
21
ba5a8d89
C
22const options = program.opts()
23
94a5ff8a
C
24const excludedKeys = {
25 level: true,
26 message: true,
27 splat: true,
28 timestamp: true,
452b3bea 29 tags: true,
cb5c2abc
C
30 label: true,
31 sql: true
94a5ff8a
C
32}
33function keysExcluder (key, value) {
34 return excludedKeys[key] === true ? undefined : value
35}
36
37const loggerFormat = winston.format.printf((info) => {
38 let additionalInfos = JSON.stringify(info, keysExcluder, 2)
39 if (additionalInfos === '{}') additionalInfos = ''
40 else additionalInfos = ' ' + additionalInfos
41
cb5c2abc
C
42 if (info.sql) {
43 if (CONFIG.LOG.PRETTIFY_SQL) {
44 additionalInfos += '\n' + sqlFormat(info.sql, {
45 language: 'sql',
ba5a8d89 46 indent: ' '
cb5c2abc
C
47 })
48 } else {
49 additionalInfos += ' - ' + info.sql
50 }
51 }
52
0647f472 53 return `[${info.label}] ${toTimeFormat(info.timestamp)} ${info.level}: ${info.message}${additionalInfos}`
94a5ff8a
C
54})
55
85b4d9c5 56const logger = winston.createLogger({
41dbdb8a
C
57 transports: [
58 new winston.transports.Console({
ba5a8d89 59 level: options.level || 'debug',
23e27dd5
C
60 stderrLevels: [],
61 format: winston.format.combine(
23e27dd5 62 winston.format.splat(),
1b05d82d 63 labelFormatter(),
23e27dd5
C
64 winston.format.colorize(),
65 loggerFormat
66 )
41dbdb8a
C
67 })
68 ],
69 exitOnError: true
70})
71
72const logLevels = {
23e27dd5
C
73 error: logger.error.bind(logger),
74 warn: logger.warn.bind(logger),
75 info: logger.info.bind(logger),
76 debug: logger.debug.bind(logger)
41dbdb8a
C
77}
78
fd8710b8
C
79run()
80 .then(() => process.exit(0))
81 .catch(err => console.error(err))
85b4d9c5 82
fd8710b8 83function run () {
ba5a8d89 84 return new Promise<void>(async res => {
e0783718 85 const files = await getFiles()
41dbdb8a 86
e0783718
C
87 for (const file of files) {
88 console.log('Opening %s.', file)
41dbdb8a 89
e0783718 90 const stream = createReadStream(file)
afffe988 91
e0783718
C
92 const rl = createInterface({
93 input: stream
94 })
0647f472 95
e0783718 96 rl.on('line', line => {
2b6c5552
C
97 try {
98 const log = JSON.parse(line)
452b3bea
C
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
2b6c5552
C
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) {
1e743faa 112 console.error('Cannot parse line.', inspect(line))
2b6c5552
C
113 throw err
114 }
e0783718 115 })
0647f472 116
e0783718
C
117 stream.once('close', () => res())
118 }
fd8710b8 119 })
0647f472 120}
337ba64e
C
121
122// Thanks: https://stackoverflow.com/a/37014317
fd8710b8
C
123async function getNewestFile (files: string[], basePath: string) {
124 const sorted = await mtimeSortFilesDesc(files, basePath)
337ba64e 125
f0af38e6 126 return (sorted.length > 0) ? sorted[0].file : ''
fd8710b8
C
127}
128
e0783718 129async function getFiles () {
ba5a8d89 130 if (options.files) return options.files
e0783718
C
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
fd8710b8
C
138function toTimeFormat (time: string) {
139 const timestamp = Date.parse(time)
337ba64e 140
fd8710b8 141 if (isNaN(timestamp) === true) return 'Unknown date'
337ba64e 142
51f636ad 143 return new Date(timestamp).toLocaleString()
e20015d7 144}
452b3bea
C
145
146function 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}