2021-06-25 10:39:27 -05:00
|
|
|
import { program } from 'commander'
|
2023-07-31 07:34:36 -05:00
|
|
|
import { createReadStream } from 'fs'
|
|
|
|
import { readdir } from 'fs/promises'
|
2017-11-16 10:29:05 -06:00
|
|
|
import { join } from 'path'
|
2022-08-16 01:06:49 -05:00
|
|
|
import { stdin } from 'process'
|
2017-11-16 10:29:05 -06:00
|
|
|
import { createInterface } from 'readline'
|
2022-01-03 10:13:11 -06:00
|
|
|
import { format as sqlFormat } from 'sql-formatter'
|
|
|
|
import { inspect } from 'util'
|
2017-11-16 10:29:05 -06:00
|
|
|
import * as winston from 'winston'
|
2023-07-31 07:34:36 -05:00
|
|
|
import { labelFormatter, mtimeSortFilesDesc } from '@server/helpers/logger.js'
|
|
|
|
import { CONFIG } from '@server/initializers/config.js'
|
2017-11-16 10:29:05 -06:00
|
|
|
|
2018-06-19 03:45:33 -05:00
|
|
|
program
|
|
|
|
.option('-l, --level [level]', 'Level log (debug/info/warn/error)')
|
2020-12-10 08:43:01 -06:00
|
|
|
.option('-f, --files [file...]', 'Files to parse. If not provided, the script will parse the latest log file from config)')
|
2021-03-05 06:26:02 -06:00
|
|
|
.option('-t, --tags [tags...]', 'Display only lines with these tags')
|
2024-02-22 03:12:04 -06:00
|
|
|
.option('-nt, --not-tags [tags...]', 'Do not display lines containing these tags')
|
2018-06-19 03:45:33 -05:00
|
|
|
.parse(process.argv)
|
|
|
|
|
2021-02-03 02:33:05 -06:00
|
|
|
const options = program.opts()
|
|
|
|
|
2018-01-25 08:05:18 -06:00
|
|
|
const excludedKeys = {
|
|
|
|
level: true,
|
|
|
|
message: true,
|
|
|
|
splat: true,
|
|
|
|
timestamp: true,
|
2021-03-05 06:26:02 -06:00
|
|
|
tags: true,
|
2021-02-01 08:03:32 -06:00
|
|
|
label: true,
|
|
|
|
sql: true
|
2018-01-25 08:05:18 -06:00
|
|
|
}
|
|
|
|
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
|
|
|
|
|
2021-02-01 08:03:32 -06:00
|
|
|
if (info.sql) {
|
|
|
|
if (CONFIG.LOG.PRETTIFY_SQL) {
|
2024-11-25 03:01:40 -06:00
|
|
|
additionalInfos += '\n' + sqlFormat(info.sql as string, {
|
2021-02-01 08:03:32 -06:00
|
|
|
language: 'sql',
|
2022-06-21 04:16:38 -05:00
|
|
|
tabWidth: 2
|
2021-02-01 08:03:32 -06:00
|
|
|
})
|
|
|
|
} else {
|
|
|
|
additionalInfos += ' - ' + info.sql
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-25 03:01:40 -06:00
|
|
|
return `[${info.label}] ${toTimeFormat(info.timestamp as string)} ${info.level}: ${info.message}${additionalInfos}`
|
2018-01-25 08:05:18 -06:00
|
|
|
})
|
|
|
|
|
2018-06-26 10:52:14 -05:00
|
|
|
const logger = winston.createLogger({
|
2017-11-16 10:29:05 -06:00
|
|
|
transports: [
|
|
|
|
new winston.transports.Console({
|
2021-02-03 02:33:05 -06:00
|
|
|
level: options.level || 'debug',
|
2018-01-19 06:58:13 -06:00
|
|
|
stderrLevels: [],
|
|
|
|
format: winston.format.combine(
|
|
|
|
winston.format.splat(),
|
2020-04-09 04:00:30 -05:00
|
|
|
labelFormatter(),
|
2018-01-19 06:58:13 -06:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-04-10 08:26:33 -05:00
|
|
|
run()
|
|
|
|
.then(() => process.exit(0))
|
|
|
|
.catch(err => console.error(err))
|
2018-06-26 10:52:14 -05:00
|
|
|
|
2022-07-12 09:21:13 -05:00
|
|
|
async function run () {
|
|
|
|
const files = await getFiles()
|
2017-11-16 10:29:05 -06:00
|
|
|
|
2022-07-12 09:21:13 -05:00
|
|
|
for (const file of files) {
|
|
|
|
if (file === 'peertube-audit.log') continue
|
2021-06-16 08:14:41 -05:00
|
|
|
|
2022-07-12 09:21:13 -05:00
|
|
|
await readFile(file)
|
|
|
|
}
|
|
|
|
}
|
2017-11-17 04:35:10 -06:00
|
|
|
|
2022-07-12 09:21:13 -05:00
|
|
|
function readFile (file: string) {
|
|
|
|
console.log('Opening %s.', file)
|
2018-03-08 11:16:15 -06:00
|
|
|
|
2022-08-16 01:06:49 -05:00
|
|
|
const stream = file === '-' ? stdin : createReadStream(file)
|
2021-03-05 06:26:02 -06:00
|
|
|
|
2022-07-12 09:21:13 -05:00
|
|
|
const rl = createInterface({
|
|
|
|
input: stream
|
|
|
|
})
|
2021-03-05 06:26:02 -06:00
|
|
|
|
2022-07-12 09:21:13 -05:00
|
|
|
return new Promise<void>(res => {
|
|
|
|
rl.on('line', line => {
|
|
|
|
try {
|
|
|
|
const log = JSON.parse(line)
|
|
|
|
if (options.tags && !containsTags(log.tags, options.tags)) {
|
|
|
|
return
|
|
|
|
}
|
2020-12-22 03:15:06 -06:00
|
|
|
|
2022-07-12 09:21:13 -05:00
|
|
|
if (options.notTags && containsTags(log.tags, options.notTags)) {
|
|
|
|
return
|
2020-12-22 03:15:06 -06:00
|
|
|
}
|
2018-03-08 11:16:15 -06:00
|
|
|
|
2022-07-12 09:21:13 -05:00
|
|
|
// Don't know why but loggerFormat does not remove splat key
|
|
|
|
Object.assign(log, { splat: undefined })
|
|
|
|
|
|
|
|
logLevels[log.level](log)
|
|
|
|
} catch (err) {
|
|
|
|
console.error('Cannot parse line.', inspect(line))
|
|
|
|
throw err
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2022-08-16 01:06:49 -05:00
|
|
|
stream.once('end', () => res())
|
2019-04-10 08:26:33 -05:00
|
|
|
})
|
2018-03-08 11:16:15 -06:00
|
|
|
}
|
2018-07-16 07:38:11 -05:00
|
|
|
|
|
|
|
// Thanks: https://stackoverflow.com/a/37014317
|
2019-04-10 08:26:33 -05:00
|
|
|
async function getNewestFile (files: string[], basePath: string) {
|
|
|
|
const sorted = await mtimeSortFilesDesc(files, basePath)
|
2018-07-16 07:38:11 -05:00
|
|
|
|
2020-07-30 07:54:31 -05:00
|
|
|
return (sorted.length > 0) ? sorted[0].file : ''
|
2019-04-10 08:26:33 -05:00
|
|
|
}
|
|
|
|
|
2020-12-10 08:43:01 -06:00
|
|
|
async function getFiles () {
|
2021-02-03 02:33:05 -06:00
|
|
|
if (options.files) return options.files
|
2020-12-10 08:43:01 -06:00
|
|
|
|
|
|
|
const logFiles = await readdir(CONFIG.STORAGE.LOG_DIR)
|
|
|
|
|
|
|
|
const filename = await getNewestFile(logFiles, CONFIG.STORAGE.LOG_DIR)
|
|
|
|
return [ join(CONFIG.STORAGE.LOG_DIR, filename) ]
|
|
|
|
}
|
|
|
|
|
2019-04-10 08:26:33 -05:00
|
|
|
function toTimeFormat (time: string) {
|
|
|
|
const timestamp = Date.parse(time)
|
2018-07-16 07:38:11 -05:00
|
|
|
|
2019-04-10 08:26:33 -05:00
|
|
|
if (isNaN(timestamp) === true) return 'Unknown date'
|
2018-07-16 07:38:11 -05:00
|
|
|
|
2021-06-29 08:22:12 -05:00
|
|
|
const d = new Date(timestamp)
|
|
|
|
return d.toLocaleString() + `.${d.getMilliseconds()}`
|
2018-07-30 03:59:31 -05:00
|
|
|
}
|
2021-03-05 06:26:02 -06:00
|
|
|
|
|
|
|
function containsTags (loggerTags: string[], optionsTags: string[]) {
|
|
|
|
if (!loggerTags) return false
|
|
|
|
|
|
|
|
for (const lt of loggerTags) {
|
|
|
|
for (const ot of optionsTags) {
|
|
|
|
if (lt === ot) return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|