github.com/cilium/cilium@v1.16.2/pkg/logging/hooks/file_rotation.go (about) 1 // SPDX-License-Identifier: Apache-2.0 2 // Copyright Authors of Cilium 3 4 package hooks 5 6 import ( 7 "github.com/cilium/lumberjack/v2" 8 "github.com/sirupsen/logrus" 9 ) 10 11 // FileRotationOption provides all parameters for file rotation 12 type FileRotationOption struct { 13 FileName string 14 MaxSize int 15 MaxAge int 16 MaxBackups int 17 LocalTime bool 18 Compress bool 19 } 20 21 type Option func(*FileRotationOption) 22 23 // WithMaxSize provides way to adjust maxSize (in MBs). Defaults to 24 // 100 MBs. 25 func WithMaxSize(maxSize int) Option { 26 return func(option *FileRotationOption) { 27 option.MaxSize = maxSize 28 } 29 } 30 31 // WithMaxAge provides way to adjust max age (in days). The default is 32 // not to remove old log files based on age. 33 func WithMaxAge(maxAge int) Option { 34 return func(option *FileRotationOption) { 35 option.MaxAge = maxAge 36 } 37 } 38 39 // WithMaxBackups provides way to adjust max number of backups. Defaults 40 // to retain all old log files though MaxAge may still cause them to get 41 // deleted. 42 func WithMaxBackups(MaxBackups int) Option { 43 return func(option *FileRotationOption) { 44 option.MaxBackups = MaxBackups 45 } 46 } 47 48 // EnableLocalTime is to determine if the time used for formatting the 49 // timestamps in backup files is the computer's local time. The default 50 // is to use UTC time. 51 func EnableLocalTime() Option { 52 return func(option *FileRotationOption) { 53 option.LocalTime = true 54 } 55 } 56 57 // EnableCompression is to enable old log file gzip compression. Defaults 58 // to false. 59 func EnableCompression() Option { 60 return func(option *FileRotationOption) { 61 option.Compress = true 62 } 63 } 64 65 // FileRotationLogHook stores the configuration of the hook 66 type FileRotationLogHook struct { 67 logger *lumberjack.Logger 68 } 69 70 // NewFileRotationLogHook creates a new FileRotationLogHook*/ 71 func NewFileRotationLogHook(fileName string, opts ...Option) *FileRotationLogHook { 72 options := &FileRotationOption{ 73 FileName: fileName, 74 MaxSize: 100, // MBs 75 LocalTime: false, // UTC 76 Compress: false, // no compression with gzip 77 } 78 79 for _, opt := range opts { 80 opt(options) 81 } 82 83 logger := &lumberjack.Logger{ 84 Filename: options.FileName, 85 MaxSize: options.MaxSize, 86 MaxAge: options.MaxAge, 87 MaxBackups: options.MaxBackups, 88 LocalTime: options.LocalTime, 89 Compress: options.Compress, 90 } 91 92 return &FileRotationLogHook{ 93 logger: logger, 94 } 95 } 96 97 // Fire is called when a log event is fired. 98 func (hook *FileRotationLogHook) Fire(entry *logrus.Entry) error { 99 line, err := entry.String() 100 if err != nil { 101 return err 102 } 103 104 _, err = hook.logger.Write([]byte(line)) 105 if err != nil { 106 return err 107 } 108 109 return nil 110 } 111 112 // Levels returns the available logging levels 113 func (hook *FileRotationLogHook) Levels() []logrus.Level { 114 return logrus.AllLevels 115 }