log.go 2.5 KB
Newer Older
E
eoLinker API Management 已提交
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
package config_manager

import (
	"encoding/json"
	log "github.com/eolinker/goku/goku-log"
	access_log "github.com/eolinker/goku/goku-node/access-log"
	"github.com/eolinker/goku/goku-node/manager/updater"
	access_field "github.com/eolinker/goku/server/access-field"
	dao "github.com/eolinker/goku/server/dao/config-log"
	entity "github.com/eolinker/goku/server/entity/config-log"
)

const (
	AccessLog  = "access"
	NodeLog = "node"
)

func init() {
	updater.Add(reloadLogConfig,1, "goku_config_log")
}
func defaultAccessLogConfig()*entity.LogConfig  {
	return &entity.LogConfig{
		Name:   AccessLog,
		Enable: 1,
		Dir:    "work/logs/",
		File:   "access.log",

		Period: "hour",
		Fields: "",
	}
}
func defaultNodeAppLogConfig()*entity.LogConfig  {
	return &entity.LogConfig{
		Name:   AccessLog,
		Enable: 1,
		Dir:    "work/logs/",
		File:   "node.log",
		Level:  "error",
		Period: "hour",

	}
}
func InitLog()  {
	reloadLogConfig()
}
func reloadLogConfig()  {
	reloadAppLog()
	reloadAccessLog()

}
func reloadAppLog()  {
	c,e:= dao.Get(NodeLog)
	if e!=nil{
		log.Warn("manager/config load goku_config_log fro node  error:",e)
		c = defaultNodeAppLogConfig()
	}
	period,err:=log.ParsePeriod(c.Period)
	if err!=nil{
		period = log.PeriodDay
		log.Warn("manager/config unmarshal access log period failed for nod , use the default config:%s",e)
	}

	level,err:= log.ParseLevel(c.Level)
	if err!=nil{
		level = log.WarnLevel
		log.Warn("manager/config unmarshal access log level failed for nod , use the default config:%s",e)
	}

	enable:= c.Enable == 1
	log.SetOutPut(enable,c.Dir,c.File,period,c.Expire)
	log.SetLevel(level)

}
func reloadAccessLog()  {
	c, e := dao.Get(AccessLog)
	if e!=nil{
		log.Warn("manager/config load  goku_config_log for access log error:",e)
		c= defaultAccessLogConfig()
	}

	period,err:=log.ParsePeriod(c.Period)
	if err!=nil{
		period = log.PeriodDay
		log.Warn("manager/config unmarshal period failed for , use the default config:%s",e)
	}
	enable:= c.Enable == 1


	fieldsConfig:= make([]AccessField ,0,access_field.Size())
	err =json.Unmarshal([]byte(c.Fields),&fieldsConfig)

	if err!= nil || len(fieldsConfig) == 0{
		log.Warn("manager/config unmarshal access log fields error:",err)

		access_log.SetFields(access_field.Default())
	}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))
				}
			}
		}
		access_log.SetFields(fields)
	}

	access_log.SetOutput( enable,c.Dir,c.File,period,c.Expire)

}