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

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

const (
Y
Your Name 已提交
13 14 15 16 17 18 19
	// ConsoleLog 控制台log
	ConsoleLog = "console"
	//NodeLog 节点log
	NodeLog = "node"
	//AccessLog access log
	AccessLog = "access"
	//ExpireDefault 默认过期时间
E
eoLinker API Management 已提交
20 21 22 23 24 25 26 27 28
	ExpireDefault = 3
)

var (
	logNames = map[string]int{
		ConsoleLog: 1,
		NodeLog:    1,
		AccessLog:  1,
	}
Y
Your Name 已提交
29
	//Expires 过期配置
E
eoLinker API Management 已提交
30 31
	Expires = []ValueTitle{
		{
Y
Your Name 已提交
32 33
			Value: 3,
			Title: "3天",
E
eoLinker API Management 已提交
34 35
		},
		{
Y
Your Name 已提交
36 37
			Value: 7,
			Title: "7天",
E
eoLinker API Management 已提交
38 39
		},
		{
Y
Your Name 已提交
40 41
			Value: 30,
			Title: "30天",
E
eoLinker API Management 已提交
42 43
		},
		{
Y
Your Name 已提交
44 45
			Value: 90,
			Title: "90天",
E
eoLinker API Management 已提交
46 47
		},
		{
Y
Your Name 已提交
48 49
			Value: 180,
			Title: "180天",
E
eoLinker API Management 已提交
50 51
		},
	}
Y
Your Name 已提交
52
	//Periods 周期
E
eoLinker API Management 已提交
53 54 55 56 57 58 59 60 61 62
	Periods = []NameTitle{
		{
			Name:  log.PeriodDay.String(),
			Title: "天",
		},
		{
			Name:  log.PeriodHour.String(),
			Title: "小时",
		},
	}
Y
Your Name 已提交
63
	//Levels 层级
E
eoLinker API Management 已提交
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
	Levels = []NameTitle{
		{
			Name:  log.ErrorLevel.String(),
			Title: strings.ToUpper(log.ErrorLevel.String()),
		},
		{
			Name:  log.WarnLevel.String(),
			Title: strings.ToUpper(log.WarnLevel.String()),
		}, {
			Name:  log.InfoLevel.String(),
			Title: strings.ToUpper(log.InfoLevel.String()),
		},
		{
			Name:  log.DebugLevel.String(),
			Title: strings.ToUpper(log.DebugLevel.String()),
		},
	}
)

Y
Your Name 已提交
83
//NameTitle name title
E
eoLinker API Management 已提交
84 85 86 87 88
type NameTitle struct {
	Name  string `json:"name"`
	Title string `json:"title"`
}

Y
Your Name 已提交
89
//ValueTitle value title
E
eoLinker API Management 已提交
90 91 92 93
type ValueTitle struct {
	Value int    `json:"value"`
	Title string `json:"title"`
}
Y
Your Name 已提交
94 95

//Param param
E
eoLinker API Management 已提交
96 97 98 99 100 101 102 103 104 105
type Param struct {
	Enable bool
	Dir    string
	File   string
	Level  string
	Period string
	Fields string
	Expire int
}

Y
Your Name 已提交
106
//PutParam put param
E
eoLinker API Management 已提交
107 108 109 110 111 112
type PutParam struct {
	Enable bool   `opt:"enable,require"`
	Dir    string `opt:"dir,require"`
	File   string `opt:"file,require"`
	Level  string `opt:"level,require"`
	Period string `opt:"period,require"`
Y
Your Name 已提交
113
	Expire int    `opt:"expire,require"`
E
eoLinker API Management 已提交
114 115
}

Y
Your Name 已提交
116
//Format 格式化
E
eoLinker API Management 已提交
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
func (p *PutParam) Format() (*Param, error) {
	l, err := log.ParseLevel(p.Level)
	if err != nil {
		return nil, err
	}
	period, err := log.ParsePeriod(p.Period)
	if err != nil {
		return nil, err
	}
	return &Param{
		Enable: p.Enable,
		Dir:    p.Dir,
		File:   p.File,
		Level:  l.String(),
		Period: period.String(),
		Expire: p.Expire,
		Fields: "",
	}, nil

}

Y
Your Name 已提交
138
//AccessParam access param
E
eoLinker API Management 已提交
139 140 141 142 143 144
type AccessParam struct {
	Enable bool   `opt:"enable,require" `
	Dir    string `opt:"dir,require"`
	File   string `opt:"file,require"`
	Period string `opt:"period,require"`
	Fields string `opt:"fields,require"`
Y
Your Name 已提交
145
	Expire int    `opt:"expire,require"`
E
eoLinker API Management 已提交
146 147
}

