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