GO日志和配置设计

本文介绍了日志和配置在软件开发中的重要性,特别是Beego框架的日志设计,基于seelog思想,以及Beego的轻量级配置设计,包括使用log.Logger接口、配置文件读取和处理方式。
摘要由CSDN通过智能技术生成

日志和配置的重要性

前面已经介绍过日志在我们程序开发中起着很重要的作用,通过日志我们可以记录调试我
们的信息,当初介绍过一个日志系统 seelog,根据不同的 level 输出不同的日志,这个对
于程序开发和程序部署来说至关重要。我们可以在程序开发中设置 level 低一点,部署的时
候把 level 设置高,这样我们开发中的调试信息可以屏蔽掉。
配置模块对于应用部署牵涉到服务器不同的一些配置信息非常有用,例如一些数据库配置
信息、监听端口、监听地址等都是可以通过配置文件来配置,这样我们的应用程序就具有很
强的灵活性,可以通过配置文件的配置部署在不同的机器上,可以连接不同的数据库之类
的。

**beego **的日志设计

beego 的日志设计部署思路来自于 seelog,根据不同的 level 来记录日志,但是 beego 设
计的日志系统比较轻量级,采用了系统的 log.Logger 接口,默认输出到 os.Stdout,用户可
以实现这个接口然后通过 beego.SetLogger 设置自定义的输出,详细的实现如下所示:

// Log levels to control the logging output.
const (
    LevelTrace = iota
    LevelDebug
    LevelInfo
    LevelWarning
    LevelError
    LevelCritical
)
// logLevel controls the global log level used by the logger.
var level = LevelTrace
// LogLevel returns the global log level and can be used in
// own implementations of the logger interface.
func Level() int {
    return level
}
// SetLogLevel sets the global log level used by the simple
// logger.
func SetLevel(l int) {
    level = l
}

上面这一段实现了日志系统的日志分级,默认的级别是 Trace,用户通过 SetLevel 可以设
置不同的分级。

// logger references the used application logger.
var BeeLogger = log.New(os.Stdout, "", log.Ldate|log.Ltime)
// SetLogger sets a new logger.
func SetLogger(l *log.Logger) {
    BeeLogger = l
}
// Trace logs a message at trace level.
func Trace(v ...interface{}) {
    if level <= LevelTrace {
        BeeLogger.Printf("[T] %v\n", v)
    }
}
// Debug logs a message at debug level.
func Debug(v ...interface{}) {
    if level <= LevelDebug {
        BeeLogger.Printf("[D] %v\n", v)
    }
}
// Info logs a message at info level.
func Info(v ...interface{}) {
    if level <= LevelInfo {
        BeeLogger.Printf("[I] %v\n", v)
    }
}
// Warning logs a message at warning level.
func Warn(v ...interface{}) {
    if level <= LevelWarning {
        BeeLogger.Printf("[W] %v\n", v)
    }
}
// Error logs a message at error level.
func Error(v ...interface{}) {
    if level <= LevelError {
        BeeLogger.Printf("[E] %v\n", v)
    }
}
// Critical logs a message at critical level.
func Critical(v ...interface{}) {
    if level <= LevelCritical {
        BeeLogger.Printf("[C] %v\n", v)
    }
}

上面这一段代码默认初始化了一个 BeeLogger 对象,默认输出到 os.Stdout,用户可以通
过 beego.SetLogger 来设置实现了 logger 的接口输出。这里面实现了六个函数:
:::info
• Trace(一般的记录信息,举例如下:)
o “Entered parse function validation block”
o “Validation: entered second ‘if’”
o “Dictionary ‘Dict’ is empty. Using default value”
• Debug(调试信息,举例如下:)
o “Web page requested: http://somesite.com Params=‘…’”
o “Response generated. Response size: 10000. Sending.”
o “New file received. Type:PNG Size:20000”
• Info(打印信息,举例如下:)
o “Web server restarted”
o “Hourly statistics: Requested pages: 12345 Errors: 123 …”
o “Service paused. Waiting for ‘resume’ call”
• Warn(警告信息,举例如下:)
o “Cache corrupted for file=‘test.file’. Reading from back-end”
o “Database 192.168.0.7/DB not responding. Using backup 192.168.0.8/DB”
o “No response from statistics server. Statistics not sent”
• Error(错误信息,举例如下:)
o “Internal error. Cannot process request #12345 Error:…”
o “Cannot perform login: credentials DB not responding”
• Critical(致命错误,举例如下:)
o “Critical panic received: … Shutting down”
o “Fatal error: … App is shutting down to prevent data corruption or loss”
:::
可以看到每个函数里面都有对 level 的判断,所以如果我们在部署的时候设置了
level=LevelWarning,那么 Trace、Debug、Info 这三个函数都不会有任何的输出,以此类推。

