1 import * as program from 'commander'
2 import { createReadStream, readdir } from 'fs-extra'
3 import { join } from 'path'
4 import { createInterface } from 'readline'
5 import * as winston from 'winston'
6 import { labelFormatter } from '../server/helpers/logger'
7 import { CONFIG } from '../server/initializers/config'
8 import { mtimeSortFilesDesc } from '../shared/core-utils/logs/logs'
11 .option('-l, --level [level]', 'Level log (debug/info/warn/error)')
14 const excludedKeys = {
21 function keysExcluder (key, value) {
22 return excludedKeys[key] === true ? undefined : value
25 const loggerFormat = winston.format.printf((info) => {
26 let additionalInfos = JSON.stringify(info, keysExcluder, 2)
27 if (additionalInfos === '{}') additionalInfos = ''
28 else additionalInfos = ' ' + additionalInfos
30 return `[${info.label}] ${toTimeFormat(info.timestamp)} ${info.level}: ${info.message}${additionalInfos}`
33 const logger = winston.createLogger({
35 new winston.transports.Console({
36 level: program['level'] || 'debug',
38 format: winston.format.combine(
39 winston.format.splat(),
41 winston.format.colorize(),
50 error: logger.error.bind(logger),
51 warn: logger.warn.bind(logger),
52 info: logger.info.bind(logger),
53 debug: logger.debug.bind(logger)
57 .then(() => process.exit(0))
58 .catch(err => console.error(err))
61 return new Promise(async res => {
62 const logFiles = await readdir(CONFIG.STORAGE.LOG_DIR)
63 const lastLogFile = await getNewestFile(logFiles, CONFIG.STORAGE.LOG_DIR)
65 const path = join(CONFIG.STORAGE.LOG_DIR, lastLogFile)
66 console.log('Opening %s.', path)
68 const stream = createReadStream(path)
70 const rl = createInterface({
74 rl.on('line', line => {
75 const log = JSON.parse(line)
76 // Don't know why but loggerFormat does not remove splat key
77 Object.assign(log, { splat: undefined })
79 logLevels[ log.level ](log)
82 stream.once('close', () => res())
86 // Thanks: https://stackoverflow.com/a/37014317
87 async function getNewestFile (files: string[], basePath: string) {
88 const sorted = await mtimeSortFilesDesc(files, basePath)
90 return (sorted.length > 0) ? sorted[ 0 ].file : ''
93 function toTimeFormat (time: string) {
94 const timestamp = Date.parse(time)
96 if (isNaN(timestamp) === true) return 'Unknown date'
98 return new Date(timestamp).toISOString()