1 import { program } from 'commander'
2 import { createReadStream, readdir } from 'fs-extra'
3 import { join } from 'path'
4 import { createInterface } from 'readline'
5 import { format as sqlFormat } from 'sql-formatter'
6 import { inspect } from 'util'
7 import * as winston from 'winston'
8 import { labelFormatter, mtimeSortFilesDesc } from '../server/helpers/logger'
9 import { CONFIG } from '../server/initializers/config'
12 .option('-l, --level [level]', 'Level log (debug/info/warn/error)')
13 .option('-f, --files [file...]', 'Files to parse. If not provided, the script will parse the latest log file from config)')
14 .option('-t, --tags [tags...]', 'Display only lines with these tags')
15 .option('-nt, --not-tags [tags...]', 'Donrt display lines containing these tags')
18 const options = program.opts()
20 const excludedKeys = {
29 function keysExcluder (key, value) {
30 return excludedKeys[key] === true ? undefined : value
33 const loggerFormat = winston.format.printf((info) => {
34 let additionalInfos = JSON.stringify(info, keysExcluder, 2)
35 if (additionalInfos === '{}') additionalInfos = ''
36 else additionalInfos = ' ' + additionalInfos
39 if (CONFIG.LOG.PRETTIFY_SQL) {
40 additionalInfos += '\n' + sqlFormat(info.sql, {
45 additionalInfos += ' - ' + info.sql
49 return `[${info.label}] ${toTimeFormat(info.timestamp)} ${info.level}: ${info.message}${additionalInfos}`
52 const logger = winston.createLogger({
54 new winston.transports.Console({
55 level: options.level || 'debug',
57 format: winston.format.combine(
58 winston.format.splat(),
60 winston.format.colorize(),
69 error: logger.error.bind(logger),
70 warn: logger.warn.bind(logger),
71 info: logger.info.bind(logger),
72 debug: logger.debug.bind(logger)
76 .then(() => process.exit(0))
77 .catch(err => console.error(err))
80 return new Promise<void>(async res => {
81 const files = await getFiles()
83 for (const file of files) {
84 if (file === 'peertube-audit.log') continue
86 console.log('Opening %s.', file)
88 const stream = createReadStream(file)
90 const rl = createInterface({
94 rl.on('line', line => {
96 const log = JSON.parse(line)
97 if (options.tags && !containsTags(log.tags, options.tags)) {
101 if (options.notTags && containsTags(log.tags, options.notTags)) {
105 // Don't know why but loggerFormat does not remove splat key
106 Object.assign(log, { splat: undefined })
108 logLevels[log.level](log)
110 console.error('Cannot parse line.', inspect(line))
115 stream.once('close', () => res())
120 // Thanks: https://stackoverflow.com/a/37014317
121 async function getNewestFile (files: string[], basePath: string) {
122 const sorted = await mtimeSortFilesDesc(files, basePath)
124 return (sorted.length > 0) ? sorted[0].file : ''
127 async function getFiles () {
128 if (options.files) return options.files
130 const logFiles = await readdir(CONFIG.STORAGE.LOG_DIR)
132 const filename = await getNewestFile(logFiles, CONFIG.STORAGE.LOG_DIR)
133 return [ join(CONFIG.STORAGE.LOG_DIR, filename) ]
136 function toTimeFormat (time: string) {
137 const timestamp = Date.parse(time)
139 if (isNaN(timestamp) === true) return 'Unknown date'
141 const d = new Date(timestamp)
142 return d.toLocaleString() + `.${d.getMilliseconds()}`
145 function containsTags (loggerTags: string[], optionsTags: string[]) {
146 if (!loggerTags) return false
148 for (const lt of loggerTags) {
149 for (const ot of optionsTags) {
150 if (lt === ot) return true