1
0
Fork 0
peertube/scripts/parse-log.ts

103 lines
2.7 KiB
TypeScript
Raw Normal View History

import { registerTSPaths } from '../server/helpers/register-ts-paths'
registerTSPaths()
2018-06-19 04:45:33 -04:00
import * as program from 'commander'
2019-04-10 09:26:33 -04:00
import { createReadStream, readdir } from 'fs-extra'
2017-11-16 11:29:05 -05: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 11:33:36 -04:00
import { mtimeSortFilesDesc } from '../shared/core-utils/logs/logs'
2017-11-16 11:29:05 -05:00
2018-06-19 04:45:33 -04:00
program
.option('-l, --level [level]', 'Level log (debug/info/warn/error)')
.parse(process.argv)
const excludedKeys = {
level: true,
message: true,
splat: true,
timestamp: true,
label: 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
2018-03-08 12:16:15 -05:00
return `[${info.label}] ${toTimeFormat(info.timestamp)} ${info.level}: ${info.message}${additionalInfos}`
})
2018-06-26 11:52:14 -04:00
const logger = winston.createLogger({
2017-11-16 11:29:05 -05:00
transports: [
new winston.transports.Console({
2018-06-19 04:45:33 -04:00
level: program['level'] || 'debug',
2018-01-19 07:58:13 -05:00
stderrLevels: [],
format: winston.format.combine(
winston.format.splat(),
labelFormatter,
winston.format.colorize(),
loggerFormat
)
2017-11-16 11:29:05 -05:00
})
],
exitOnError: true
})
const logLevels = {
2018-01-19 07:58:13 -05:00
error: logger.error.bind(logger),
warn: logger.warn.bind(logger),
info: logger.info.bind(logger),
debug: logger.debug.bind(logger)
2017-11-16 11:29:05 -05:00
}
2019-04-10 09:26:33 -04:00
run()
.then(() => process.exit(0))
.catch(err => console.error(err))
2018-06-26 11:52:14 -04:00
2019-04-10 09:26:33 -04:00
function run () {
return new Promise(async res => {
const logFiles = await readdir(CONFIG.STORAGE.LOG_DIR)
const lastLogFile = await getNewestFile(logFiles, CONFIG.STORAGE.LOG_DIR)
2017-11-16 11:29:05 -05:00
2019-04-10 09:26:33 -04:00
const path = join(CONFIG.STORAGE.LOG_DIR, lastLogFile)
console.log('Opening %s.', path)
2017-11-16 11:29:05 -05:00
2019-04-10 09:26:33 -04:00
const stream = createReadStream(path)
2017-11-17 05:35:10 -05:00
2019-04-10 09:26:33 -04:00
const rl = createInterface({
input: stream
})
2018-03-08 12:16:15 -05:00
2019-04-10 09:26:33 -04:00
rl.on('line', line => {
const log = JSON.parse(line)
// Don't know why but loggerFormat does not remove splat key
Object.assign(log, { splat: undefined })
2018-03-08 12:16:15 -05:00
2019-04-10 09:26:33 -04:00
logLevels[ log.level ](log)
})
2018-03-08 12:16:15 -05:00
2019-04-10 09:26:33 -04:00
stream.once('close', () => res())
})
2018-03-08 12:16:15 -05:00
}
// Thanks: https://stackoverflow.com/a/37014317
2019-04-10 09:26:33 -04:00
async function getNewestFile (files: string[], basePath: string) {
const sorted = await mtimeSortFilesDesc(files, basePath)
2019-04-10 09:26:33 -04:00
return (sorted.length > 0) ? sorted[ 0 ].file : ''
}
function toTimeFormat (time: string) {
const timestamp = Date.parse(time)
2019-04-10 09:26:33 -04:00
if (isNaN(timestamp) === true) return 'Unknown date'
2019-04-10 09:26:33 -04:00
return new Date(timestamp).toISOString()
2018-07-30 04:59:31 -04:00
}