代码拉取完成,页面将自动刷新
func Config(conf LogConfig)
注意: 不调用配置方法,日志默认打印到终端。
{
"log": {
"engine": {
"adapter": "console",
"config": {
}
},
"path": "",
"cache_size": 10000,
"enable_func_call_depth": true,
"func_call_depth": 3
}
}
{
"log": {
"engine": {
"adapter": "file",
"config": {
"filename": "test.log"
}
},
"path": "",
"cache_size": 10000,
"enable_func_call_depth": true,
"func_call_depth": 3
}
}
注意: 按天生成日志文件。
package main
import (
"encoding/json"
"io/ioutil"
"log"
"strings"
"git.oschina.net/cloudzone/cloudcommon-go/logger"
)
// Config test example
type Config struct {
LogConfig logger.LogConfig `json:"log"`
}
func main() {
logger.Trace("this is a test")
logger.Debug("this is a test")
logger.Info("this is a test")
logger.Warn("this is a test")
logger.Error("this is a test")
logger.Fatal("this is a test")
c := ParseConfig("cfg_file.json")
logger.Config(c.LogConfig)
logger.Trace("this is a test")
logger.Debug("this is a test")
logger.Info("this is a test")
logger.Warn("this is a test")
logger.Error("this is a test")
logger.Fatal("this is a test")
}
// ParseConfig parse config from cfg file
func ParseConfig(cfg string) *Config {
configContent, err := ToTrimString(cfg)
if err != nil {
log.Fatalln("read config file:", cfg, "fail:", err)
}
var c Config
err = json.Unmarshal([]byte(configContent), &c)
if err != nil {
log.Fatalln("parse config file:", cfg, "fail:", err)
}
log.Println("read config file:", cfg, "successfully.", c)
return &c
}
// ToString return string from read cfg file
func ToString(filePath string) (string, error) {
b, err := ioutil.ReadFile(filePath)
if err != nil {
return "", err
}
return string(b), nil
}
// ToTrimString read cfg file and replace space
func ToTrimString(filePath string) (string, error) {
str, err := ToString(filePath)
if err != nil {
return "", err
}
return strings.TrimSpace(str), nil
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。