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'
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)')
20 const excludedKeys = {
28 function keysExcluder (key, value) {
29 return excludedKeys[key] === true ? undefined : value
32 const loggerFormat = winston.format.printf((info) => {
33 let additionalInfos = JSON.stringify(info, keysExcluder, 2)
34 if (additionalInfos === '{}') additionalInfos = ''
35 else additionalInfos = ' ' + additionalInfos
38 if (CONFIG.LOG.PRETTIFY_SQL) {
39 additionalInfos += '\n' + sqlFormat(info.sql, {
44 additionalInfos += ' - ' + info.sql
48 return `[${info.label}] ${toTimeFormat(info.timestamp)} ${info.level}: ${info.message}${additionalInfos}`
51 const logger = winston.createLogger({
53 new winston.transports.Console({
54 level: program['level'] || 'debug',
56 format: winston.format.combine(
57 winston.format.splat(),
59 winston.format.colorize(),
68 error: logger.error.bind(logger),
69 warn: logger.warn.bind(logger),
70 info: logger.info.bind(logger),
71 debug: logger.debug.bind(logger)
75 .then(() => process.exit(0))
76 .catch(err => console.error(err))
79 return new Promise(async res => {
80 const files = await getFiles()
82 for (const file of files) {
83 console.log('Opening %s.', file)
85 const stream = createReadStream(file)
87 const rl = createInterface({
91 rl.on('line', line => {
93 const log = JSON.parse(line)
94 // Don't know why but loggerFormat does not remove splat key
95 Object.assign(log, { splat: undefined })
97 logLevels[log.level](log)
99 console.error('Cannot parse line.', inspect(line))
104 stream.once('close', () => res())
109 // Thanks: https://stackoverflow.com/a/37014317
110 async function getNewestFile (files: string[], basePath: string) {
111 const sorted = await mtimeSortFilesDesc(files, basePath)
113 return (sorted.length > 0) ? sorted[0].file : ''
116 async function getFiles () {
117 if (program['files']) return program['files']
119 const logFiles = await readdir(CONFIG.STORAGE.LOG_DIR)
121 const filename = await getNewestFile(logFiles, CONFIG.STORAGE.LOG_DIR)
122 return [ join(CONFIG.STORAGE.LOG_DIR, filename) ]
125 function toTimeFormat (time: string) {
126 const timestamp = Date.parse(time)
128 if (isNaN(timestamp) === true) return 'Unknown date'
130 return new Date(timestamp).toISOString()