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 } from '../server/helpers/logger'
10 import { CONFIG } from '../server/initializers/config'
11 import { mtimeSortFilesDesc } from '../shared/core-utils/logs/logs'
12 import { inspect } from 'util'
13 import { format as sqlFormat } from 'sql-formatter'
16 .option('-l, --level [level]', 'Level log (debug/info/warn/error)')
17 .option('-f, --files [file...]', 'Files to parse. If not provided, the script will parse the latest log file from config)')
18 .option('-t, --tags [tags...]', 'Display only lines with these tags')
19 .option('-nt, --not-tags [tags...]', 'Donrt display lines containing these tags')
22 const options = program.opts()
24 const excludedKeys = {
33 function keysExcluder (key, value) {
34 return excludedKeys[key] === true ? undefined : value
37 const loggerFormat = winston.format.printf((info) => {
38 let additionalInfos = JSON.stringify(info, keysExcluder, 2)
39 if (additionalInfos === '{}') additionalInfos = ''
40 else additionalInfos = ' ' + additionalInfos
43 if (CONFIG.LOG.PRETTIFY_SQL) {
44 additionalInfos += '\n' + sqlFormat(info.sql, {
49 additionalInfos += ' - ' + info.sql
53 return `[${info.label}] ${toTimeFormat(info.timestamp)} ${info.level}: ${info.message}${additionalInfos}`
56 const logger = winston.createLogger({
58 new winston.transports.Console({
59 level: options.level || 'debug',
61 format: winston.format.combine(
62 winston.format.splat(),
64 winston.format.colorize(),
73 error: logger.error.bind(logger),
74 warn: logger.warn.bind(logger),
75 info: logger.info.bind(logger),
76 debug: logger.debug.bind(logger)
80 .then(() => process.exit(0))
81 .catch(err => console.error(err))
84 return new Promise<void>(async res => {
85 const files = await getFiles()
87 for (const file of files) {
88 if (file === 'peertube-audit.log') continue
90 console.log('Opening %s.', file)
92 const stream = createReadStream(file)
94 const rl = createInterface({
98 rl.on('line', line => {
100 const log = JSON.parse(line)
101 if (options.tags && !containsTags(log.tags, options.tags)) {
105 if (options.notTags && containsTags(log.tags, options.notTags)) {
109 // Don't know why but loggerFormat does not remove splat key
110 Object.assign(log, { splat: undefined })
112 logLevels[log.level](log)
114 console.error('Cannot parse line.', inspect(line))
119 stream.once('close', () => res())
124 // Thanks: https://stackoverflow.com/a/37014317
125 async function getNewestFile (files: string[], basePath: string) {
126 const sorted = await mtimeSortFilesDesc(files, basePath)
128 return (sorted.length > 0) ? sorted[0].file : ''
131 async function getFiles () {
132 if (options.files) return options.files
134 const logFiles = await readdir(CONFIG.STORAGE.LOG_DIR)
136 const filename = await getNewestFile(logFiles, CONFIG.STORAGE.LOG_DIR)
137 return [ join(CONFIG.STORAGE.LOG_DIR, filename) ]
140 function toTimeFormat (time: string) {
141 const timestamp = Date.parse(time)
143 if (isNaN(timestamp) === true) return 'Unknown date'
145 const d = new Date(timestamp)
146 return d.toLocaleString() + `.${d.getMilliseconds()}`
149 function containsTags (loggerTags: string[], optionsTags: string[]) {
150 if (!loggerTags) return false
152 for (const lt of loggerTags) {
153 for (const ot of optionsTags) {
154 if (lt === ot) return true