码迷,mamicode.com
首页 > 其他好文 > 详细

Golang之beego读取配置信息,输出log模块

时间:2018-01-29 16:21:08      阅读:1461      评论:0      收藏:0      [点我收藏+]

标签:level   img   bug   alt   str   rac   ini   配置   main   

1,准备好配置文件

[server]
listen_ip = "0.0.0.0"
listen_port = 8888

[logs]
log_level=debug
log_path=./logs/logagent.log

[collect]
log_path=D:\project\logs\logagent.log
topic=nginx_log
chan_size=100

通过golang读取配置文件

package main

import (
    "fmt"
    "github.com/astaxie/beego/config"
)

func main() {
    conf, err := config.NewConfig("ini", "D:/project/src/go_dev/day11/config/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:", 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)
}

main.go运行结果

port: 8888
log_level: debug
log_path: D:\project\logs\logagent.log

Process finished with exit code 0

2,beego输出log文件日志

技术分享图片

main.go

package main

import (
    "encoding/json"
    "fmt"
    "github.com/astaxie/beego/logs"
)

func main() {
    config := make(map[string]interface{})
    config["filename"] = "D:/project/src/go_dev/day11/logs/logcollect.log"
    config["level"] = logs.LevelDebug

    configStr, err := json.Marshal(config)
    if err != nil {
        fmt.Println("marshal failed,err:", 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,my name is %s", "stu02")
    logs.Warn("this is a warn,my name is %s", "stu03")
}

运行结果,生成log文件

技术分享图片

 

Golang之beego读取配置信息,输出log模块

标签:level   img   bug   alt   str   rac   ini   配置   main   

原文地址:https://www.cnblogs.com/pyyu/p/8376275.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!