2019-04-10 09:26:33 -04:00
|
|
|
import * as express from 'express'
|
|
|
|
import { UserRight } from '../../../../shared/models/users'
|
|
|
|
import { asyncMiddleware, authenticate, ensureUserHasRight } from '../../../middlewares'
|
2019-04-11 11:33:36 -04:00
|
|
|
import { mtimeSortFilesDesc } from '../../../../shared/core-utils/logs/logs'
|
2019-04-11 04:05:43 -04:00
|
|
|
import { readdir, readFile } from 'fs-extra'
|
2019-12-11 08:14:01 -05:00
|
|
|
import { AUDIT_LOG_FILENAME, MAX_LOGS_OUTPUT_CHARACTERS, LOG_FILENAME } from '../../../initializers/constants'
|
2019-04-10 09:26:33 -04:00
|
|
|
import { join } from 'path'
|
2019-12-11 08:14:01 -05:00
|
|
|
import { getAuditLogsValidator, getLogsValidator } from '../../../middlewares/validators/logs'
|
2019-04-10 09:26:33 -04:00
|
|
|
import { LogLevel } from '../../../../shared/models/server/log-level.type'
|
2019-04-11 05:33:44 -04:00
|
|
|
import { CONFIG } from '../../../initializers/config'
|
2019-12-11 08:14:01 -05:00
|
|
|
import { logger } from '@server/helpers/logger'
|
2019-04-10 09:26:33 -04:00
|
|
|
|
|
|
|
const logsRouter = express.Router()
|
|
|
|
|
|
|
|
logsRouter.get('/logs',
|
|
|
|
authenticate,
|
|
|
|
ensureUserHasRight(UserRight.MANAGE_LOGS),
|
|
|
|
getLogsValidator,
|
|
|
|
asyncMiddleware(getLogs)
|
|
|
|
)
|
|
|
|
|
2019-12-11 08:14:01 -05:00
|
|
|
logsRouter.get('/audit-logs',
|
|
|
|
authenticate,
|
|
|
|
ensureUserHasRight(UserRight.MANAGE_LOGS),
|
|
|
|
getAuditLogsValidator,
|
|
|
|
asyncMiddleware(getAuditLogs)
|
|
|
|
)
|
|
|
|
|
2019-04-10 09:26:33 -04:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
|
|
|
logsRouter
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2019-12-11 08:14:01 -05:00
|
|
|
const auditLogNameFilter = generateLogNameFilter(AUDIT_LOG_FILENAME)
|
|
|
|
async function getAuditLogs (req: express.Request, res: express.Response) {
|
|
|
|
const output = await generateOutput({
|
|
|
|
startDateQuery: req.query.startDate,
|
|
|
|
endDateQuery: req.query.endDate,
|
|
|
|
level: 'audit',
|
|
|
|
nameFilter: auditLogNameFilter
|
|
|
|
})
|
|
|
|
|
|
|
|
return res.json(output).end()
|
|
|
|
}
|
|
|
|
|
|
|
|
const logNameFilter = generateLogNameFilter(LOG_FILENAME)
|
2019-04-10 09:26:33 -04:00
|
|
|
async function getLogs (req: express.Request, res: express.Response) {
|
2019-12-11 08:14:01 -05:00
|
|
|
const output = await generateOutput({
|
|
|
|
startDateQuery: req.query.startDate,
|
|
|
|
endDateQuery: req.query.endDate,
|
|
|
|
level: req.query.level || 'info',
|
|
|
|
nameFilter: logNameFilter
|
|
|
|
})
|
|
|
|
|
|
|
|
return res.json(output).end()
|
|
|
|
}
|
|
|
|
|
|
|
|
async function generateOutput (options: {
|
2020-01-31 10:56:52 -05:00
|
|
|
startDateQuery: string
|
|
|
|
endDateQuery?: string
|
|
|
|
level: LogLevel
|
2019-12-11 08:14:01 -05:00
|
|
|
nameFilter: RegExp
|
|
|
|
}) {
|
|
|
|
const { startDateQuery, level, nameFilter } = options
|
|
|
|
|
2019-04-10 09:26:33 -04:00
|
|
|
const logFiles = await readdir(CONFIG.STORAGE.LOG_DIR)
|
|
|
|
const sortedLogFiles = await mtimeSortFilesDesc(logFiles, CONFIG.STORAGE.LOG_DIR)
|
|
|
|
let currentSize = 0
|
|
|
|
|
2019-12-11 08:14:01 -05:00
|
|
|
const startDate = new Date(startDateQuery)
|
|
|
|
const endDate = options.endDateQuery ? new Date(options.endDateQuery) : new Date()
|
2019-04-10 09:26:33 -04:00
|
|
|
|
2019-04-11 04:05:43 -04:00
|
|
|
let output: string[] = []
|
2019-04-10 09:26:33 -04:00
|
|
|
|
|
|
|
for (const meta of sortedLogFiles) {
|
2019-12-11 08:14:01 -05:00
|
|
|
if (nameFilter.exec(meta.file) === null) continue
|
|
|
|
|
2019-04-10 09:26:33 -04:00
|
|
|
const path = join(CONFIG.STORAGE.LOG_DIR, meta.file)
|
2019-12-12 03:15:38 -05:00
|
|
|
logger.debug('Opening %s to fetch logs.', path)
|
2019-04-10 09:26:33 -04:00
|
|
|
|
|
|
|
const result = await getOutputFromFile(path, startDate, endDate, level, currentSize)
|
|
|
|
if (!result.output) break
|
|
|
|
|
2019-04-11 04:05:43 -04:00
|
|
|
output = result.output.concat(output)
|
2019-04-10 09:26:33 -04:00
|
|
|
currentSize = result.currentSize
|
|
|
|
|
2019-04-11 04:05:43 -04:00
|
|
|
if (currentSize > MAX_LOGS_OUTPUT_CHARACTERS || (result.logTime && result.logTime < startDate.getTime())) break
|
2019-04-10 09:26:33 -04:00
|
|
|
}
|
|
|
|
|
2019-12-11 08:14:01 -05:00
|
|
|
return output
|
2019-04-10 09:26:33 -04:00
|
|
|
}
|
|
|
|
|
2019-04-11 04:05:43 -04:00
|
|
|
async function getOutputFromFile (path: string, startDate: Date, endDate: Date, level: LogLevel, currentSize: number) {
|
2019-04-10 09:26:33 -04:00
|
|
|
const startTime = startDate.getTime()
|
|
|
|
const endTime = endDate.getTime()
|
2019-04-11 04:05:43 -04:00
|
|
|
let logTime: number
|
2019-04-10 09:26:33 -04:00
|
|
|
|
|
|
|
const logsLevel: { [ id in LogLevel ]: number } = {
|
2019-12-11 08:14:01 -05:00
|
|
|
audit: -1,
|
2019-04-10 09:26:33 -04:00
|
|
|
debug: 0,
|
|
|
|
info: 1,
|
|
|
|
warn: 2,
|
|
|
|
error: 3
|
|
|
|
}
|
|
|
|
|
2019-04-11 04:05:43 -04:00
|
|
|
const content = await readFile(path)
|
|
|
|
const lines = content.toString().split('\n')
|
|
|
|
const output: any[] = []
|
2019-04-10 09:26:33 -04:00
|
|
|
|
2019-04-11 04:05:43 -04:00
|
|
|
for (let i = lines.length - 1; i >= 0; i--) {
|
2020-01-31 10:56:52 -05:00
|
|
|
const line = lines[i]
|
2019-04-11 04:05:43 -04:00
|
|
|
let log: any
|
2019-04-10 09:26:33 -04:00
|
|
|
|
2019-04-11 04:05:43 -04:00
|
|
|
try {
|
|
|
|
log = JSON.parse(line)
|
|
|
|
} catch {
|
|
|
|
// Maybe there a multiple \n at the end of the file
|
|
|
|
continue
|
|
|
|
}
|
2019-04-10 09:26:33 -04:00
|
|
|
|
2019-04-11 04:05:43 -04:00
|
|
|
logTime = new Date(log.timestamp).getTime()
|
2020-01-31 10:56:52 -05:00
|
|
|
if (logTime >= startTime && logTime <= endTime && logsLevel[log.level] >= logsLevel[level]) {
|
2019-04-11 04:05:43 -04:00
|
|
|
output.push(log)
|
2019-04-10 09:26:33 -04:00
|
|
|
|
2019-04-11 04:05:43 -04:00
|
|
|
currentSize += line.length
|
2019-04-10 09:26:33 -04:00
|
|
|
|
2019-04-11 04:05:43 -04:00
|
|
|
if (currentSize > MAX_LOGS_OUTPUT_CHARACTERS) break
|
|
|
|
} else if (logTime < startTime) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2019-04-10 09:26:33 -04:00
|
|
|
|
2019-04-11 04:05:43 -04:00
|
|
|
return { currentSize, output: output.reverse(), logTime }
|
2019-04-10 09:26:33 -04:00
|
|
|
}
|
2019-12-11 08:14:01 -05:00
|
|
|
|
|
|
|
function generateLogNameFilter (baseName: string) {
|
2019-12-12 03:15:38 -05:00
|
|
|
return new RegExp('^' + baseName.replace(/\.log$/, '') + '\\d*.log$')
|
2019-12-11 08:14:01 -05:00
|
|
|
}
|