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'
14 .option('-l, --level [level]', 'Level log (debug/info/warn/error)')
17 const excludedKeys = {
24 function keysExcluder (key, value) {
25 return excludedKeys[key] === true ? undefined : value
28 const loggerFormat = winston.format.printf((info) => {
29 let additionalInfos = JSON.stringify(info, keysExcluder, 2)
30 if (additionalInfos === '{}') additionalInfos = ''
31 else additionalInfos = ' ' + additionalInfos
33 return `[${info.label}] ${toTimeFormat(info.timestamp)} ${info.level}: ${info.message}${additionalInfos}`
36 const logger = winston.createLogger({
38 new winston.transports.Console({
39 level: program['level'] || 'debug',
41 format: winston.format.combine(
42 winston.format.splat(),
44 winston.format.colorize(),
53 error: logger.error.bind(logger),
54 warn: logger.warn.bind(logger),
55 info: logger.info.bind(logger),
56 debug: logger.debug.bind(logger)
60 .then(() => process.exit(0))
61 .catch(err => console.error(err))
64 return new Promise(async res => {
65 const logFiles = await readdir(CONFIG.STORAGE.LOG_DIR)
66 const lastLogFile = await getNewestFile(logFiles, CONFIG.STORAGE.LOG_DIR)
68 const path = join(CONFIG.STORAGE.LOG_DIR, lastLogFile)
69 console.log('Opening %s.', path)
71 const stream = createReadStream(path)
73 const rl = createInterface({
77 rl.on('line', line => {
78 const log = JSON.parse(line)
79 // Don't know why but loggerFormat does not remove splat key
80 Object.assign(log, { splat: undefined })
82 logLevels[log.level](log)
85 stream.once('close', () => res())
89 // Thanks: https://stackoverflow.com/a/37014317
90 async function getNewestFile (files: string[], basePath: string) {
91 const sorted = await mtimeSortFilesDesc(files, basePath)
93 return (sorted.length > 0) ? sorted[0].file : ''
96 function toTimeFormat (time: string) {
97 const timestamp = Date.parse(time)
99 if (isNaN(timestamp) === true) return 'Unknown date'
101 return new Date(timestamp).toISOString()