Format and remove 13.4.md spaces

This commit is contained in:
vCaesar
2017-06-10 12:29:53 +08:00
parent 34e13ed128
commit 6006af58fc

View File

@@ -10,83 +10,83 @@ beego的日志设计部署思路来自于seelog根据不同的level来记录
```Go
// Log levels to control the logging output.
const (
// 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 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 {
// 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) {
// SetLogLevel sets the global log level used by the simple
// logger.
func SetLevel(l int) {
level = l
}
}
```
上面这一段实现了日志系统的日志分级默认的级别是Trace用户通过SetLevel可以设置不同的分级。
```Go
// logger references the used application logger.
var BeeLogger = log.New(os.Stdout, "", log.Ldate|log.Ltime)
// 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) {
// SetLogger sets a new logger.
func SetLogger(l *log.Logger) {
BeeLogger = l
}
}
// Trace logs a message at trace level.
func Trace(v ...interface{}) {
// 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{}) {
// 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{}) {
// 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{}) {
// 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{}) {
// 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{}) {
// 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的接口输出。这里面实现了六个函数
@@ -121,31 +121,31 @@ beego的日志设计部署思路来自于seelog根据不同的level来记录
首先定义了一些ini配置文件的一些全局性常量
```Go
var (
var (
bComment = []byte{'#'}
bEmpty = []byte{}
bEqual = []byte{'='}
bDQuote = []byte{'"'}
)
)
```
定义了配置文件的格式:
```Go
// A Config represents the configuration.
type Config struct {
// 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数据
```Go
// ParseFile creates a new Config and parses the file configuration from the
// named file.
func LoadConfig(name string) (*Config, error) {
// 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
@@ -200,36 +200,36 @@ beego的日志设计部署思路来自于seelog根据不同的level来记录
cfg.offset[key] = off
}
return cfg, nil
}
}
```
下面实现了一些读取配置文件的函数返回的值确定为bool、int、float64或string
```Go
// Bool returns the boolean value for a given key.
func (c *Config) Bool(key string) (bool, error) {
// 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) {
// 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) {
// 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 {
// String returns the string value for a given key.
func (c *Config) String(key string) string {
return c.data[key]
}
}
```
## 应用指南
下面这个函数是我一个应用中的例子用来获取远程url地址的json数据实现如下
```Go
func GetJson() {
func GetJson() {
resp, err := http.Get(beego.AppConfig.String("url"))
if err != nil {
beego.Critical("http get info error")
@@ -241,13 +241,13 @@ beego的日志设计部署思路来自于seelog根据不同的level来记录
if err != nil {
beego.Critical("error:", err)
}
}
}
```
函数中调用了框架的日志函数`beego.Critical`函数用来报错,调用了`beego.AppConfig.String("url")`用来获取配置文件中的信息,配置文件的信息如下(app.conf)
```Go
appname = hs
url ="http://www.api.com/api.html"
appname = hs
url ="http://www.api.com/api.html"
```
## links