PeerTube/scripts/parse-log.ts

77 lines
1.9 KiB
TypeScript
Raw Normal View History

2018-06-19 03:45:33 -05:00
import * as program from 'commander'
2017-11-16 10:29:05 -06:00
import { createReadStream } from 'fs'
import { join } from 'path'
import { createInterface } from 'readline'
import * as winston from 'winston'
import { labelFormatter } from '../server/helpers/logger'
2017-11-16 10:29:05 -06:00
import { CONFIG } from '../server/initializers/constants'
2018-06-19 03:45:33 -05: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 11:16:15 -06:00
return `[${info.label}] ${toTimeFormat(info.timestamp)} ${info.level}: ${info.message}${additionalInfos}`
})
2018-01-19 06:58:13 -06:00
const logger = new winston.createLogger({
2017-11-16 10:29:05 -06:00
transports: [
new winston.transports.Console({
2018-06-19 03:45:33 -05:00
level: program['level'] || 'debug',
2018-01-19 06:58:13 -06:00
stderrLevels: [],
format: winston.format.combine(
winston.format.splat(),
labelFormatter,
winston.format.colorize(),
loggerFormat
)
2017-11-16 10:29:05 -06:00
})
],
exitOnError: true
})
const logLevels = {
2018-01-19 06:58:13 -06:00
error: logger.error.bind(logger),
warn: logger.warn.bind(logger),
info: logger.info.bind(logger),
debug: logger.debug.bind(logger)
2017-11-16 10:29:05 -06:00
}
2018-01-19 06:58:13 -06:00
const path = join(CONFIG.STORAGE.LOG_DIR, 'peertube.log')
2017-11-16 10:29:05 -06:00
console.log('Opening %s.', path)
const rl = createInterface({
input: createReadStream(path)
})
rl.on('line', line => {
const log = JSON.parse(line)
2018-01-19 06:58:13 -06:00
// Don't know why but loggerFormat does not remove splat key
Object.assign(log, { splat: undefined })
2017-11-17 04:35:10 -06:00
2018-01-19 06:58:13 -06:00
logLevels[log.level](log)
2017-11-16 10:29:05 -06:00
})
2018-03-08 11:16:15 -06:00
function toTimeFormat (time: string) {
const timestamp = Date.parse(time)
if (isNaN(timestamp) === true) return 'Unknown date'
return new Date(timestamp).toISOString()
}