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