Skip to content

Commit

Permalink
Add ability to disable HTTP logs
Browse files Browse the repository at this point in the history
  • Loading branch information
Chocobozzz committed Oct 24, 2023
1 parent edc3ff6 commit 90db2b3
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 23 deletions.
3 changes: 3 additions & 0 deletions config/default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,9 @@ log:
log_ping_requests: 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

# Accept warn/error logs coming from the client
Expand Down
4 changes: 3 additions & 1 deletion config/production.yaml.example
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,9 @@ log:
log_ping_requests: 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

# Accept warn/error logs coming from the client
Expand Down Expand Up @@ -470,7 +473,6 @@ user:
video_quota_daily: -1
default_channel_name: 'Main $1 channel' # The placeholder $1 is used to represent the user's username


video_channels:
max_per_user: 20 # Allows each user to create up to 20 video channels.

Expand Down
9 changes: 6 additions & 3 deletions packages/tests/src/api/server/logs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()

await server.servers.ping()
Expand All @@ -122,23 +122,26 @@ describe('Test logs', function () {
const logsString = JSON.stringify(body)

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)

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()

await server.servers.ping()
await server.videos.list()

const body = await logsCommand.getLogs({ startDate: now, level: 'info' })
const logsString = JSON.stringify(body)

expect(logsString.includes('/api/v1/ping')).to.be.false
expect(logsString.includes(' HTTP/1.1"')).to.be.false
})
})

Expand Down
1 change: 1 addition & 0 deletions server/core/initializers/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ const CONFIG = {
ANONYMIZE_IP: config.get<boolean>('log.anonymize_ip'),
LOG_PING_REQUESTS: config.get<boolean>('log.log_ping_requests'),
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'),
ACCEPT_CLIENT_LOG: config.get<boolean>('log.accept_client_log')
},
Expand Down
42 changes: 23 additions & 19 deletions server/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,27 +167,31 @@ if (isTestOrDevInstance()) {
}))
}

// For the logger
token('remote-addr', (req: express.Request) => {
if (CONFIG.LOG.ANONYMIZE_IP === true || req.get('DNT') === '1') {
return anonymize(req.ip, 16, 16)
}
// HTTP logging
if (CONFIG.LOG.LOG_HTTP_REQUESTS) {
token('remote-addr', (req: express.Request) => {
if (CONFIG.LOG.ANONYMIZE_IP === true || req.get('DNT') === '1') {
return anonymize(req.ip, 16, 16)
}

return req.ip
})
token('user-agent', (req: express.Request) => {
if (req.get('DNT') === '1') {
return parse(req.get('user-agent')).family
}
return req.ip
})

return req.get('user-agent')
})
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'
}))
token('user-agent', (req: express.Request) => {
if (req.get('DNT') === '1') {
return parse(req.get('user-agent')).family
}

return req.get('user-agent')
})

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
app.use(apiFailMiddleware)
Expand Down

0 comments on commit 90db2b3

Please sign in to comment.