]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - scripts/parse-log.ts
It's not the week-end yet
[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 87 for (const file of files) {
8ebf2a5d
C
88 if (file === 'peertube-audit.log') continue
89
e0783718 90 console.log('Opening %s.', file)
41dbdb8a 91
e0783718 92 const stream = createReadStream(file)
afffe988 93
e0783718
C
94 const rl = createInterface({
95 input: stream
96 })
0647f472 97
e0783718 98 rl.on('line', line => {
2b6c5552
C
99 try {
100 const log = JSON.parse(line)
452b3bea
C
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
2b6c5552
C
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) {
1e743faa 114 console.error('Cannot parse line.', inspect(line))
2b6c5552
C
115 throw err
116 }
e0783718 117 })
0647f472 118
e0783718
C
119 stream.once('close', () => res())
120 }
fd8710b8 121 })
0647f472 122}
337ba64e
C
123
124// Thanks: https://stackoverflow.com/a/37014317
fd8710b8
C
125async function getNewestFile (files: string[], basePath: string) {
126 const sorted = await mtimeSortFilesDesc(files, basePath)
337ba64e 127
f0af38e6 128 return (sorted.length > 0) ? sorted[0].file : ''
fd8710b8
C
129}
130
e0783718 131async function getFiles () {
ba5a8d89 132 if (options.files) return options.files
e0783718
C
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
fd8710b8
C
140function toTimeFormat (time: string) {
141 const timestamp = Date.parse(time)
337ba64e 142
fd8710b8 143 if (isNaN(timestamp) === true) return 'Unknown date'
337ba64e 144
51f636ad 145 return new Date(timestamp).toLocaleString()
e20015d7 146}
452b3bea
C
147
148function 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}