Move logging options to a struct
This commit is contained in:
		
							parent
							
								
									f7b28cb1d3
								
							
						
					
					
						commit
						3afcadae76
					
				|  | @ -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"` | ||||||
|  | } | ||||||
|  | @ -61,6 +61,7 @@ type Options struct { | ||||||
| 
 | 
 | ||||||
| 	Cookie  CookieOptions  `cfg:",squash"` | 	Cookie  CookieOptions  `cfg:",squash"` | ||||||
| 	Session SessionOptions `cfg:",squash"` | 	Session SessionOptions `cfg:",squash"` | ||||||
|  | 	Logging Logging        `cfg:",squash"` | ||||||
| 
 | 
 | ||||||
| 	Upstreams                     []string      `flag:"upstream" cfg:"upstreams"` | 	Upstreams                     []string      `flag:"upstream" cfg:"upstreams"` | ||||||
| 	SkipAuthRegex                 []string      `flag:"skip-auth-regex" cfg:"skip_auth_regex"` | 	SkipAuthRegex                 []string      `flag:"skip-auth-regex" cfg:"skip_auth_regex"` | ||||||
|  | @ -101,21 +102,6 @@ type Options struct { | ||||||
| 	ApprovalPrompt                     string `flag:"approval-prompt" cfg:"approval_prompt"` // Deprecated by OIDC 1.0
 | 	ApprovalPrompt                     string `flag:"approval-prompt" cfg:"approval_prompt"` // Deprecated by OIDC 1.0
 | ||||||
| 	UserIDClaim                        string `flag:"user-id-claim" cfg:"user_id_claim"` | 	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"` | 	SignatureKey    string `flag:"signature-key" cfg:"signature_key"` | ||||||
| 	AcrValues       string `flag:"acr-values" cfg:"acr_values"` | 	AcrValues       string `flag:"acr-values" cfg:"acr_values"` | ||||||
| 	JWTKey          string `flag:"jwt-key" cfg:"jwt_key"` | 	JWTKey          string `flag:"jwt-key" cfg:"jwt_key"` | ||||||
|  | @ -197,20 +183,24 @@ func NewOptions() *Options { | ||||||
| 		UserIDClaim:                      "email", | 		UserIDClaim:                      "email", | ||||||
| 		InsecureOIDCAllowUnverifiedEmail: false, | 		InsecureOIDCAllowUnverifiedEmail: false, | ||||||
| 		SkipOIDCDiscovery:                false, | 		SkipOIDCDiscovery:                false, | ||||||
| 		LoggingFilename:                  "", | 		Logging: Logging{ | ||||||
| 		LoggingMaxSize:                   100, | 			ExcludePaths:    "", | ||||||
| 		LoggingMaxAge:                    7, | 			LocalTime:       true, | ||||||
| 		LoggingMaxBackups:                0, | 			SilencePing:     false, | ||||||
| 		LoggingLocalTime:                 true, | 			AuthEnabled:     true, | ||||||
| 		LoggingCompress:                  false, | 			AuthFormat:      logger.DefaultAuthLoggingFormat, | ||||||
| 		ExcludeLoggingPaths:              "", | 			RequestEnabled:  true, | ||||||
| 		SilencePingLogging:               false, | 			RequestFormat:   logger.DefaultRequestLoggingFormat, | ||||||
| 		StandardLogging:                  true, | 			StandardEnabled: true, | ||||||
| 		StandardLoggingFormat:            logger.DefaultStandardLoggingFormat, | 			StandardFormat:  logger.DefaultStandardLoggingFormat, | ||||||
| 		RequestLogging:                   true, | 			File: LogFileOptions{ | ||||||
| 		RequestLoggingFormat:             logger.DefaultRequestLoggingFormat, | 				Filename:   "", | ||||||
| 		AuthLogging:                      true, | 				MaxSize:    100, | ||||||
| 		AuthLoggingFormat:                logger.DefaultAuthLoggingFormat, | 				MaxAge:     7, | ||||||
|  | 				MaxBackups: 0, | ||||||
|  | 				Compress:   false, | ||||||
|  | 			}, | ||||||
|  | 		}, | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -455,55 +455,55 @@ func validateCookieName(o *options.Options, msgs []string) []string { | ||||||
| 
 | 
 | ||||||
| func setupLogger(o *options.Options, msgs []string) []string { | func setupLogger(o *options.Options, msgs []string) []string { | ||||||
| 	// Setup the log file
 | 	// Setup the log file
 | ||||||
| 	if len(o.LoggingFilename) > 0 { | 	if len(o.Logging.File.Filename) > 0 { | ||||||
| 		// Validate that the file/dir can be written
 | 		// 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 err != nil { | ||||||
| 			if os.IsPermission(err) { | 			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() | 		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{ | 		logWriter := &lumberjack.Logger{ | ||||||
| 			Filename:   o.LoggingFilename, | 			Filename:   o.Logging.File.Filename, | ||||||
| 			MaxSize:    o.LoggingMaxSize, // megabytes
 | 			MaxSize:    o.Logging.File.MaxSize, // megabytes
 | ||||||
| 			MaxAge:     o.LoggingMaxAge,  // days
 | 			MaxAge:     o.Logging.File.MaxAge,  // days
 | ||||||
| 			MaxBackups: o.LoggingMaxBackups, | 			MaxBackups: o.Logging.File.MaxBackups, | ||||||
| 			LocalTime:  o.LoggingLocalTime, | 			LocalTime:  o.Logging.LocalTime, | ||||||
| 			Compress:   o.LoggingCompress, | 			Compress:   o.Logging.File.Compress, | ||||||
| 		} | 		} | ||||||
| 
 | 
 | ||||||
| 		logger.SetOutput(logWriter) | 		logger.SetOutput(logWriter) | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	// Supply a sanity warning to the logger if all logging is disabled
 | 	// 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.") | 		logger.Print("Warning: Logging disabled. No further logs will be shown.") | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	// Pass configuration values to the standard logger
 | 	// Pass configuration values to the standard logger
 | ||||||
| 	logger.SetStandardEnabled(o.StandardLogging) | 	logger.SetStandardEnabled(o.Logging.StandardEnabled) | ||||||
| 	logger.SetAuthEnabled(o.AuthLogging) | 	logger.SetAuthEnabled(o.Logging.AuthEnabled) | ||||||
| 	logger.SetReqEnabled(o.RequestLogging) | 	logger.SetReqEnabled(o.Logging.RequestEnabled) | ||||||
| 	logger.SetStandardTemplate(o.StandardLoggingFormat) | 	logger.SetStandardTemplate(o.Logging.StandardFormat) | ||||||
| 	logger.SetAuthTemplate(o.AuthLoggingFormat) | 	logger.SetAuthTemplate(o.Logging.AuthFormat) | ||||||
| 	logger.SetReqTemplate(o.RequestLoggingFormat) | 	logger.SetReqTemplate(o.Logging.RequestFormat) | ||||||
| 	logger.SetGetClientFunc(func(r *http.Request) string { | 	logger.SetGetClientFunc(func(r *http.Request) string { | ||||||
| 		return ip.GetClientString(o.GetRealClientIPParser(), r, false) | 		return ip.GetClientString(o.GetRealClientIPParser(), r, false) | ||||||
| 	}) | 	}) | ||||||
| 
 | 
 | ||||||
| 	excludePaths := make([]string, 0) | 	excludePaths := make([]string, 0) | ||||||
| 	excludePaths = append(excludePaths, strings.Split(o.ExcludeLoggingPaths, ",")...) | 	excludePaths = append(excludePaths, strings.Split(o.Logging.ExcludePaths, ",")...) | ||||||
| 	if o.SilencePingLogging { | 	if o.Logging.SilencePing { | ||||||
| 		excludePaths = append(excludePaths, o.PingPath) | 		excludePaths = append(excludePaths, o.PingPath) | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	logger.SetExcludePaths(excludePaths) | 	logger.SetExcludePaths(excludePaths) | ||||||
| 
 | 
 | ||||||
| 	if !o.LoggingLocalTime { | 	if !o.Logging.LocalTime { | ||||||
| 		logger.SetFlags(logger.Flags() | logger.LUTC) | 		logger.SetFlags(logger.Flags() | logger.LUTC) | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue