1 import { registerTSPaths } from '../server/helpers/register-ts-paths'
4 import { program } from 'commander'
5 import { createReadStream, readdir } from 'fs-extra'
6 import { join } from 'path'
7 import { createInterface } from 'readline'
8 import * as winston from 'winston'
9 import { labelFormatter, mtimeSortFilesDesc } from '../server/helpers/logger'
10 import { CONFIG } from '../server/initializers/config'
11 import { inspect } from 'util'
12 import { format as sqlFormat } from 'sql-formatter'
15 .option('-l, --level [level]', 'Level log (debug/info/warn/error)')
16 .option('-f, --files [file...]', 'Files to parse. If not provided, the script will parse the latest log file from config)')
17 .option('-t, --tags [tags...]', 'Display only lines with these tags')
18 .option('-nt, --not-tags [tags...]', 'Donrt display lines containing these tags')
21 const options = program.opts()
23 const excludedKeys = {
32 function keysExcluder (key, value) {
33 return excludedKeys[key] === true ? undefined : value
36 const loggerFormat = winston.format.printf((info) => {
37 let additionalInfos = JSON.stringify(info, keysExcluder, 2)
38 if (additionalInfos === '{}') additionalInfos = ''
39 else additionalInfos = ' ' + additionalInfos
42 if (CONFIG.LOG.PRETTIFY_SQL) {
43 additionalInfos += '\n' + sqlFormat(info.sql, {
48 additionalInfos += ' - ' + info.sql
52 return `[${info.label}] ${toTimeFormat(info.timestamp)} ${info.level}: ${info.message}${additionalInfos}`
55 const logger = winston.createLogger({
57 new winston.transports.Console({
58 level: options.level || 'debug',
60 format: winston.format.combine(
61 winston.format.splat(),
63 winston.format.colorize(),
72 error: logger.error.bind(logger),
73 warn: logger.warn.bind(logger),
74 info: logger.info.bind(logger),
75 debug: logger.debug.bind(logger)
79 .then(() => process.exit(0))
80 .catch(err => console.error(err))
83 return new Promise<void>(async res => {
84 const files = await getFiles()
86 for (const file of files) {
87 if (file === 'peertube-audit.log') continue
89 console.log('Opening %s.', file)
91 const stream = createReadStream(file)
93 const rl = createInterface({
97 rl.on('line', line => {
99 const log = JSON.parse(line)
100 if (options.tags && !containsTags(log.tags, options.tags)) {
104 if (options.notTags && containsTags(log.tags, options.notTags)) {
108 // Don't know why but loggerFormat does not remove splat key
109 Object.assign(log, { splat: undefined })
111 logLevels[log.level](log)
113 console.error('Cannot parse line.', inspect(line))
118 stream.once('close', () => res())
123 // Thanks: https://stackoverflow.com/a/37014317
124 async function getNewestFile (files: string[], basePath: string) {
125 const sorted = await mtimeSortFilesDesc(files, basePath)
127 return (sorted.length > 0) ? sorted[0].file : ''
130 async function getFiles () {
131 if (options.files) return options.files
133 const logFiles = await readdir(CONFIG.STORAGE.LOG_DIR)
135 const filename = await getNewestFile(logFiles, CONFIG.STORAGE.LOG_DIR)
136 return [ join(CONFIG.STORAGE.LOG_DIR, filename) ]
139 function toTimeFormat (time: string) {
140 const timestamp = Date.parse(time)
142 if (isNaN(timestamp) === true) return 'Unknown date'
144 const d = new Date(timestamp)
145 return d.toLocaleString() + `.${d.getMilliseconds()}`
148 function containsTags (loggerTags: string[], optionsTags: string[]) {
149 if (!loggerTags) return false
151 for (const lt of loggerTags) {
152 for (const ot of optionsTags) {
153 if (lt === ot) return true