17 Star 10 Fork 5

cloudzone / cloudcommon-go

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README

日志

级别

  • Trace(format string, args ...interface{}) 打印trace级别日志
  • Debug(format string, args ...interface{}) Debug 打印debug级别日志
  • Info(format string, args ...interface{}) 打印info级别日志
  • Warn(format string, args ...interface{}) 打印warn级别日志
  • Error(format string, args ...interface{}) 打印error级别日志
  • Fatal(format string, args ...interface{}) 打印critial级别日志

配置

  • 日志配置方法
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
}
Go
1
https://gitee.com/cloudzone/cloudcommon-go.git
git@gitee.com:cloudzone/cloudcommon-go.git
cloudzone
cloudcommon-go
cloudcommon-go
dev

搜索帮助