-
Notifications
You must be signed in to change notification settings - Fork 0
/
toolgo.go
152 lines (133 loc) · 3.28 KB
/
toolgo.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package toolgo
import (
"fmt"
"github.com/fsnotify/fsnotify"
rotatelogs "github.com/lestrrat-go/file-rotatelogs"
"github.com/lngwu11/toolgo/loggo"
"github.com/spf13/viper"
"os"
"path"
"strings"
"time"
)
type Conf struct {
ConfigFile ConfigFileConf `mapstructure:"-"`
Logger LoggerConf
Config interface{}
}
type LoggerConf struct {
LogLevel string
FilePath string
FileMaxAge int // 文件最大保存时间(天)
FileRotationTime int // 日志切割时间间隔(小时)
}
type ConfigFileConf struct {
FilePath string
}
var defaultConf = Conf{
Logger: LoggerConf{
LogLevel: "ERROR",
FilePath: "logs/toolgo.log",
FileMaxAge: 30,
FileRotationTime: 24,
},
ConfigFile: ConfigFileConf{
FilePath: "conf/conf.toml",
},
}
var cfg = &defaultConf
func (c *Conf) Init() {
err := initConf(c)
if err != nil {
panic(err)
}
}
func (c *Conf) SetConfigFilePath(path string) *Conf {
c.ConfigFile.FilePath = path
return c
}
func (c *Conf) SetLogFilePath(path string) *Conf {
c.Logger.FilePath = path
return c
}
func (c *Conf) SetConfig(config interface{}) *Conf {
c.Config = config
return c
}
func initLog() (err error) {
loggo.ResetLogging()
err = loggo.RegisterWriter(os.Stdout.Name(), loggo.NewSimpleWriter(os.Stdout, nil))
if err != nil {
return
}
filePath := cfg.Logger.FilePath
if filePath != "" {
//获取文件后缀
fileSuffix := path.Ext(filePath)
//获取不带后缀的文件名
filenameOnly := strings.TrimSuffix(filePath, fileSuffix)
logWriter, _ := rotatelogs.New(
filenameOnly+".%Y%m%d"+fileSuffix,
//生成软链 指向最新的日志文件
rotatelogs.WithLinkName(filePath),
//文件最大保存时间
rotatelogs.WithMaxAge(time.Duration(cfg.Logger.FileMaxAge)*24*time.Hour),
//设置日志切割时间间隔
rotatelogs.WithRotationTime(time.Duration(cfg.Logger.FileRotationTime)*time.Hour),
)
err = loggo.RegisterWriter(path.Base(filePath), loggo.NewSimpleWriter(logWriter, loggo.DefaultFormatter))
if err != nil {
return
}
}
return loggo.ConfigureLoggers(cfg.Logger.LogLevel)
}
func initConf(c *Conf) (err error) {
filePath := c.ConfigFile.FilePath
if len(filePath) > 0 {
// 读取配置文件并转化成对应的结构体
viper.SetConfigFile(filePath)
if err = viper.ReadInConfig(); err != nil {
return
}
if err = viper.Unmarshal(cfg); err != nil {
return
}
//fmt.Printf("%+v\n", cfg.Config)
viper.WatchConfig()
viper.OnConfigChange(func(e fsnotify.Event) {
// 配置文件发生变更之后会调用的回调函数
_, _ = fmt.Fprintf(os.Stderr, "%s viper.OnConfigChange:%s", time.Now().Format("2006-01-02 15:04:05.000"), e.Name)
})
}
return initLog()
}
func init() {
_ = initConf(cfg)
}
// Deprecated: use Default instead
func GetDefaultConf() *Conf {
return cfg
}
func Default() *Conf {
return cfg
}
func InitLog(level, filePath string) (err error) {
loggo.ResetLogging()
err = loggo.RegisterWriter(os.Stdout.Name(), loggo.NewSimpleWriter(os.Stdout, nil))
if err != nil {
return
}
if filePath != "" {
var w loggo.Writer
w, err = loggo.NewLogFileWriter(filePath, 0644, nil)
if err != nil {
return
}
err = loggo.RegisterWriter(path.Base(filePath), w)
if err != nil {
return
}
}
return loggo.ConfigureLoggers(level)
}