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

import "C"
import (
	"encoding/json"
Y
Your Name 已提交
6 7
	"strings"

黄孟柱 已提交
8 9 10
	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 已提交
11 12 13
)

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

var (
	logNames = map[string]int{
		ConsoleLog: 1,
		NodeLog:    1,
		AccessLog:  1,
	}
Y
Your Name 已提交
30
	//Expires 过期时间选项数组
E
eoLinker API Management 已提交
31 32
	Expires = []ValueTitle{
		{
Y
Your Name 已提交
33 34
			Value: 3,
			Title: "3天",
E
eoLinker API Management 已提交
35 36
		},
		{
Y
Your Name 已提交
37 38
			Value: 7,
			Title: "7天",
E
eoLinker API Management 已提交
39 40
		},
		{
Y
Your Name 已提交
41 42
			Value: 30,
			Title: "30天",
E
eoLinker API Management 已提交
43 44
		},
		{
Y
Your Name 已提交
45 46
			Value: 90,
			Title: "90天",
E
eoLinker API Management 已提交
47 48
		},
		{
Y
Your Name 已提交
49 50
			Value: 180,
			Title: "180天",
E
eoLinker API Management 已提交
51 52
		},
	}
Y
Your Name 已提交
53
	//Periods 日志生成周期
E
eoLinker API Management 已提交
54 55 56 57 58 59 60 61 62 63
	Periods = []NameTitle{
		{
			Name:  log.PeriodDay.String(),
			Title: "天",
		},
		{
			Name:  log.PeriodHour.String(),
			Title: "小时",
		},
	}
Y
Your Name 已提交
64
	//Levels 日志级别
E
eoLinker API Management 已提交
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
	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 已提交
84
//NameTitle 名称标题结构体
E
eoLinker API Management 已提交
85 86 87 88 89
type NameTitle struct {
	Name  string `json:"name"`
	Title string `json:"title"`
}

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

Y
Your Name 已提交
96
//Param 日志参数
E
eoLinker API Management 已提交
97 98 99 100 101 102 103 104 105 106
type Param struct {
	Enable bool
	Dir    string
	File   string
	Level  string
	Period string
	Fields string
	Expire int
}

Y
Your Name 已提交
107
//PutParam put方式所需参数
E
eoLinker API Management 已提交
108 109 110 111 112 113
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 已提交
114
	Expire int    `opt:"expire,require"`
E
eoLinker API Management 已提交
115 116
}

Y
Your Name 已提交
117
//Format 格式化
E
eoLinker API Management 已提交
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
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 已提交
139
//AccessParam access参数
E
eoLinker API Management 已提交
140 141 142 143 144 145
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 已提交
146
	Expire int    `opt:"expire,require"`
E
eoLinker API Management 已提交
147 148
}

Y
Your Name 已提交
149
//Format 格式化
E
eoLinker API Management 已提交
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
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 已提交
166
//LogConfig 日志配置
E
eoLinker API Management 已提交
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
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 已提交
189
	if c.Expire < ExpireDefault {
E
eoLinker API Management 已提交
190 191 192 193
		c.Expire = ExpireDefault
	}
}

Y
Your Name 已提交
194
//AccessConfig access配置
E
eoLinker API Management 已提交
195 196 197 198 199 200 201 202 203 204 205
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 已提交
206

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

Y
Your Name 已提交
214
//InitFields 域初始化
E
eoLinker API Management 已提交
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 269
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

}