Add ability to disable HTTP logs

This commit is contained in:
Chocobozzz 2023-10-24 10:57:41 +02:00
parent edc3ff6085
commit 90db2b3aed
No known key found for this signature in database
GPG Key ID: 583A612D890159BE
5 changed files with 36 additions and 23 deletions

View File

@ -240,6 +240,9 @@ log:
log_ping_requests: true log_ping_requests: true
log_tracker_unknown_infohash: true log_tracker_unknown_infohash: true
# If you have many concurrent requests, you can disable HTTP requests logging to reduce PeerTube CPU load
log_http_requests: true
prettify_sql: false prettify_sql: false
# Accept warn/error logs coming from the client # Accept warn/error logs coming from the client

View File

@ -238,6 +238,9 @@ log:
log_ping_requests: true log_ping_requests: true
log_tracker_unknown_infohash: true log_tracker_unknown_infohash: true
# If you have many concurrent requests, you can disable HTTP requests logging to reduce PeerTube CPU load
log_http_requests: true
prettify_sql: false prettify_sql: false
# Accept warn/error logs coming from the client # Accept warn/error logs coming from the client
@ -470,7 +473,6 @@ user:
video_quota_daily: -1 video_quota_daily: -1
default_channel_name: 'Main $1 channel' # The placeholder $1 is used to represent the user's username default_channel_name: 'Main $1 channel' # The placeholder $1 is used to represent the user's username
video_channels: video_channels:
max_per_user: 20 # Allows each user to create up to 20 video channels. max_per_user: 20 # Allows each user to create up to 20 video channels.

View File

@ -113,7 +113,7 @@ describe('Test logs', function () {
} }
}) })
it('Should log ping requests', async function () { it('Should log ping/HTTP requests', async function () {
const now = new Date() const now = new Date()
await server.servers.ping() await server.servers.ping()
@ -122,23 +122,26 @@ describe('Test logs', function () {
const logsString = JSON.stringify(body) const logsString = JSON.stringify(body)
expect(logsString.includes('/api/v1/ping')).to.be.true expect(logsString.includes('/api/v1/ping')).to.be.true
expect(logsString.includes(' HTTP/1.1')).to.be.true
}) })
it('Should not log ping requests', async function () { it('Should not log ping/HTTP requests', async function () {
this.timeout(60000) this.timeout(60000)
await killallServers([ server ]) await killallServers([ server ])
await server.run({ log: { log_ping_requests: false } }) await server.run({ log: { log_ping_requests: false, log_http_requests: false } })
const now = new Date() const now = new Date()
await server.servers.ping() await server.servers.ping()
await server.videos.list()
const body = await logsCommand.getLogs({ startDate: now, level: 'info' }) const body = await logsCommand.getLogs({ startDate: now, level: 'info' })
const logsString = JSON.stringify(body) const logsString = JSON.stringify(body)
expect(logsString.includes('/api/v1/ping')).to.be.false expect(logsString.includes('/api/v1/ping')).to.be.false
expect(logsString.includes(' HTTP/1.1"')).to.be.false
}) })
}) })

View File

@ -220,6 +220,7 @@ const CONFIG = {
ANONYMIZE_IP: config.get<boolean>('log.anonymize_ip'), ANONYMIZE_IP: config.get<boolean>('log.anonymize_ip'),
LOG_PING_REQUESTS: config.get<boolean>('log.log_ping_requests'), LOG_PING_REQUESTS: config.get<boolean>('log.log_ping_requests'),
LOG_TRACKER_UNKNOWN_INFOHASH: config.get<boolean>('log.log_tracker_unknown_infohash'), LOG_TRACKER_UNKNOWN_INFOHASH: config.get<boolean>('log.log_tracker_unknown_infohash'),
LOG_HTTP_REQUESTS: config.get<boolean>('log.log_http_requests'),
PRETTIFY_SQL: config.get<boolean>('log.prettify_sql'), PRETTIFY_SQL: config.get<boolean>('log.prettify_sql'),
ACCEPT_CLIENT_LOG: config.get<boolean>('log.accept_client_log') ACCEPT_CLIENT_LOG: config.get<boolean>('log.accept_client_log')
}, },

View File

@ -167,27 +167,31 @@ if (isTestOrDevInstance()) {
})) }))
} }
// For the logger // HTTP logging
token('remote-addr', (req: express.Request) => { if (CONFIG.LOG.LOG_HTTP_REQUESTS) {
if (CONFIG.LOG.ANONYMIZE_IP === true || req.get('DNT') === '1') { token('remote-addr', (req: express.Request) => {
return anonymize(req.ip, 16, 16) if (CONFIG.LOG.ANONYMIZE_IP === true || req.get('DNT') === '1') {
} return anonymize(req.ip, 16, 16)
}
return req.ip return req.ip
}) })
token('user-agent', (req: express.Request) => {
if (req.get('DNT') === '1') {
return parse(req.get('user-agent')).family
}
return req.get('user-agent') token('user-agent', (req: express.Request) => {
}) if (req.get('DNT') === '1') {
app.use(morgan('combined', { return parse(req.get('user-agent')).family
stream: { }
write: (str: string) => logger.info(str.trim(), { tags: [ 'http' ] })
}, return req.get('user-agent')
skip: req => CONFIG.LOG.LOG_PING_REQUESTS === false && req.originalUrl === '/api/v1/ping' })
}))
app.use(morgan('combined', {
stream: {
write: (str: string) => logger.info(str.trim(), { tags: [ 'http' ] })
},
skip: req => CONFIG.LOG.LOG_PING_REQUESTS === false && req.originalUrl === '/api/v1/ping'
}))
}
// Add .fail() helper to response // Add .fail() helper to response
app.use(apiFailMiddleware) app.use(apiFailMiddleware)