1
0
mirror of https://github.com/oauth2-proxy/oauth2-proxy.git synced 2024-11-30 09:16:52 +02:00
oauth2-proxy/pkg/validation/logging.go

62 lines
1.7 KiB
Go
Raw Normal View History

package validation
import (
"os"
"github.com/oauth2-proxy/oauth2-proxy/pkg/apis/options"
"github.com/oauth2-proxy/oauth2-proxy/pkg/logger"
"gopkg.in/natefinch/lumberjack.v2"
)
// configureLogger is responsible for configuring the logger based on the options given
2020-06-14 21:58:44 +02:00
func configureLogger(o options.Logging, msgs []string) []string {
// Setup the log file
if len(o.File.Filename) > 0 {
// Validate that the file/dir can be written
file, err := os.OpenFile(o.File.Filename, os.O_WRONLY|os.O_CREATE, 0600)
if err != nil {
if os.IsPermission(err) {
return append(msgs, "unable to write to log file: "+o.File.Filename)
}
}
err = file.Close()
if err != nil {
return append(msgs, "error closing the log file: "+o.File.Filename)
}
logger.Printf("Redirecting logging to file: %s", o.File.Filename)
logWriter := &lumberjack.Logger{
Filename: o.File.Filename,
MaxSize: o.File.MaxSize, // megabytes
MaxAge: o.File.MaxAge, // days
MaxBackups: o.File.MaxBackups,
LocalTime: o.LocalTime,
Compress: o.File.Compress,
}
logger.SetOutput(logWriter)
}
// Supply a sanity warning to the logger if all logging is disabled
if !o.StandardEnabled && !o.AuthEnabled && !o.RequestEnabled {
logger.Print("Warning: Logging disabled. No further logs will be shown.")
}
// Pass configuration values to the standard logger
logger.SetStandardEnabled(o.StandardEnabled)
logger.SetAuthEnabled(o.AuthEnabled)
logger.SetReqEnabled(o.RequestEnabled)
logger.SetStandardTemplate(o.StandardFormat)
logger.SetAuthTemplate(o.AuthFormat)
logger.SetReqTemplate(o.RequestFormat)
2020-06-14 21:58:44 +02:00
logger.SetExcludePaths(o.ExcludePaths)
if !o.LocalTime {
logger.SetFlags(logger.Flags() | logger.LUTC)
}
return msgs
}