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

import (
	"fmt"
Y
Your Name 已提交
5

黄孟柱 已提交
6
	log "github.com/eolinker/goku-api-gateway/goku-log"
Y
Your Name 已提交
7
	config_log "github.com/eolinker/goku-api-gateway/server/dao/console-sqlite3/config-log"
黄孟柱 已提交
8
	entity "github.com/eolinker/goku-api-gateway/server/entity/config-log"
E
eoLinker API Management 已提交
9 10
)

Y
Your Name 已提交
11
//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
	err := config_log.Set(c)
Y
Your Name 已提交
31
	if err != nil {
E
eoLinker API Management 已提交
32 33
		return err
	}
Y
Your Name 已提交
34
	if name == ConsoleLog {
E
eoLinker API Management 已提交
35 36 37 38
		go reset(c)
	}
	return nil
}
Y
Your Name 已提交
39
func reset(c *entity.LogConfig) {
E
eoLinker API Management 已提交
40

Y
Your Name 已提交
41 42 43
	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 已提交
44 45
	log.SetLevel(l)

Y
Your Name 已提交
46
}