git.iko.soy/typeable/oauth2-proxy
on 71dc702 help sign in
58 lines 1.7 kB history raw
1package validation
3import (
4 "os"
6 "github.com/oauth2-proxy/oauth2-proxy/pkg/apis/options"
7 "github.com/oauth2-proxy/oauth2-proxy/pkg/logger"
8 "gopkg.in/natefinch/lumberjack.v2"
9)
11// configureLogger is responsible for configuring the logger based on the options given
12func configureLogger(o options.Logging, msgs []string) []string {
13 // Setup the log file
14 if len(o.File.Filename) > 0 {
15 // Validate that the file/dir can be written
16 file, err := os.OpenFile(o.File.Filename, os.O_WRONLY|os.O_CREATE, 0666)
17 if err != nil {
18 if os.IsPermission(err) {
19 return append(msgs, "unable to write to log file: "+o.File.Filename)
20 }
21 }
22 file.Close()
24 logger.Printf("Redirecting logging to file: %s", o.File.Filename)
26 logWriter := &lumberjack.Logger{
27 Filename: o.File.Filename,
28 MaxSize: o.File.MaxSize, // megabytes
29 MaxAge: o.File.MaxAge, // days
30 MaxBackups: o.File.MaxBackups,
31 LocalTime: o.LocalTime,
32 Compress: o.File.Compress,
33 }
35 logger.SetOutput(logWriter)
36 }
38 // Supply a sanity warning to the logger if all logging is disabled
39 if !o.StandardEnabled && !o.AuthEnabled && !o.RequestEnabled {
40 logger.Print("Warning: Logging disabled. No further logs will be shown.")
41 }
43 // Pass configuration values to the standard logger
44 logger.SetStandardEnabled(o.StandardEnabled)
45 logger.SetAuthEnabled(o.AuthEnabled)
46 logger.SetReqEnabled(o.RequestEnabled)
47 logger.SetStandardTemplate(o.StandardFormat)
48 logger.SetAuthTemplate(o.AuthFormat)
49 logger.SetReqTemplate(o.RequestFormat)
51 logger.SetExcludePaths(o.ExcludePaths)
53 if !o.LocalTime {
54 logger.SetFlags(logger.Flags() | logger.LUTC)
55 }
57 return msgs