configureLogger is responsible for configuring the logger based on the options given
(o options.Logging, msgs []string)
| 10 | |
| 11 | // configureLogger is responsible for configuring the logger based on the options given |
| 12 | func 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, 0600) |
| 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 | err = file.Close() |
| 23 | if err != nil { |
| 24 | return append(msgs, "error closing the log file: "+o.File.Filename) |
| 25 | } |
| 26 | |
| 27 | logger.Printf("Redirecting logging to file: %s", o.File.Filename) |
| 28 | |
| 29 | logWriter := &lumberjack.Logger{ |
| 30 | Filename: o.File.Filename, |
| 31 | MaxSize: o.File.MaxSize, // megabytes |
| 32 | MaxAge: o.File.MaxAge, // days |
| 33 | MaxBackups: o.File.MaxBackups, |
| 34 | LocalTime: o.LocalTime, |
| 35 | Compress: o.File.Compress, |
| 36 | } |
| 37 | |
| 38 | logger.SetOutput(logWriter) |
| 39 | } |
| 40 | |
| 41 | // Supply a sanity warning to the logger if all logging is disabled |
| 42 | if !o.StandardEnabled && !o.AuthEnabled && !o.RequestEnabled { |
| 43 | logger.Error("Warning: Logging disabled. No further logs will be shown.") |
| 44 | } |
| 45 | |
| 46 | // Pass configuration values to the standard logger |
| 47 | logger.SetStandardEnabled(o.StandardEnabled) |
| 48 | logger.SetErrToInfo(o.ErrToInfo) |
| 49 | logger.SetAuthEnabled(o.AuthEnabled) |
| 50 | logger.SetReqEnabled(o.RequestEnabled) |
| 51 | logger.SetStandardTemplate(o.StandardFormat) |
| 52 | logger.SetAuthTemplate(o.AuthFormat) |
| 53 | logger.SetReqTemplate(o.RequestFormat) |
| 54 | |
| 55 | logger.SetExcludePaths(o.ExcludePaths) |
| 56 | |
| 57 | if !o.LocalTime { |
| 58 | logger.SetFlags(logger.Flags() | logger.LUTC) |
| 59 | } |
| 60 | |
| 61 | return msgs |
| 62 | } |
no test coverage detected