diff options
author | Chocobozzz <me@florianbigard.com> | 2019-04-10 15:26:33 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2019-04-10 16:38:32 +0200 |
commit | fd8710b897a67518d3a61c319e54b6a65ba443ef (patch) | |
tree | d9953b7e0bb4e5a119c872ab21021f4c1ab33bea /server/controllers | |
parent | 31b6ddf86652502e0c96d77fa10861ce4af11aa4 (diff) | |
download | PeerTube-fd8710b897a67518d3a61c319e54b6a65ba443ef.tar.gz PeerTube-fd8710b897a67518d3a61c319e54b6a65ba443ef.tar.zst PeerTube-fd8710b897a67518d3a61c319e54b6a65ba443ef.zip |
Add logs endpoint
Diffstat (limited to 'server/controllers')
-rw-r--r-- | server/controllers/api/server/index.ts | 2 | ||||
-rw-r--r-- | server/controllers/api/server/logs.ts | 90 |
2 files changed, 92 insertions, 0 deletions
diff --git a/server/controllers/api/server/index.ts b/server/controllers/api/server/index.ts index 814248e5f..de09588df 100644 --- a/server/controllers/api/server/index.ts +++ b/server/controllers/api/server/index.ts | |||
@@ -4,6 +4,7 @@ import { statsRouter } from './stats' | |||
4 | import { serverRedundancyRouter } from './redundancy' | 4 | import { serverRedundancyRouter } from './redundancy' |
5 | import { serverBlocklistRouter } from './server-blocklist' | 5 | import { serverBlocklistRouter } from './server-blocklist' |
6 | import { contactRouter } from './contact' | 6 | import { contactRouter } from './contact' |
7 | import { logsRouter } from './logs' | ||
7 | 8 | ||
8 | const serverRouter = express.Router() | 9 | const serverRouter = express.Router() |
9 | 10 | ||
@@ -12,6 +13,7 @@ serverRouter.use('/', serverRedundancyRouter) | |||
12 | serverRouter.use('/', statsRouter) | 13 | serverRouter.use('/', statsRouter) |
13 | serverRouter.use('/', serverBlocklistRouter) | 14 | serverRouter.use('/', serverBlocklistRouter) |
14 | serverRouter.use('/', contactRouter) | 15 | serverRouter.use('/', contactRouter) |
16 | serverRouter.use('/', logsRouter) | ||
15 | 17 | ||
16 | // --------------------------------------------------------------------------- | 18 | // --------------------------------------------------------------------------- |
17 | 19 | ||
diff --git a/server/controllers/api/server/logs.ts b/server/controllers/api/server/logs.ts new file mode 100644 index 000000000..c551c67e3 --- /dev/null +++ b/server/controllers/api/server/logs.ts | |||
@@ -0,0 +1,90 @@ | |||
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/utils/logs/logs' | ||
5 | import { readdir } from 'fs-extra' | ||
6 | import { CONFIG, MAX_LOGS_OUTPUT_CHARACTERS } from '../../../initializers' | ||
7 | import { createInterface } from 'readline' | ||
8 | import { createReadStream } from 'fs' | ||
9 | import { join } from 'path' | ||
10 | import { getLogsValidator } from '../../../middlewares/validators/logs' | ||
11 | import { LogLevel } from '../../../../shared/models/server/log-level.type' | ||
12 | |||
13 | const logsRouter = express.Router() | ||
14 | |||
15 | logsRouter.get('/logs', | ||
16 | authenticate, | ||
17 | ensureUserHasRight(UserRight.MANAGE_LOGS), | ||
18 | getLogsValidator, | ||
19 | asyncMiddleware(getLogs) | ||
20 | ) | ||
21 | |||
22 | // --------------------------------------------------------------------------- | ||
23 | |||
24 | export { | ||
25 | logsRouter | ||
26 | } | ||
27 | |||
28 | // --------------------------------------------------------------------------- | ||
29 | |||
30 | async function getLogs (req: express.Request, res: express.Response) { | ||
31 | const logFiles = await readdir(CONFIG.STORAGE.LOG_DIR) | ||
32 | const sortedLogFiles = await mtimeSortFilesDesc(logFiles, CONFIG.STORAGE.LOG_DIR) | ||
33 | let currentSize = 0 | ||
34 | |||
35 | const startDate = new Date(req.query.startDate) | ||
36 | const endDate = req.query.endDate ? new Date(req.query.endDate) : new Date() | ||
37 | const level: LogLevel = req.query.level || 'info' | ||
38 | |||
39 | let output = '' | ||
40 | |||
41 | for (const meta of sortedLogFiles) { | ||
42 | const path = join(CONFIG.STORAGE.LOG_DIR, meta.file) | ||
43 | |||
44 | const result = await getOutputFromFile(path, startDate, endDate, level, currentSize) | ||
45 | if (!result.output) break | ||
46 | |||
47 | output = output + result.output | ||
48 | currentSize = result.currentSize | ||
49 | |||
50 | if (currentSize > MAX_LOGS_OUTPUT_CHARACTERS) break | ||
51 | } | ||
52 | |||
53 | return res.json(output).end() | ||
54 | } | ||
55 | |||
56 | function getOutputFromFile (path: string, startDate: Date, endDate: Date, level: LogLevel, currentSize: number) { | ||
57 | const startTime = startDate.getTime() | ||
58 | const endTime = endDate.getTime() | ||
59 | |||
60 | const logsLevel: { [ id in LogLevel ]: number } = { | ||
61 | debug: 0, | ||
62 | info: 1, | ||
63 | warn: 2, | ||
64 | error: 3 | ||
65 | } | ||
66 | |||
67 | return new Promise<{ output: string, currentSize: number }>(res => { | ||
68 | const stream = createReadStream(path) | ||
69 | let output = '' | ||
70 | |||
71 | stream.once('close', () => res({ output, currentSize })) | ||
72 | |||
73 | const rl = createInterface({ | ||
74 | input: stream | ||
75 | }) | ||
76 | |||
77 | rl.on('line', line => { | ||
78 | const log = JSON.parse(line) | ||
79 | |||
80 | const logTime = new Date(log.timestamp).getTime() | ||
81 | if (logTime >= startTime && logTime <= endTime && logsLevel[log.level] >= logsLevel[level]) { | ||
82 | output += line | ||
83 | |||
84 | currentSize += line.length | ||
85 | |||
86 | if (currentSize > MAX_LOGS_OUTPUT_CHARACTERS) stream.close() | ||
87 | } | ||
88 | }) | ||
89 | }) | ||
90 | } | ||