golang基础-beego读取配置、log日志输出
发布时间:2020-12-16 09:43:03 所属栏目:大数据 来源:网络整理
导读:beego读取配置文件 配置文件内容如下: [server] listen_ip = "0.0.0.0" listen_port = 8888 [logs] log_level= debug log_path= ./logs/logagent.log [collect] log_path= D:projectlogslogagent.log topic= nginx_log chan_size= 100 通过代码读取配置文
beego读取配置文件配置文件内容如下: [server]
listen_ip = "0.0.0.0"
listen_port = 8888
[logs]
log_level=debug
log_path=./logs/logagent.log
[collect]
log_path=D:projectlogslogagent.log
topic=nginx_log
chan_size=100
通过代码读取配置文件 package main
import (
"fmt"
"github.com/astaxie/beego/config"
)
func main() {
conf,err := config.NewConfig("ini","logagent.conf")
if err != nil {
fmt.Println("new config failed,err:",err)
return
}
port,err:= conf.Int("server::listen_port")
if err != nil {
fmt.Println("read server:port failed,err)
return
}
fmt.Println("port:",port)
log_level := conf.String("logs::log_level")
if len(log_level) == 0 {
log_level = "debug"
}
fmt.Println("log_level:",log_level)
log_path := conf.String("collect::log_path")
fmt.Println("log_path:",log_path)
}
输出如下: PS E:golanggo_prosrcsafly> go run demo.go
port: 8888
log_level: debug
log_path: D:projectlogslogagent.log
PS E:golanggo_prosrcsafly>
beego输出log文件日志需要创建logs文件夹 package main
import (
"encoding/json"
"fmt"
"github.com/astaxie/beego/logs"
)
func main() {
//我测试如下3个路径均可
config := make(map[string]interface{})
// config["filename"] = "e:/golang/go_pro/logs/logcollect.log"
// config["filename"] = "e://golang//go_pro//logs//logcollect.log"
config["filename"] = "e:golanggo_prologslogcollect.log"
config["level"] = logs.LevelDebug
configStr,err := json.Marshal(config)
if err != nil {
fmt.Println("marshal failed,err)
return
}
logs.SetLogger(logs.AdapterFile,string(configStr))
logs.Debug("this is a test,my name is %s","stu01")
logs.Trace("this is a trace,"stu02")
logs.Warn("this is a warn,"stu03")
}
然后就会在logs文件夹中输出log文件logcollect.log (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |