• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

config.go

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
package blog4go

import (
"encoding/xml"
"errors"
"io/ioutil"
"os"
)

const (
// TypeTimeBaseRotate is time base logrotate tag
TypeTimeBaseRotate = "time"
// TypeSizeBaseRotate is size base logrotate tag
TypeSizeBaseRotate = "size"
)

var (
// ErrConfigFiltersNotFound not found filters
ErrConfigFiltersNotFound = errors.New("Please define at least one filter")
// ErrConfigBadAttributes wrong attribute
ErrConfigBadAttributes = errors.New("Bad attributes setting")
// ErrConfigLevelsNotFound not found levels
ErrConfigLevelsNotFound = errors.New("Please define levels attribution")
// ErrConfigFilePathNotFound not found file path
ErrConfigFilePathNotFound = errors.New("Please define the file path")
// ErrConfigFileRotateTypeNotFound not found rotate type
ErrConfigFileRotateTypeNotFound = errors.New("Please define the file rotate type")
// ErrConfigSocketAddressNotFound not found socket address
ErrConfigSocketAddressNotFound = errors.New("Please define a socket address")
// ErrConfigSocketNetworkNotFound not found socket port
ErrConfigSocketNetworkNotFound = errors.New("Please define a socket network type")
)

// Config struct define the config struct used for file wirter
type Config struct {
Filters  []filter `xml:"filter"`
MinLevel string   `xml:"minlevel,attr"`
}

// log filter
type filter struct {
Levels     string     `xml:"levels,attr"`
Colored    bool       `xml:"colored,attr"`
File       file       `xml:"file"`
RotateFile rotateFile `xml:"rotatefile"`
Console    console    `xml:"console"`
Socket     socket     `xml:"socket"`
}

type file struct {
Path string `xml:"path,attr"`
}

type rotateFile struct {
Path        string `xml:"path,attr"`
Type        string `xml:"type,attr"`
RotateLines int    `xml:"rotateLines,attr"`
RotateSize  int64  `xml:"rotateSize,attr"`
Retentions  int64  `xml:"retentions,attr"`
}

type console struct {
// redirect stderr to stdout
Redirect bool `xml:"redirect"`
}

type socket struct {
Network string `xml:"network,attr"`
Address string `xml:"address,attr"`
}

// check if config is valid
func (config *Config) valid() error {
// check minlevel validation
if "" != config.MinLevel && !LevelFromString(config.MinLevel).valid() {
return ErrConfigBadAttributes
}
// check filters len
if len(config.Filters) < 1 {
return ErrConfigFiltersNotFound
}

// check filter one by one
for _, filter := range config.Filters {
if "" == filter.Levels {
return ErrConfigLevelsNotFound
}

if (file{}) != filter.File {
// seem not needed now
//if "" == filter.File.Path {
//return ErrConfigFilePathNotFound
//}
} else if (rotateFile{}) != filter.RotateFile {
if "" == filter.RotateFile.Path {
return ErrConfigFilePathNotFound
}

if "" == filter.RotateFile.Type {
return ErrConfigFileRotateTypeNotFound
}
} else if (socket{}) != filter.Socket {
if "" == filter.Socket.Address {
return ErrConfigSocketAddressNotFound
}

if "" == filter.Socket.Network {
return ErrConfigSocketNetworkNotFound
}
}
}

return nil
}

// read config from a xml file
func readConfig(fileName string) (*Config, error) {
file, err := os.Open(fileName)
if nil != err {
return nil, err
}
defer file.Close()

in, err := ioutil.ReadAll(file)
if nil != err {
return nil, err
}

config := new(Config)
err = xml.Unmarshal(in, config)
if nil != err {
return nil, err
}

return config, err
}


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
关于Mac或Linux下GO的Permissiondenied提示错误发布时间:2022-07-10
下一篇:
第二十五期:知乎用Go替代Python,说明了啥发布时间:2022-07-10
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap