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