]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - scripts/parse-log.ts
Translated using Weblate (Croatian)
[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
52208599
C
79async function run () {
80 const files = await getFiles()
41dbdb8a 81
52208599
C
82 for (const file of files) {
83 if (file === 'peertube-audit.log') continue
8ebf2a5d 84
52208599
C
85 await readFile(file)
86 }
87}
afffe988 88
52208599
C
89function readFile (file: string) {
90 console.log('Opening %s.', file)
0647f472 91
52208599 92 const stream = createReadStream(file)
452b3bea 93
52208599
C
94 const rl = createInterface({
95 input: stream
96 })
452b3bea 97
52208599
C
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 }
2b6c5552 105
52208599
C
106 if (options.notTags && containsTags(log.tags, options.notTags)) {
107 return
2b6c5552 108 }
0647f472 109
52208599
C
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())
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
ed76401e
C
145 const d = new Date(timestamp)
146 return d.toLocaleString() + `.${d.getMilliseconds()}`
e20015d7 147}
452b3bea
C
148
149function 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}