]>
Commit | Line | Data |
---|---|---|
1 | import express from 'express' | |
2 | import { readdir, readFile } from 'fs-extra' | |
3 | import { join } from 'path' | |
4 | import { logger, mtimeSortFilesDesc } from '@server/helpers/logger' | |
5 | import { LogLevel } from '../../../../shared/models/server/log-level.type' | |
6 | import { UserRight } from '../../../../shared/models/users' | |
7 | import { CONFIG } from '../../../initializers/config' | |
8 | import { AUDIT_LOG_FILENAME, LOG_FILENAME, MAX_LOGS_OUTPUT_CHARACTERS } from '../../../initializers/constants' | |
9 | import { asyncMiddleware, authenticate, ensureUserHasRight } from '../../../middlewares' | |
10 | import { getAuditLogsValidator, getLogsValidator } from '../../../middlewares/validators/logs' | |
11 | ||
12 | const logsRouter = express.Router() | |
13 | ||
14 | logsRouter.get('/logs', | |
15 | authenticate, | |
16 | ensureUserHasRight(UserRight.MANAGE_LOGS), | |
17 | getLogsValidator, | |
18 | asyncMiddleware(getLogs) | |
19 | ) | |
20 | ||
21 | logsRouter.get('/audit-logs', | |
22 | authenticate, | |
23 | ensureUserHasRight(UserRight.MANAGE_LOGS), | |
24 | getAuditLogsValidator, | |
25 | asyncMiddleware(getAuditLogs) | |
26 | ) | |
27 | ||
28 | // --------------------------------------------------------------------------- | |
29 | ||
30 | export { | |
31 | logsRouter | |
32 | } | |
33 | ||
34 | // --------------------------------------------------------------------------- | |
35 | ||
36 | const auditLogNameFilter = generateLogNameFilter(AUDIT_LOG_FILENAME) | |
37 | async function getAuditLogs (req: express.Request, res: express.Response) { | |
38 | const output = await generateOutput({ | |
39 | startDateQuery: req.query.startDate, | |
40 | endDateQuery: req.query.endDate, | |
41 | level: 'audit', | |
42 | nameFilter: auditLogNameFilter | |
43 | }) | |
44 | ||
45 | return res.json(output).end() | |
46 | } | |
47 | ||
48 | const logNameFilter = generateLogNameFilter(LOG_FILENAME) | |
49 | async function getLogs (req: express.Request, res: express.Response) { | |
50 | const output = await generateOutput({ | |
51 | startDateQuery: req.query.startDate, | |
52 | endDateQuery: req.query.endDate, | |
53 | level: req.query.level || 'info', | |
54 | nameFilter: logNameFilter | |
55 | }) | |
56 | ||
57 | return res.json(output).end() | |
58 | } | |
59 | ||
60 | async function generateOutput (options: { | |
61 | startDateQuery: string | |
62 | endDateQuery?: string | |
63 | level: LogLevel | |
64 | nameFilter: RegExp | |
65 | }) { | |
66 | const { startDateQuery, level, nameFilter } = options | |
67 | ||
68 | const logFiles = await readdir(CONFIG.STORAGE.LOG_DIR) | |
69 | const sortedLogFiles = await mtimeSortFilesDesc(logFiles, CONFIG.STORAGE.LOG_DIR) | |
70 | let currentSize = 0 | |
71 | ||
72 | const startDate = new Date(startDateQuery) | |
73 | const endDate = options.endDateQuery ? new Date(options.endDateQuery) : new Date() | |
74 | ||
75 | let output: string[] = [] | |
76 | ||
77 | for (const meta of sortedLogFiles) { | |
78 | if (nameFilter.exec(meta.file) === null) continue | |
79 | ||
80 | const path = join(CONFIG.STORAGE.LOG_DIR, meta.file) | |
81 | logger.debug('Opening %s to fetch logs.', path) | |
82 | ||
83 | const result = await getOutputFromFile(path, startDate, endDate, level, currentSize) | |
84 | if (!result.output) break | |
85 | ||
86 | output = result.output.concat(output) | |
87 | currentSize = result.currentSize | |
88 | ||
89 | if (currentSize > MAX_LOGS_OUTPUT_CHARACTERS || (result.logTime && result.logTime < startDate.getTime())) break | |
90 | } | |
91 | ||
92 | return output | |
93 | } | |
94 | ||
95 | async function getOutputFromFile (path: string, startDate: Date, endDate: Date, level: LogLevel, currentSize: number) { | |
96 | const startTime = startDate.getTime() | |
97 | const endTime = endDate.getTime() | |
98 | let logTime: number | |
99 | ||
100 | const logsLevel: { [ id in LogLevel ]: number } = { | |
101 | audit: -1, | |
102 | debug: 0, | |
103 | info: 1, | |
104 | warn: 2, | |
105 | error: 3 | |
106 | } | |
107 | ||
108 | const content = await readFile(path) | |
109 | const lines = content.toString().split('\n') | |
110 | const output: any[] = [] | |
111 | ||
112 | for (let i = lines.length - 1; i >= 0; i--) { | |
113 | const line = lines[i] | |
114 | let log: any | |
115 | ||
116 | try { | |
117 | log = JSON.parse(line) | |
118 | } catch { | |
119 | // Maybe there a multiple \n at the end of the file | |
120 | continue | |
121 | } | |
122 | ||
123 | logTime = new Date(log.timestamp).getTime() | |
124 | if (logTime >= startTime && logTime <= endTime && logsLevel[log.level] >= logsLevel[level]) { | |
125 | output.push(log) | |
126 | ||
127 | currentSize += line.length | |
128 | ||
129 | if (currentSize > MAX_LOGS_OUTPUT_CHARACTERS) break | |
130 | } else if (logTime < startTime) { | |
131 | break | |
132 | } | |
133 | } | |
134 | ||
135 | return { currentSize, output: output.reverse(), logTime } | |
136 | } | |
137 | ||
138 | function generateLogNameFilter (baseName: string) { | |
139 | return new RegExp('^' + baseName.replace(/\.log$/, '') + '\\d*.log$') | |
140 | } |