]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - scripts/parse-log.ts
Translated using Weblate (German)
[github/Chocobozzz/PeerTube.git] / scripts / parse-log.ts
1 import { program } from 'commander'
2 import { createReadStream, readdir } from 'fs-extra'
3 import { join } from 'path'
4 import { createInterface } from 'readline'
5 import { format as sqlFormat } from 'sql-formatter'
6 import { inspect } from 'util'
7 import * as winston from 'winston'
8 import { labelFormatter, mtimeSortFilesDesc } from '../server/helpers/logger'
9 import { CONFIG } from '../server/initializers/config'
10
11 program
12 .option('-l, --level [level]', 'Level log (debug/info/warn/error)')
13 .option('-f, --files [file...]', 'Files to parse. If not provided, the script will parse the latest log file from config)')
14 .option('-t, --tags [tags...]', 'Display only lines with these tags')
15 .option('-nt, --not-tags [tags...]', 'Donrt display lines containing these tags')
16 .parse(process.argv)
17
18 const options = program.opts()
19
20 const excludedKeys = {
21 level: true,
22 message: true,
23 splat: true,
24 timestamp: true,
25 tags: true,
26 label: true,
27 sql: true
28 }
29 function keysExcluder (key, value) {
30 return excludedKeys[key] === true ? undefined : value
31 }
32
33 const loggerFormat = winston.format.printf((info) => {
34 let additionalInfos = JSON.stringify(info, keysExcluder, 2)
35 if (additionalInfos === '{}') additionalInfos = ''
36 else additionalInfos = ' ' + additionalInfos
37
38 if (info.sql) {
39 if (CONFIG.LOG.PRETTIFY_SQL) {
40 additionalInfos += '\n' + sqlFormat(info.sql, {
41 language: 'sql',
42 tabWidth: 2
43 })
44 } else {
45 additionalInfos += ' - ' + info.sql
46 }
47 }
48
49 return `[${info.label}] ${toTimeFormat(info.timestamp)} ${info.level}: ${info.message}${additionalInfos}`
50 })
51
52 const logger = winston.createLogger({
53 transports: [
54 new winston.transports.Console({
55 level: options.level || 'debug',
56 stderrLevels: [],
57 format: winston.format.combine(
58 winston.format.splat(),
59 labelFormatter(),
60 winston.format.colorize(),
61 loggerFormat
62 )
63 })
64 ],
65 exitOnError: true
66 })
67
68 const logLevels = {
69 error: logger.error.bind(logger),
70 warn: logger.warn.bind(logger),
71 info: logger.info.bind(logger),
72 debug: logger.debug.bind(logger)
73 }
74
75 run()
76 .then(() => process.exit(0))
77 .catch(err => console.error(err))
78
79 async function run () {
80 const files = await getFiles()
81
82 for (const file of files) {
83 if (file === 'peertube-audit.log') continue
84
85 await readFile(file)
86 }
87 }
88
89 function readFile (file: string) {
90 console.log('Opening %s.', file)
91
92 const stream = createReadStream(file)
93
94 const rl = createInterface({
95 input: stream
96 })
97
98 return new Promise<void>(res => {
99 rl.on('line', line => {
100 try {
101 const log = JSON.parse(line)
102 if (options.tags && !containsTags(log.tags, options.tags)) {
103 return
104 }
105
106 if (options.notTags && containsTags(log.tags, options.notTags)) {
107 return
108 }
109
110 // Don't know why but loggerFormat does not remove splat key
111 Object.assign(log, { splat: undefined })
112
113 logLevels[log.level](log)
114 } catch (err) {
115 console.error('Cannot parse line.', inspect(line))
116 throw err
117 }
118 })
119
120 stream.once('close', () => res())
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 const d = new Date(timestamp)
146 return d.toLocaleString() + `.${d.getMilliseconds()}`
147 }
148
149 function containsTags (loggerTags: string[], optionsTags: string[]) {
150 if (!loggerTags) return false
151
152 for (const lt of loggerTags) {
153 for (const ot of optionsTags) {
154 if (lt === ot) return true
155 }
156 }
157
158 return false
159 }