log.go 2.7 KB
Newer Older
Y
Your Name 已提交
1
package configmanager
E
eoLinker API Management 已提交
2 3 4

import (
	"encoding/json"
黄孟柱 已提交
5 6 7 8 9 10
	log "github.com/eolinker/goku-api-gateway/goku-log"
	access_log "github.com/eolinker/goku-api-gateway/goku-node/access-log"
	"github.com/eolinker/goku-api-gateway/goku-node/manager/updater"
	access_field "github.com/eolinker/goku-api-gateway/server/access-field"
	dao "github.com/eolinker/goku-api-gateway/server/dao/config-log"
	entity "github.com/eolinker/goku-api-gateway/server/entity/config-log"
E
eoLinker API Management 已提交
11 12 13
)

const (
Y
Your Name 已提交
14
	//AccessLog access日志
Y
Your Name 已提交
15
	AccessLog = "access"
Y
Your Name 已提交
16 17
	//NodeLog 节点日志
	NodeLog = "node"
E
eoLinker API Management 已提交
18 19 20
)

func init() {
Y
Your Name 已提交
21
	updater.Add(reloadLogConfig, 1, "goku_config_log")
E
eoLinker API Management 已提交
22
}
Y
Your Name 已提交
23
func defaultAccessLogConfig() *entity.LogConfig {
E
eoLinker API Management 已提交
24 25 26 27 28 29 30 31 32 33
	return &entity.LogConfig{
		Name:   AccessLog,
		Enable: 1,
		Dir:    "work/logs/",
		File:   "access.log",

		Period: "hour",
		Fields: "",
	}
}
Y
Your Name 已提交
34
func defaultNodeAppLogConfig() *entity.LogConfig {
E
eoLinker API Management 已提交
35 36 37 38 39 40 41 42 43
	return &entity.LogConfig{
		Name:   AccessLog,
		Enable: 1,
		Dir:    "work/logs/",
		File:   "node.log",
		Level:  "error",
		Period: "hour",
	}
}
Y
Your Name 已提交
44 45

//InitLog 初始化日志
Y
Your Name 已提交
46
func InitLog() {
E
eoLinker API Management 已提交
47 48
	reloadLogConfig()
}
Y
Your Name 已提交
49
func reloadLogConfig() {
E
eoLinker API Management 已提交
50 51 52 53
	reloadAppLog()
	reloadAccessLog()

}
Y
Your Name 已提交
54 55 56 57
func reloadAppLog() {
	c, e := dao.Get(NodeLog)
	if e != nil {
		log.Warn("manager/config load goku_config_log fro node  error:", e)
E
eoLinker API Management 已提交
58 59
		c = defaultNodeAppLogConfig()
	}
Y
Your Name 已提交
60 61
	period, err := log.ParsePeriod(c.Period)
	if err != nil {
E
eoLinker API Management 已提交
62
		period = log.PeriodDay
Y
Your Name 已提交
63
		log.Warn("manager/config unmarshal access log period failed for nod , use the default config:%s", e)
E
eoLinker API Management 已提交
64 65
	}

Y
Your Name 已提交
66 67
	level, err := log.ParseLevel(c.Level)
	if err != nil {
E
eoLinker API Management 已提交
68
		level = log.WarnLevel
Y
Your Name 已提交
69
		log.Warn("manager/config unmarshal access log level failed for nod , use the default config:%s", e)
E
eoLinker API Management 已提交
70 71
	}

Y
Your Name 已提交
72 73
	enable := c.Enable == 1
	log.SetOutPut(enable, c.Dir, c.File, period, c.Expire)
E
eoLinker API Management 已提交
74 75 76
	log.SetLevel(level)

}
Y
Your Name 已提交
77
func reloadAccessLog() {
E
eoLinker API Management 已提交
78
	c, e := dao.Get(AccessLog)
Y
Your Name 已提交
79 80 81
	if e != nil {
		log.Warn("manager/config load  goku_config_log for access log error:", e)
		c = defaultAccessLogConfig()
E
eoLinker API Management 已提交
82 83
	}

Y
Your Name 已提交
84 85
	period, err := log.ParsePeriod(c.Period)
	if err != nil {
E
eoLinker API Management 已提交
86
		period = log.PeriodDay
Y
Your Name 已提交
87
		log.Warn("manager/config unmarshal period failed for , use the default config:%s", e)
E
eoLinker API Management 已提交
88
	}
Y
Your Name 已提交
89
	enable := c.Enable == 1
E
eoLinker API Management 已提交
90

Y
Your Name 已提交
91 92
	fieldsConfig := make([]AccessField, 0, access_field.Size())
	err = json.Unmarshal([]byte(c.Fields), &fieldsConfig)
E
eoLinker API Management 已提交
93

Y
Your Name 已提交
94 95
	if err != nil || len(fieldsConfig) == 0 {
		log.Warn("manager/config unmarshal access log fields error:", err)
E
eoLinker API Management 已提交
96 97

		access_log.SetFields(access_field.Default())
Y
Your Name 已提交
98 99 100 101 102 103
	} else {
		fields := make([]access_field.AccessFieldKey, 0, access_field.Size())
		for _, f := range fieldsConfig {
			if f.Select {
				if access_field.Has(f.Name) {
					fields = append(fields, access_field.Parse(f.Name))
E
eoLinker API Management 已提交
104 105 106 107 108 109
				}
			}
		}
		access_log.SetFields(fields)
	}

Y
Your Name 已提交
110
	access_log.SetOutput(enable, c.Dir, c.File, period, c.Expire)
E
eoLinker API Management 已提交
111

Y
Your Name 已提交
112
}