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