1
0
mirror of https://github.com/oauth2-proxy/oauth2-proxy.git synced 2025-08-08 22:46:33 +02:00

Add silence ping logging flag

Add ability to silence logging of requests to /ping endpoint, reducing
log clutter

Pros:
- Don't have to change all handlers to set/not set silent ping logging
- Don't have to duplicate `loggingHandler` (this could be preferable yet)

Cons:
- Leaking oauth2proxy logic into `package logger`
- Defining default pingPath in two locations

Alternative:
- Add generic exclude path to `logger.go` and pass in `/ping`.
This commit is contained in:
Karl Skewes
2019-05-31 20:11:28 +12:00
parent e952ab4bdf
commit ec97000169
7 changed files with 58 additions and 6 deletions

View File

@ -88,6 +88,8 @@ type Logger struct {
stdEnabled bool
authEnabled bool
reqEnabled bool
silentPing bool
pingPath string
stdLogTemplate *template.Template
authTemplate *template.Template
reqTemplate *template.Template
@ -101,6 +103,8 @@ func New(flag int) *Logger {
stdEnabled: true,
authEnabled: true,
reqEnabled: true,
silentPing: false,
pingPath: "/ping",
stdLogTemplate: template.Must(template.New("std-log").Parse(DefaultStandardLoggingFormat)),
authTemplate: template.Must(template.New("auth-log").Parse(DefaultAuthLoggingFormat)),
reqTemplate: template.Must(template.New("req-log").Parse(DefaultRequestLoggingFormat)),
@ -177,6 +181,9 @@ func (l *Logger) PrintReq(username, upstream string, req *http.Request, url url.
return
}
if url.Path == l.pingPath && l.silentPing {
return
}
duration := float64(time.Now().Sub(ts)) / float64(time.Second)
if username == "" {
@ -302,6 +309,20 @@ func (l *Logger) SetReqEnabled(e bool) {
l.reqEnabled = e
}
// SetPingPath sets the ping path.
func (l *Logger) SetPingPath(s string) {
l.mu.Lock()
defer l.mu.Unlock()
l.pingPath = s
}
// SetSilentPing disables ping request logging.
func (l *Logger) SetSilentPing(e bool) {
l.mu.Lock()
defer l.mu.Unlock()
l.silentPing = e
}
// SetStandardTemplate sets the template for standard logging.
func (l *Logger) SetStandardTemplate(t string) {
l.mu.Lock()
@ -365,6 +386,17 @@ func SetReqEnabled(e bool) {
std.SetReqEnabled(e)
}
// SetPingPath sets the healthcheck endpoint path.
// FIXME: Seems wrong to define this
func SetPingPath(s string) {
std.SetPingPath(s)
}
// SetSilentPing disables request logging for the ping endpoint.
func SetSilentPing(e bool) {
std.SetSilentPing(e)
}
// SetStandardTemplate sets the template for standard logging for
// the standard logger.
func SetStandardTemplate(t string) {