set.go 1023 字节
Newer Older
Y
Your Name 已提交
1
package configlog
E
eoLinker API Management 已提交
2 3 4

import (
	"fmt"
黄孟柱 已提交
5 6 7 8
	log "github.com/eolinker/goku-api-gateway/goku-log"
	dao2 "github.com/eolinker/goku-api-gateway/server/dao"
	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 已提交
9 10
)

Y
Your Name 已提交
11
//Set set
Y
Your Name 已提交
12 13 14
func Set(name string, param *Param) error {
	if _, has := logNames[name]; !has {
		return fmt.Errorf("not has that log config of %s", name)
E
eoLinker API Management 已提交
15
	}
Y
Your Name 已提交
16
	c := new(entity.LogConfig)
E
eoLinker API Management 已提交
17 18 19 20 21 22 23 24
	c.Name = name
	c.Level = param.Level

	c.Period = param.Period
	c.File = param.File
	c.Dir = param.Dir
	if param.Enable {
		c.Enable = 1
Y
Your Name 已提交
25
	} else {
E
eoLinker API Management 已提交
26 27 28 29
		c.Enable = 0
	}
	c.Fields = param.Fields
	c.Expire = param.Expire
Y
Your Name 已提交
30 31
	err := dao.Set(c)
	if err != nil {
E
eoLinker API Management 已提交
32 33
		return err
	}
Y
Your Name 已提交
34
	_ = dao2.UpdateTable("goku_config_log")
E
eoLinker API Management 已提交
35

Y
Your Name 已提交
36
	if name == ConsoleLog {
E
eoLinker API Management 已提交
37 38 39 40
		go reset(c)
	}
	return nil
}
Y
Your Name 已提交
41
func reset(c *entity.LogConfig) {
E
eoLinker API Management 已提交
42

Y
Your Name 已提交
43 44 45
	period, _ := log.ParsePeriod(c.Period)
	log.SetOutPut(c.Enable == 1, c.Dir, c.File, period, c.Expire)
	l, _ := log.ParseLevel(c.Level)
E
eoLinker API Management 已提交
46 47
	log.SetLevel(l)

Y
Your Name 已提交
48
}