Y
Your Name 已提交
148
//Format 格式化
E
eoLinker API Management 已提交
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
func (p *AccessParam) Format() (*Param, error) {
	period, err := log.ParsePeriod(p.Period)
	if err != nil {
		return nil, err
	}
	return &Param{
		Enable: p.Enable,
		Dir:    p.Dir,
		File:   p.File,
		Level:  "",
		Period: period.String(),
		Fields: p.Fields,
		Expire: p.Expire,
	}, nil
}

Y
Your Name 已提交
165
//LogConfig 日志配置
E
eoLinker API Management 已提交
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
type LogConfig struct {
	Name    string       `json:"-"`
	Enable  bool         `json:"enable" opt:"enable" default:"true"`
	Dir     string       `json:"dir" opt:"dir" default:"work/logs/"`
	File    string       `json:"file"`
	Level   string       `json:"level"`
	Levels  []NameTitle  `json:"levels"`
	Period  string       `json:"period"`
	Periods []NameTitle  `json:"periods"`
	Expire  int          `json:"expire"`
	Expires []ValueTitle `json:"expires"`
}

func (c *LogConfig) Read(ent *entity.LogConfig) {

	c.Name = ent.Name
	c.Enable = ent.Enable == 1
	c.Dir = ent.Dir
	c.File = ent.File
	c.Level = ent.Level
	c.Period = ent.Period
	c.Expire = ent.Expire
Y
Your Name 已提交
188
	if c.Expire < ExpireDefault {
E
eoLinker API Management 已提交
189 190 191 192
		c.Expire = ExpireDefault
	}
}

Y
Your Name 已提交
193
//AccessConfig access配置
E
eoLinker API Management 已提交
194 195 196 197 198 199 200 201 202 203 204
type AccessConfig struct {
	Name    string         `json:"-"`
	Enable  bool           `json:"enable" opt:"enable" default:"true"`
	Dir     string         `json:"dir" opt:"dir" default:"work/logs/"`
	File    string         `json:"file" opt:"file" default:"access"`
	Period  string         `json:"period"`
	Periods []NameTitle    `json:"periods"`
	Expire  int            `json:"expire"`
	Expires []ValueTitle   `json:"expires"`
	Fields  []*AccessField `json:"fields"`
}
Y
Your Name 已提交
205 206

//AccessField access日志字段
E
eoLinker API Management 已提交
207 208 209 210 211 212
type AccessField struct {
	Name   string `json:"name"`
	Select bool   `json:"select"`
	Desc   string `json:"desc"`
}

Y
Your Name 已提交
213
//InitFields 初始化字段
E
eoLinker API Management 已提交
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
func (c *AccessConfig) InitFields() {
	// 如果有新增的字段,按默认顺序拼接到末尾
	c.Fields = make([]*AccessField, 0, access_field.Size())
	for _, value := range access_field.All() {

		field := &AccessField{
			Name:   value.Key(),
			Select: access_field.IsDefault(value),
			Desc:   value.Info(),
		}
		c.Fields = append(c.Fields, field)
	}
}
func (c *AccessConfig) Read(ent *entity.LogConfig) {

	c.Name = ent.Name
	c.Enable = ent.Enable == 1
	c.Dir = ent.Dir
	c.File = ent.File
	c.Period = ent.Period
	c.Expire = ent.Expire
	fields := make([]*AccessField, 0, access_field.Size())
	e := json.Unmarshal([]byte(ent.Fields), &fields)
	if e != nil {
		c.InitFields()
		return
	}

	all := access_field.CopyKey()

	fieldsTmp := make([]*AccessField, 0, access_field.Size())
	// 筛选有效的字段
	for _, field := range fields {
		if desc, has := all[field.Name]; has {
			field.Desc = desc
			fieldsTmp = append(fieldsTmp, field)
			delete(all, field.Name)
		}
	}

	// 如果有新增的字段,按默认顺序拼接到末尾
	for _, value := range access_field.All() {
		if _, has := all[value.Key()]; has {
			field := &AccessField{
				Name:   value.Key(),
				Select: false,
				Desc:   value.Info(),
			}
			fieldsTmp = append(fieldsTmp, field)
		}
	}

	c.Fields = fieldsTmp

}