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