log.go 1.5 KB
Newer Older
Y
Your Name 已提交
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
package dao_version_config

import (
	"encoding/json"

	"github.com/eolinker/goku-api-gateway/common/database"
	"github.com/eolinker/goku-api-gateway/config"
)

func GetLogInfo() (*config.LogConfig, *config.AccessLogConfig, error) {
	db := database.GetConnection()
	sql := "SELECT `name`,`enable`,`dir`,`file`,`period`,IFNULL(`level`,''),IFNULL(`fields`,''),`expire` FROM goku_config_log;"
	rows, err := db.Query(sql)
	if err != nil {
		return nil, nil, err
	}
	defer rows.Close()
	var logCf *config.LogConfig
	var accessCf *config.AccessLogConfig
	for rows.Next() {
		var name, dir, file, level, fields, period string
		var enable, expire int
		err = rows.Scan(&name, &enable, &dir, &file, &period, &level, &fields, &expire)
		if err != nil {
			return nil, nil, err
		}
		if name == "console" {
			continue
		} else if name == "access" {
			tmp := make([]map[string]interface{}, 0)
			err = json.Unmarshal([]byte(fields), &tmp)
			if err != nil {
				return nil, nil, err
			}
			fields := make([]string, 0)
			for _, t := range tmp {
				fields = append(fields, t["name"].(string))
			}
			accessCf = &config.AccessLogConfig{
				Name:   name,
				Enable: enable,
				Dir:    dir,
				File:   file,
				Period: period,
				Expire: expire,
				Fields: fields,
			}
		} else if name == "node" {
			logCf = &config.LogConfig{
				Name:   name,
				Enable: enable,
				Dir:    dir,
				File:   file,
				Period: period,
				Level:  level,
				Expire: expire,
			}
		}
	}
	return logCf, accessCf, nil
}