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