1 import { registerTSPaths } from '../server/helpers/register-ts-paths'
4 import * as 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'
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)')
19 const excludedKeys = {
26 function keysExcluder (key, value) {
27 return excludedKeys[key] === true ? undefined : value
30 const loggerFormat = winston.format.printf((info) => {
31 let additionalInfos = JSON.stringify(info, keysExcluder, 2)
32 if (additionalInfos === '{}') additionalInfos = ''
33 else additionalInfos = ' ' + additionalInfos
35 return `[${info.label}] ${toTimeFormat(info.timestamp)} ${info.level}: ${info.message}${additionalInfos}`
38 const logger = winston.createLogger({
40 new winston.transports.Console({
41 level: program['level'] || 'debug',
43 format: winston.format.combine(
44 winston.format.splat(),
46 winston.format.colorize(),
55 error: logger.error.bind(logger),
56 warn: logger.warn.bind(logger),
57 info: logger.info.bind(logger),
58 debug: logger.debug.bind(logger)
62 .then(() => process.exit(0))
63 .catch(err => console.error(err))
66 return new Promise(async res => {
67 const files = await getFiles()
69 for (const file of files) {
70 console.log('Opening %s.', file)
72 const stream = createReadStream(file)
74 const rl = createInterface({
78 rl.on('line', line => {
80 const log = JSON.parse(line)
81 // Don't know why but loggerFormat does not remove splat key
82 Object.assign(log, { splat: undefined })
84 logLevels[log.level](log)
86 console.error('Cannot parse line.', inspect(line))
91 stream.once('close', () => res())
96 // Thanks: https://stackoverflow.com/a/37014317
97 async function getNewestFile (files: string[], basePath: string) {
98 const sorted = await mtimeSortFilesDesc(files, basePath)
100 return (sorted.length > 0) ? sorted[0].file : ''
103 async function getFiles () {
104 if (program['files']) return program['files']
106 const logFiles = await readdir(CONFIG.STORAGE.LOG_DIR)
108 const filename = await getNewestFile(logFiles, CONFIG.STORAGE.LOG_DIR)
109 return [ join(CONFIG.STORAGE.LOG_DIR, filename) ]
112 function toTimeFormat (time: string) {
113 const timestamp = Date.parse(time)
115 if (isNaN(timestamp) === true) return 'Unknown date'
117 return new Date(timestamp).toISOString()