Skip to content

Commit

Permalink
Move logging options to a struct
Browse files Browse the repository at this point in the history
  • Loading branch information
JoelSpeed committed May 31, 2020
1 parent f7b28cb commit 3afcada
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 55 deletions.
24 changes: 24 additions & 0 deletions pkg/apis/options/logging.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package options

// Logging contains all options required for configuring the logging
type Logging struct {
AuthEnabled bool `flag:"auth-logging" cfg:"auth_logging"`
AuthFormat string `flag:"auth-logging-format" cfg:"auth_logging_format"`
RequestEnabled bool `flag:"request-logging" cfg:"request_logging"`
RequestFormat string `flag:"request-logging-format" cfg:"request_logging_format"`
StandardEnabled bool `flag:"standard-logging" cfg:"standard_logging"`
StandardFormat string `flag:"standard-logging-format" cfg:"standard_logging_format"`
ExcludePaths string `flag:"exclude-logging-paths" cfg:"exclude_logging_paths"`
LocalTime bool `flag:"logging-local-time" cfg:"logging_local_time"`
SilencePing bool `flag:"silence-ping-logging" cfg:"silence_ping_logging"`
File LogFileOptions `cfg:",squash"`
}

// LogFileOptions contains options for configuring logging to a file
type LogFileOptions struct {
Filename string `flag:"logging-filename" cfg:"logging_filename"`
MaxSize int `flag:"logging-max-size" cfg:"logging_max_size"`
MaxAge int `flag:"logging-max-age" cfg:"logging_max_age"`
MaxBackups int `flag:"logging-max-backups" cfg:"logging_max_backups"`
Compress bool `flag:"logging-compress" cfg:"logging_compress"`
}
60 changes: 25 additions & 35 deletions pkg/apis/options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ type Options struct {

Cookie CookieOptions `cfg:",squash"`
Session SessionOptions `cfg:",squash"`
Logging Logging `cfg:",squash"`

Upstreams []string `flag:"upstream" cfg:"upstreams"`
SkipAuthRegex []string `flag:"skip-auth-regex" cfg:"skip_auth_regex"`
Expand Down Expand Up @@ -101,27 +102,12 @@ type Options struct {
ApprovalPrompt string `flag:"approval-prompt" cfg:"approval_prompt"` // Deprecated by OIDC 1.0
UserIDClaim string `flag:"user-id-claim" cfg:"user_id_claim"`

// Configuration values for logging
LoggingFilename string `flag:"logging-filename" cfg:"logging_filename"`
LoggingMaxSize int `flag:"logging-max-size" cfg:"logging_max_size"`
LoggingMaxAge int `flag:"logging-max-age" cfg:"logging_max_age"`
LoggingMaxBackups int `flag:"logging-max-backups" cfg:"logging_max_backups"`
LoggingLocalTime bool `flag:"logging-local-time" cfg:"logging_local_time"`
LoggingCompress bool `flag:"logging-compress" cfg:"logging_compress"`
StandardLogging bool `flag:"standard-logging" cfg:"standard_logging"`
StandardLoggingFormat string `flag:"standard-logging-format" cfg:"standard_logging_format"`
RequestLogging bool `flag:"request-logging" cfg:"request_logging"`
RequestLoggingFormat string `flag:"request-logging-format" cfg:"request_logging_format"`
ExcludeLoggingPaths string `flag:"exclude-logging-paths" cfg:"exclude_logging_paths"`
SilencePingLogging bool `flag:"silence-ping-logging" cfg:"silence_ping_logging"`
AuthLogging bool `flag:"auth-logging" cfg:"auth_logging"`
AuthLoggingFormat string `flag:"auth-logging-format" cfg:"auth_logging_format"`
SignatureKey string `flag:"signature-key" cfg:"signature_key"`
AcrValues string `flag:"acr-values" cfg:"acr_values"`
JWTKey string `flag:"jwt-key" cfg:"jwt_key"`
JWTKeyFile string `flag:"jwt-key-file" cfg:"jwt_key_file"`
PubJWKURL string `flag:"pubjwk-url" cfg:"pubjwk_url"`
GCPHealthChecks bool `flag:"gcp-healthchecks" cfg:"gcp_healthchecks"`
SignatureKey string `flag:"signature-key" cfg:"signature_key"`
AcrValues string `flag:"acr-values" cfg:"acr_values"`
JWTKey string `flag:"jwt-key" cfg:"jwt_key"`
JWTKeyFile string `flag:"jwt-key-file" cfg:"jwt_key_file"`
PubJWKURL string `flag:"pubjwk-url" cfg:"pubjwk_url"`
GCPHealthChecks bool `flag:"gcp-healthchecks" cfg:"gcp_healthchecks"`

// internal values that are set after config validation
redirectURL *url.URL
Expand Down Expand Up @@ -197,20 +183,24 @@ func NewOptions() *Options {
UserIDClaim: "email",
InsecureOIDCAllowUnverifiedEmail: false,
SkipOIDCDiscovery: false,
LoggingFilename: "",
LoggingMaxSize: 100,
LoggingMaxAge: 7,
LoggingMaxBackups: 0,
LoggingLocalTime: true,
LoggingCompress: false,
ExcludeLoggingPaths: "",
SilencePingLogging: false,
StandardLogging: true,
StandardLoggingFormat: logger.DefaultStandardLoggingFormat,
RequestLogging: true,
RequestLoggingFormat: logger.DefaultRequestLoggingFormat,
AuthLogging: true,
AuthLoggingFormat: logger.DefaultAuthLoggingFormat,
Logging: Logging{
ExcludePaths: "",
LocalTime: true,
SilencePing: false,
AuthEnabled: true,
AuthFormat: logger.DefaultAuthLoggingFormat,
RequestEnabled: true,
RequestFormat: logger.DefaultRequestLoggingFormat,
StandardEnabled: true,
StandardFormat: logger.DefaultStandardLoggingFormat,
File: LogFileOptions{
Filename: "",
MaxSize: 100,
MaxAge: 7,
MaxBackups: 0,
Compress: false,
},
},
}
}

Expand Down
40 changes: 20 additions & 20 deletions pkg/validation/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -455,55 +455,55 @@ func validateCookieName(o *options.Options, msgs []string) []string {

func setupLogger(o *options.Options, msgs []string) []string {
// Setup the log file
if len(o.LoggingFilename) > 0 {
if len(o.Logging.File.Filename) > 0 {
// Validate that the file/dir can be written
file, err := os.OpenFile(o.LoggingFilename, os.O_WRONLY|os.O_CREATE, 0666)
file, err := os.OpenFile(o.Logging.File.Filename, os.O_WRONLY|os.O_CREATE, 0666)
if err != nil {
if os.IsPermission(err) {
return append(msgs, "unable to write to log file: "+o.LoggingFilename)
return append(msgs, "unable to write to log file: "+o.Logging.File.Filename)
}
}
file.Close()

logger.Printf("Redirecting logging to file: %s", o.LoggingFilename)
logger.Printf("Redirecting logging to file: %s", o.Logging.File.Filename)

logWriter := &lumberjack.Logger{
Filename: o.LoggingFilename,
MaxSize: o.LoggingMaxSize, // megabytes
MaxAge: o.LoggingMaxAge, // days
MaxBackups: o.LoggingMaxBackups,
LocalTime: o.LoggingLocalTime,
Compress: o.LoggingCompress,
Filename: o.Logging.File.Filename,
MaxSize: o.Logging.File.MaxSize, // megabytes
MaxAge: o.Logging.File.MaxAge, // days
MaxBackups: o.Logging.File.MaxBackups,
LocalTime: o.Logging.LocalTime,
Compress: o.Logging.File.Compress,
}

logger.SetOutput(logWriter)
}

// Supply a sanity warning to the logger if all logging is disabled
if !o.StandardLogging && !o.AuthLogging && !o.RequestLogging {
if !o.Logging.StandardEnabled && !o.Logging.AuthEnabled && !o.Logging.RequestEnabled {
logger.Print("Warning: Logging disabled. No further logs will be shown.")
}

// Pass configuration values to the standard logger
logger.SetStandardEnabled(o.StandardLogging)
logger.SetAuthEnabled(o.AuthLogging)
logger.SetReqEnabled(o.RequestLogging)
logger.SetStandardTemplate(o.StandardLoggingFormat)
logger.SetAuthTemplate(o.AuthLoggingFormat)
logger.SetReqTemplate(o.RequestLoggingFormat)
logger.SetStandardEnabled(o.Logging.StandardEnabled)
logger.SetAuthEnabled(o.Logging.AuthEnabled)
logger.SetReqEnabled(o.Logging.RequestEnabled)
logger.SetStandardTemplate(o.Logging.StandardFormat)
logger.SetAuthTemplate(o.Logging.AuthFormat)
logger.SetReqTemplate(o.Logging.RequestFormat)
logger.SetGetClientFunc(func(r *http.Request) string {
return ip.GetClientString(o.GetRealClientIPParser(), r, false)
})

excludePaths := make([]string, 0)
excludePaths = append(excludePaths, strings.Split(o.ExcludeLoggingPaths, ",")...)
if o.SilencePingLogging {
excludePaths = append(excludePaths, strings.Split(o.Logging.ExcludePaths, ",")...)
if o.Logging.SilencePing {
excludePaths = append(excludePaths, o.PingPath)
}

logger.SetExcludePaths(excludePaths)

if !o.LoggingLocalTime {
if !o.Logging.LocalTime {
logger.SetFlags(logger.Flags() | logger.LUTC)
}

Expand Down

0 comments on commit 3afcada

Please sign in to comment.