]>
Commit | Line | Data |
---|---|---|
1 | import * as express from 'express' | |
2 | import { UserRight } from '../../../../shared/models/users' | |
3 | import { asyncMiddleware, authenticate, ensureUserHasRight } from '../../../middlewares' | |
4 | import { mtimeSortFilesDesc } from '../../../../shared/core-utils/logs/logs' | |
5 | import { readdir, readFile } from 'fs-extra' | |
6 | import { MAX_LOGS_OUTPUT_CHARACTERS } from '../../../initializers/constants' | |
7 | import { join } from 'path' | |
8 | import { getLogsValidator } from '../../../middlewares/validators/logs' | |
9 | import { LogLevel } from '../../../../shared/models/server/log-level.type' | |
10 | import { CONFIG } from '../../../initializers/config' | |
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 | // --------------------------------------------------------------------------- | |
22 | ||
23 | export { | |
24 | logsRouter | |
25 | } | |
26 | ||
27 | // --------------------------------------------------------------------------- | |
28 | ||
29 | async function getLogs (req: express.Request, res: express.Response) { | |
30 | const logFiles = await readdir(CONFIG.STORAGE.LOG_DIR) | |
31 | const sortedLogFiles = await mtimeSortFilesDesc(logFiles, CONFIG.STORAGE.LOG_DIR) | |
32 | let currentSize = 0 | |
33 | ||
34 | const startDate = new Date(req.query.startDate) | |
35 | const endDate = req.query.endDate ? new Date(req.query.endDate) : new Date() | |
36 | const level: LogLevel = req.query.level || 'info' | |
37 | ||
38 | let output: string[] = [] | |
39 | ||
40 | for (const meta of sortedLogFiles) { | |
41 | const path = join(CONFIG.STORAGE.LOG_DIR, meta.file) | |
42 | ||
43 | const result = await getOutputFromFile(path, startDate, endDate, level, currentSize) | |
44 | if (!result.output) break | |
45 | ||
46 | output = result.output.concat(output) | |
47 | currentSize = result.currentSize | |
48 | ||
49 | if (currentSize > MAX_LOGS_OUTPUT_CHARACTERS || (result.logTime && result.logTime < startDate.getTime())) break | |
50 | } | |
51 | ||
52 | return res.json(output).end() | |
53 | } | |
54 | ||
55 | async function getOutputFromFile (path: string, startDate: Date, endDate: Date, level: LogLevel, currentSize: number) { | |
56 | const startTime = startDate.getTime() | |
57 | const endTime = endDate.getTime() | |
58 | let logTime: number | |
59 | ||
60 | const logsLevel: { [ id in LogLevel ]: number } = { | |
61 | debug: 0, | |
62 | info: 1, | |
63 | warn: 2, | |
64 | error: 3 | |
65 | } | |
66 | ||
67 | const content = await readFile(path) | |
68 | const lines = content.toString().split('\n') | |
69 | const output: any[] = [] | |
70 | ||
71 | for (let i = lines.length - 1; i >= 0; i--) { | |
72 | const line = lines[ i ] | |
73 | let log: any | |
74 | ||
75 | try { | |
76 | log = JSON.parse(line) | |
77 | } catch { | |
78 | // Maybe there a multiple \n at the end of the file | |
79 | continue | |
80 | } | |
81 | ||
82 | logTime = new Date(log.timestamp).getTime() | |
83 | if (logTime >= startTime && logTime <= endTime && logsLevel[ log.level ] >= logsLevel[ level ]) { | |
84 | output.push(log) | |
85 | ||
86 | currentSize += line.length | |
87 | ||
88 | if (currentSize > MAX_LOGS_OUTPUT_CHARACTERS) break | |
89 | } else if (logTime < startTime) { | |
90 | break | |
91 | } | |
92 | } | |
93 | ||
94 | return { currentSize, output: output.reverse(), logTime } | |
95 | } |