PeerTube/scripts/parse-log.ts

134 lines
3.5 KiB
TypeScript
Raw Normal View History

import { registerTSPaths } from '../server/helpers/register-ts-paths'
registerTSPaths()
2018-06-19 10:45:33 +02:00
import * as program from 'commander'
2019-04-10 15:26:33 +02:00
import { createReadStream, readdir } from 'fs-extra'
2017-11-16 17:29:05 +01:00
import { join } from 'path'
import { createInterface } from 'readline'
import * as winston from 'winston'
import { labelFormatter } from '../server/helpers/logger'
import { CONFIG } from '../server/initializers/config'
2019-04-11 17:33:36 +02:00
import { mtimeSortFilesDesc } from '../shared/core-utils/logs/logs'
import { inspect } from 'util'
2021-02-01 15:03:32 +01:00
import { format as sqlFormat } from 'sql-formatter'
2017-11-16 17:29:05 +01:00
2018-06-19 10:45:33 +02:00
program
.option('-l, --level [level]', 'Level log (debug/info/warn/error)')
.option('-f, --files [file...]', 'Files to parse. If not provided, the script will parse the latest log file from config)')
2018-06-19 10:45:33 +02:00
.parse(process.argv)
2021-02-03 09:33:05 +01:00
const options = program.opts()
const excludedKeys = {
level: true,
message: true,
splat: true,
timestamp: true,
2021-02-01 15:03:32 +01:00
label: true,
sql: true
}
function keysExcluder (key, value) {
return excludedKeys[key] === true ? undefined : value
}
const loggerFormat = winston.format.printf((info) => {
let additionalInfos = JSON.stringify(info, keysExcluder, 2)
if (additionalInfos === '{}') additionalInfos = ''
else additionalInfos = ' ' + additionalInfos
2021-02-01 15:03:32 +01:00
if (info.sql) {
if (CONFIG.LOG.PRETTIFY_SQL) {
additionalInfos += '\n' + sqlFormat(info.sql, {
language: 'sql',
2021-02-03 09:33:05 +01:00
indent: ' '
2021-02-01 15:03:32 +01:00
})
} else {
additionalInfos += ' - ' + info.sql
}
}
2018-03-08 18:16:15 +01:00
return `[${info.label}] ${toTimeFormat(info.timestamp)} ${info.level}: ${info.message}${additionalInfos}`
})
2018-06-26 17:52:14 +02:00
const logger = winston.createLogger({
2017-11-16 17:29:05 +01:00
transports: [
new winston.transports.Console({
2021-02-03 09:33:05 +01:00
level: options.level || 'debug',
2018-01-19 13:58:13 +01:00
stderrLevels: [],
format: winston.format.combine(
winston.format.splat(),
2020-04-09 11:00:30 +02:00
labelFormatter(),
2018-01-19 13:58:13 +01:00
winston.format.colorize(),
loggerFormat
)
2017-11-16 17:29:05 +01:00
})
],
exitOnError: true
})
const logLevels = {
2018-01-19 13:58:13 +01:00
error: logger.error.bind(logger),
warn: logger.warn.bind(logger),
info: logger.info.bind(logger),
debug: logger.debug.bind(logger)
2017-11-16 17:29:05 +01:00
}
2019-04-10 15:26:33 +02:00
run()
.then(() => process.exit(0))
.catch(err => console.error(err))
2018-06-26 17:52:14 +02:00
2019-04-10 15:26:33 +02:00
function run () {
2021-02-03 09:33:05 +01:00
return new Promise<void>(async res => {
const files = await getFiles()
2017-11-16 17:29:05 +01:00
for (const file of files) {
console.log('Opening %s.', file)
2017-11-16 17:29:05 +01:00
const stream = createReadStream(file)
2017-11-17 11:35:10 +01:00
const rl = createInterface({
input: stream
})
2018-03-08 18:16:15 +01:00
rl.on('line', line => {
2020-12-22 10:15:06 +01:00
try {
const log = JSON.parse(line)
// Don't know why but loggerFormat does not remove splat key
Object.assign(log, { splat: undefined })
logLevels[log.level](log)
} catch (err) {
console.error('Cannot parse line.', inspect(line))
2020-12-22 10:15:06 +01:00
throw err
}
})
2018-03-08 18:16:15 +01:00
stream.once('close', () => res())
}
2019-04-10 15:26:33 +02:00
})
2018-03-08 18:16:15 +01:00
}
// Thanks: https://stackoverflow.com/a/37014317
2019-04-10 15:26:33 +02:00
async function getNewestFile (files: string[], basePath: string) {
const sorted = await mtimeSortFilesDesc(files, basePath)
2020-07-30 14:54:31 +02:00
return (sorted.length > 0) ? sorted[0].file : ''
2019-04-10 15:26:33 +02:00
}
async function getFiles () {
2021-02-03 09:33:05 +01:00
if (options.files) return options.files
const logFiles = await readdir(CONFIG.STORAGE.LOG_DIR)
const filename = await getNewestFile(logFiles, CONFIG.STORAGE.LOG_DIR)
return [ join(CONFIG.STORAGE.LOG_DIR, filename) ]
}
2019-04-10 15:26:33 +02:00
function toTimeFormat (time: string) {
const timestamp = Date.parse(time)
2019-04-10 15:26:33 +02:00
if (isNaN(timestamp) === true) return 'Unknown date'
2019-04-10 15:26:33 +02:00
return new Date(timestamp).toISOString()
2018-07-30 10:59:31 +02:00
}