**beego **的配置设计

配置信息的解析,beego 实现了一个 key=value 的配置文件读取,类似 ini 配置文件的格式,
就是一个文件解析的过程,然后把解析的数据保存到 map 中,最后在调用的时候通过几个
string、int 之类的函数调用返回相应的值,具体的实现请看下面:
首先定义了一些 ini 配置文件的一些全局性常量 :

var (
    bComment = []byte{'#'}
    bEmpty = []byte{}
    bEqual = []byte{'='}
    bDQuote = []byte{'"'}
)

定义了配置文件的格式:

// A Config represents the configuration.
type Config struct {
    filename string
    comment map[int][]string // id: []{comment, key...}; id 1 is for main comment.
    data map[string]string // key: value
    offset map[string]int64 // key: offset; for editing.
    sync.RWMutex
}

定义了解析文件的函数,解析文件的过程是打开文件,然后一行一行的读取,解析注释、空
行和 key=value 数据:

// ParseFile creates a new Config and parses the file configuration from the
// named file.
func LoadConfig(name string) (*Config, error) {
    file, err := os.Open(name)
    if err != nil {
        return nil, err
    }
    cfg := &Config{
        file.Name(),
        make(map[int][]string),
        make(map[string]string),
        make(map[string]int64),
        sync.RWMutex{},
    }
    cfg.Lock()
    defer cfg.Unlock()
    defer file.Close()
    var comment bytes.Buffer
    buf := bufio.NewReader(file)
    for nComment, off := 0, int64(1); ; {
        line, _, err := buf.ReadLine()
        if err == io.EOF {
            break
        }
        if bytes.Equal(line, bEmpty) {
            continue
        }
        off += int64(len(line))
        if bytes.HasPrefix(line, bComment) {
            line = bytes.TrimLeft(line, "#")
            line = bytes.TrimLeftFunc(line, unicode.IsSpace)
            comment.Write(line)
            comment.WriteByte('\n')
            continue
        }
        if comment.Len() != 0 {
            cfg.comment[nComment] = []string{comment.String()}
            comment.Reset()
            nComment++
        }
        val := bytes.SplitN(line, bEqual, 2)
        if bytes.HasPrefix(val[1], bDQuote) {
            val[1] = bytes.Trim(val[1], `"`)
        }
        key := strings.TrimSpace(string(val[0]))
        cfg.comment[nComment-1] = append(cfg.comment[nComment-1], key)
        cfg.data[key] = strings.TrimSpace(string(val[1]))
        cfg.offset[key] = off
    }
    return cfg, nil
}

下面实现了一些读取配置文件的函数,返回的值确定为 bool、int、float64 或 string:

// Bool returns the boolean value for a given key.
func (c *Config) Bool(key string) (bool, error) {
    return strconv.ParseBool(c.data[key])
}
// Int returns the integer value for a given key.
func (c *Config) Int(key string) (int, error) {
    return strconv.Atoi(c.data[key])
}
// Float returns the float value for a given key.
func (c *Config) Float(key string) (float64, error) {
    return strconv.ParseFloat(c.data[key], 64)
}
// String returns the string value for a given key.
func (c *Config) String(key string) string {
    return c.data[key]
}

应用指南

下面这个函数是我一个应用中的例子,用来获取远程 url 地址的 json 数据,实现如下:

func GetJson() {
    resp, err := http.Get(beego.AppConfig.String("url"))
    if err != nil {
        beego.Critical("http get info error")
        return
    }
    defer resp.Body.Close()
    body, err := ioutil.ReadAll(resp.Body)
    err = json.Unmarshal(body, &AllInfo)
    if err != nil {
        beego.Critical("error:", err)
    }
}

函数中调用了框架的日志函数 beego.Critical 函数用来报错,调用了
beego.AppConfig.String(“url”)用来获取配置文件中的信息,配置文件的信息如下
(app.conf):

appname = hs

url ="http://www.api.com/api.html"
  • 15
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值