aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/controllers/api
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2022-07-15 15:30:14 +0200
committerChocobozzz <me@florianbigard.com>2022-07-18 11:37:18 +0200
commit42b40636991b97fe818007fab19091764fc5db73 (patch)
treedb431787c06ce898d22e91ff771f795219274fc6 /server/controllers/api
parent654d4ede7fa4d0faa71e49bcfab6b65a686397b2 (diff)
downloadPeerTube-42b40636991b97fe818007fab19091764fc5db73.tar.gz
PeerTube-42b40636991b97fe818007fab19091764fc5db73.tar.zst
PeerTube-42b40636991b97fe818007fab19091764fc5db73.zip
Add ability for client to create server logs
Diffstat (limited to 'server/controllers/api')
-rw-r--r--server/controllers/api/server/logs.ts41
1 files changed, 35 insertions, 6 deletions
diff --git a/server/controllers/api/server/logs.ts b/server/controllers/api/server/logs.ts
index 8aa4b7190..ed0aa6e8e 100644
--- a/server/controllers/api/server/logs.ts
+++ b/server/controllers/api/server/logs.ts
@@ -3,15 +3,29 @@ import { readdir, readFile } from 'fs-extra'
3import { join } from 'path' 3import { join } from 'path'
4import { isArray } from '@server/helpers/custom-validators/misc' 4import { isArray } from '@server/helpers/custom-validators/misc'
5import { logger, mtimeSortFilesDesc } from '@server/helpers/logger' 5import { logger, mtimeSortFilesDesc } from '@server/helpers/logger'
6import { LogLevel } from '../../../../shared/models/server/log-level.type' 6import { pick } from '@shared/core-utils'
7import { ClientLogCreate, HttpStatusCode } from '@shared/models'
8import { ServerLogLevel } from '../../../../shared/models/server/server-log-level.type'
7import { UserRight } from '../../../../shared/models/users' 9import { UserRight } from '../../../../shared/models/users'
8import { CONFIG } from '../../../initializers/config' 10import { CONFIG } from '../../../initializers/config'
9import { AUDIT_LOG_FILENAME, LOG_FILENAME, MAX_LOGS_OUTPUT_CHARACTERS } from '../../../initializers/constants' 11import { AUDIT_LOG_FILENAME, LOG_FILENAME, MAX_LOGS_OUTPUT_CHARACTERS } from '../../../initializers/constants'
10import { asyncMiddleware, authenticate, ensureUserHasRight } from '../../../middlewares' 12import { asyncMiddleware, authenticate, buildRateLimiter, ensureUserHasRight, optionalAuthenticate } from '../../../middlewares'
11import { getAuditLogsValidator, getLogsValidator } from '../../../middlewares/validators/logs' 13import { createClientLogValidator, getAuditLogsValidator, getLogsValidator } from '../../../middlewares/validators/logs'
14
15const createClientLogRateLimiter = buildRateLimiter({
16 windowMs: CONFIG.RATES_LIMIT.RECEIVE_CLIENT_LOG.WINDOW_MS,
17 max: CONFIG.RATES_LIMIT.RECEIVE_CLIENT_LOG.MAX
18})
12 19
13const logsRouter = express.Router() 20const logsRouter = express.Router()
14 21
22logsRouter.post('/logs/client',
23 createClientLogRateLimiter,
24 optionalAuthenticate,
25 createClientLogValidator,
26 createClientLog
27)
28
15logsRouter.get('/logs', 29logsRouter.get('/logs',
16 authenticate, 30 authenticate,
17 ensureUserHasRight(UserRight.MANAGE_LOGS), 31 ensureUserHasRight(UserRight.MANAGE_LOGS),
@@ -34,6 +48,21 @@ export {
34 48
35// --------------------------------------------------------------------------- 49// ---------------------------------------------------------------------------
36 50
51function createClientLog (req: express.Request, res: express.Response) {
52 const logInfo = req.body as ClientLogCreate
53
54 const meta = {
55 tags: [ 'client' ],
56 username: res.locals.oauth?.token?.User?.username,
57
58 ...pick(logInfo, [ 'userAgent', 'stackTrace', 'meta', 'url' ])
59 }
60
61 logger.log(logInfo.level, `Client log: ${logInfo.message}`, meta)
62
63 return res.sendStatus(HttpStatusCode.NO_CONTENT_204)
64}
65
37const auditLogNameFilter = generateLogNameFilter(AUDIT_LOG_FILENAME) 66const auditLogNameFilter = generateLogNameFilter(AUDIT_LOG_FILENAME)
38async function getAuditLogs (req: express.Request, res: express.Response) { 67async function getAuditLogs (req: express.Request, res: express.Response) {
39 const output = await generateOutput({ 68 const output = await generateOutput({
@@ -63,7 +92,7 @@ async function generateOutput (options: {
63 startDateQuery: string 92 startDateQuery: string
64 endDateQuery?: string 93 endDateQuery?: string
65 94
66 level: LogLevel 95 level: ServerLogLevel
67 nameFilter: RegExp 96 nameFilter: RegExp
68 tagsOneOf?: string[] 97 tagsOneOf?: string[]
69}) { 98}) {
@@ -104,7 +133,7 @@ async function getOutputFromFile (options: {
104 path: string 133 path: string
105 startDate: Date 134 startDate: Date
106 endDate: Date 135 endDate: Date
107 level: LogLevel 136 level: ServerLogLevel
108 currentSize: number 137 currentSize: number
109 tagsOneOf: Set<string> 138 tagsOneOf: Set<string>
110}) { 139}) {
@@ -116,7 +145,7 @@ async function getOutputFromFile (options: {
116 145
117 let logTime: number 146 let logTime: number
118 147
119 const logsLevel: { [ id in LogLevel ]: number } = { 148 const logsLevel: { [ id in ServerLogLevel ]: number } = {
120 audit: -1, 149 audit: -1,
121 debug: 0, 150 debug: 0,
122 info: 1, 151 info: 1,