github.com/kaydxh/golang@v0.0.131/pkg/logs/config.go (about) 1 /* 2 *Copyright (c) 2022, kaydxh 3 * 4 *Permission is hereby granted, free of charge, to any person obtaining a copy 5 *of this software and associated documentation files (the "Software"), to deal 6 *in the Software without restriction, including without limitation the rights 7 *to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 *copies of the Software, and to permit persons to whom the Software is 9 *furnished to do so, subject to the following conditions: 10 * 11 *The above copyright notice and this permission notice shall be included in all 12 *copies or substantial portions of the Software. 13 * 14 *THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 *IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 *FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 *AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 *LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 *OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 *SOFTWARE. 21 */ 22 package logs 23 24 import ( 25 "os" 26 "path/filepath" 27 "time" 28 29 "github.com/go-playground/validator/v10" 30 time_ "github.com/kaydxh/golang/go/time" 31 logrus_ "github.com/kaydxh/golang/pkg/logs/logrus" 32 viper_ "github.com/kaydxh/golang/pkg/viper" 33 "github.com/sirupsen/logrus" 34 "google.golang.org/protobuf/types/known/durationpb" 35 36 "github.com/spf13/viper" 37 ) 38 39 const ( 40 DefaultMaxAge = 72 * time.Hour 41 DefaultMaxCount = 72 42 DefaultRotateInterval = time.Hour 43 //100MB 44 DefaultRotateSize = 104857600 45 ) 46 47 type Config struct { 48 Proto Log 49 Validator *validator.Validate 50 opts struct { 51 // If set, overrides params below 52 viper *viper.Viper 53 } 54 } 55 56 type completedConfig struct { 57 *Config 58 completeError error 59 } 60 61 type CompletedConfig struct { 62 // Embed a private pointer that cannot be instantiated outside of this package. 63 *completedConfig 64 } 65 66 // Validate checks Config. 67 func (c *completedConfig) Validate() error { 68 return c.Validator.Struct(c) 69 } 70 71 func (c *completedConfig) Apply() error { 72 if c.completeError != nil { 73 return c.completeError 74 } 75 if c.Validator == nil { 76 c.Validator = validator.New() 77 } 78 79 return c.install() 80 } 81 82 func (c *completedConfig) install() error { 83 logrus.Infof("Installing Logs") 84 85 switch c.Proto.GetFormatter() { 86 case Log_json: 87 logrus.SetFormatter(&logrus.JSONFormatter{ 88 CallerPrettyfier: GenShortCallPrettyfier(), 89 }) 90 91 case Log_text: 92 //DisableColors set true, out format: 93 //time="2021-08-07 20:21:46.468" level=info msg="Installing WebHandler" func="options.(*CompletedServerRunOptions).Run()" file="options.go:59" 94 //DisableColors set false, out format: 95 //INFO[2021-08-07T19:53:42+08:00]options.go:59 options.(*CompletedServerRunOptions).Run() Installing WebHandler 96 97 //DisableQuote: ture, this config can format newline characters(\n) in the log message 98 logrus.SetFormatter(&logrus.TextFormatter{ 99 //ForceQuote: true, 100 DisableColors: true, 101 DisableQuote: true, 102 FullTimestamp: true, 103 TimestampFormat: time_.DefaultTimeMillFormat, 104 CallerPrettyfier: GenShortCallPrettyfier(), 105 }) 106 107 default: 108 logrus.SetFormatter(&logrus_.GlogFormatter{ 109 //ForceQuote: true, 110 DisableColors: true, 111 DisableQuote: true, 112 FullTimestamp: true, 113 TimestampFormat: time_.DefaultTimeMillFormat, 114 CallerPrettyfier: GenShortCallPrettyfier(), 115 EnableGoroutineId: c.Proto.GetEnableGoroutineId(), 116 }) 117 } 118 119 level, err := logrus.ParseLevel(c.Proto.GetLevel().String()) 120 if err != nil { 121 return err 122 } 123 logrus.SetLevel(level) 124 logrus.SetReportCaller(c.Proto.GetReportCaller()) 125 126 err = WithRotate( 127 logrus.StandardLogger(), 128 c.Proto.GetFilepath(), 129 c.Proto.GetRedirct(), 130 WithMaxAge(c.Proto.GetMaxAge().AsDuration()), 131 WithMaxCount(c.Proto.GetMaxCount()), 132 WithRotateSize(c.Proto.GetRotateSize()), 133 WithRotateInterval(c.Proto.GetRotateInterval().AsDuration()), 134 WithPrefixName(filepath.Base(os.Args[0])), 135 WithSuffixName(".log"), 136 ) 137 if err != nil { 138 return err 139 } 140 141 logrus.WithField( 142 "path", 143 c.Proto.GetFilepath(), 144 ).WithField( 145 "rotate_interval", c.Proto.GetRotateInterval().AsDuration(), 146 ).WithField( 147 "rotate_size", c.Proto.GetRotateSize(), 148 ).Infof( 149 "Installed log", 150 ) 151 152 return nil 153 } 154 155 // Complete set default ServerRunOptions. 156 func (c *Config) Complete() CompletedConfig { 157 err := c.loadViper() 158 if err != nil { 159 return CompletedConfig{&completedConfig{ 160 Config: c, 161 completeError: err, 162 }} 163 } 164 c.parseViper() 165 return CompletedConfig{&completedConfig{Config: c}} 166 } 167 168 func (c *Config) parseViper() { 169 170 } 171 172 func (c *Config) loadViper() error { 173 if c.opts.viper != nil { 174 return viper_.UnmarshalProtoMessageWithJsonPb(c.opts.viper, &c.Proto) 175 } 176 177 return nil 178 } 179 180 func NewConfig(options ...ConfigOption) *Config { 181 c := &Config{ 182 Proto: Log{ 183 Level: Log_info, 184 Formatter: Log_text, 185 MaxAge: durationpb.New(DefaultMaxAge), 186 MaxCount: DefaultMaxCount, 187 RotateInterval: durationpb.New(DefaultRotateInterval), 188 RotateSize: DefaultRotateSize, 189 190 Filepath: "./log/" + filepath.Base(os.Args[0]), 191 }, 192 } 193 194 c.ApplyOptions(options...) 195 196 return c 197 }