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