2018-06-19 10:45:33 +02:00
|
|
|
import * as program from 'commander'
|
2018-08-27 13:28:49 +02:00
|
|
|
import { createReadStream, readdirSync, statSync } from 'fs-extra'
|
2017-11-16 17:29:05 +01:00
|
|
|
import { join } from 'path'
|
|
|
|
import { createInterface } from 'readline'
|
|
|
|
import * as winston from 'winston'
|
2018-01-25 15:05:18 +01:00
|
|
|
import { labelFormatter } from '../server/helpers/logger'
|
2017-11-16 17:29:05 +01:00
|
|
|
import { CONFIG } from '../server/initializers/constants'
|
|
|
|
|
2018-06-19 10:45:33 +02:00
|
|
|
program
|
|
|
|
.option('-l, --level [level]', 'Level log (debug/info/warn/error)')
|
|
|
|
.parse(process.argv)
|
|
|
|
|
2018-01-25 15:05:18 +01:00
|
|
|
const excludedKeys = {
|
|
|
|
level: true,
|
|
|
|
message: true,
|
|
|
|
splat: true,
|
|
|
|
timestamp: true,
|
|
|
|
label: true
|
|
|
|
}
|
|
|
|
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
|
|
|
|
|
2018-03-08 18:16:15 +01:00
|
|
|
return `[${info.label}] ${toTimeFormat(info.timestamp)} ${info.level}: ${info.message}${additionalInfos}`
|
2018-01-25 15:05:18 +01:00
|
|
|
})
|
|
|
|
|
2018-06-26 17:52:14 +02:00
|
|
|
const logger = winston.createLogger({
|
2017-11-16 17:29:05 +01:00
|
|
|
transports: [
|
|
|
|
new winston.transports.Console({
|
2018-06-19 10:45:33 +02:00
|
|
|
level: program['level'] || 'debug',
|
2018-01-19 13:58:13 +01:00
|
|
|
stderrLevels: [],
|
|
|
|
format: winston.format.combine(
|
|
|
|
winston.format.splat(),
|
|
|
|
labelFormatter,
|
|
|
|
winston.format.colorize(),
|
|
|
|
loggerFormat
|
|
|
|
)
|
2017-11-16 17:29:05 +01:00
|
|
|
})
|
|
|
|
],
|
|
|
|
exitOnError: true
|
|
|
|
})
|
|
|
|
|
|
|
|
const logLevels = {
|
2018-01-19 13:58:13 +01:00
|
|
|
error: logger.error.bind(logger),
|
|
|
|
warn: logger.warn.bind(logger),
|
|
|
|
info: logger.info.bind(logger),
|
|
|
|
debug: logger.debug.bind(logger)
|
2017-11-16 17:29:05 +01:00
|
|
|
}
|
|
|
|
|
2018-06-26 17:52:14 +02:00
|
|
|
const logFiles = readdirSync(CONFIG.STORAGE.LOG_DIR)
|
2018-07-16 14:38:11 +02:00
|
|
|
const lastLogFile = getNewestFile(logFiles, CONFIG.STORAGE.LOG_DIR)
|
2018-06-26 17:52:14 +02:00
|
|
|
|
|
|
|
const path = join(CONFIG.STORAGE.LOG_DIR, lastLogFile)
|
2017-11-16 17:29:05 +01:00
|
|
|
console.log('Opening %s.', path)
|
|
|
|
|
|
|
|
const rl = createInterface({
|
|
|
|
input: createReadStream(path)
|
|
|
|
})
|
|
|
|
|
|
|
|
rl.on('line', line => {
|
|
|
|
const log = JSON.parse(line)
|
2018-01-19 13:58:13 +01:00
|
|
|
// Don't know why but loggerFormat does not remove splat key
|
|
|
|
Object.assign(log, { splat: undefined })
|
2017-11-17 11:35:10 +01:00
|
|
|
|
2018-01-19 13:58:13 +01:00
|
|
|
logLevels[log.level](log)
|
2017-11-16 17:29:05 +01:00
|
|
|
})
|
2018-03-08 18:16:15 +01:00
|
|
|
|
|
|
|
function toTimeFormat (time: string) {
|
|
|
|
const timestamp = Date.parse(time)
|
|
|
|
|
|
|
|
if (isNaN(timestamp) === true) return 'Unknown date'
|
|
|
|
|
|
|
|
return new Date(timestamp).toISOString()
|
|
|
|
}
|
2018-07-16 14:38:11 +02:00
|
|
|
|
|
|
|
// Thanks: https://stackoverflow.com/a/37014317
|
|
|
|
function getNewestFile (files: string[], basePath: string) {
|
|
|
|
const out = []
|
|
|
|
|
|
|
|
files.forEach(file => {
|
|
|
|
const stats = statSync(basePath + '/' + file)
|
|
|
|
if (stats.isFile()) out.push({ file, mtime: stats.mtime.getTime() })
|
|
|
|
})
|
|
|
|
|
|
|
|
out.sort((a, b) => b.mtime - a.mtime)
|
|
|
|
|
|
|
|
return (out.length > 0) ? out[ 0 ].file : ''
|
2018-07-30 10:59:31 +02:00
|
|
|
}
|