forked from gocw/wepay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
48 lines (44 loc) · 1010 Bytes
/
config.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
package main
import (
"github.com/Sirupsen/logrus"
"github.com/koding/multiconfig"
"github.com/joho/godotenv"
)
// Config 配置结构体
type Config struct {
DB string `default:"mysql"`
DBURL string `default:"localhost"`
Debug bool `default:"true"`
}
var config Config
// initConfig 初始化config
func initConfig() {
//获取.env文件
err := godotenv.Overload()
if err != nil {
logrus.Infof("Error loading .env file: %+v", err)
}
//读取config default值
m := multiconfig.MultiLoader(
&multiconfig.TagLoader{},
&multiconfig.EnvironmentLoader{
CamelCase: true,
},
)
//载入全局变量config
m.Load(&config)
logrus.Info(config)
//设置日志等级
if config.Debug {
logrus.SetFormatter(&logrus.TextFormatter{
FullTimestamp: true,
TimestampFormat: "06-01-02 15:04:05.00",
})
logrus.SetLevel(logrus.DebugLevel)
} else {
logrus.SetFormatter(&logrus.JSONFormatter{
TimestampFormat: "06-01-02 15:04:05.00",
})
logrus.SetLevel(logrus.InfoLevel)
}
}