推荐:CoLog - Go语言的前缀级执行日志库

推荐:CoLog - Go语言的前缀级执行日志库

cologCoLog is a prefix-based leveled execution log for Go项目地址:https://gitcode.com/gh_mirrors/co/colog

Build Status godoc reference

CoLog 是一款为Go编程语言设计的基于前缀的日志级别处理库,其设计理念深受 Logrus 影响,但更专注于解析标准库log的输出。通过下面的示例图,你可以直观地理解它的功能。

为什么选择CoLog?

关于CoLog的详细介绍和设计思想,你可以查阅这篇博客文章:CoLog - Golang中的前缀式日志记录

主要特性

  • 支持挂钩:通过 AddHook 将日志条目发送到外部系统。
  • 自定义格式化器:内置了颜色、纯文本和JSON格式化器,可通过 SetFormatter 自由切换。
  • 内置6个日志级别:trace、debug、info、warning、error、alert,并支持1至3字母的缩写形式。
  • 可自定义前缀(Header):通过 SetHeadersAddHeader 设置。
  • 级别控制:使用 SetMinLevelSetDefaultLevel 管理显示级别的最小值和默认值。
  • 字段解析支持:启用 ParseFields 进行键值对解析。
  • 自定义键值提取器:通过 SetExtractor 实现。
  • 固定上下文值:利用 FixedValue 添加常量信息,用 ClearFixedValues 清除。
  • 独立日志器:使用 NewCoLogNewLogger 创建独立的日志实例。
  • 兼容性:与现有的Logrus挂钩和格式化器通过cologrus 兼容。
  • Windows终端颜色支持:通过wincolog 实现。

使用案例

基本用法

package main

import (
	"log"

	"github.com/comail/colog"
)

func main() {
	colog.Register()
	log.Print("info: that's all it takes!")
}

JSON输出到文件并解析字段

package main

import (
	"log"
	"os"
	"time"

	"github.com/comail/colog"
)

func main() {
	file, err := os.OpenFile("temp_json.log", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0777)
	if err != nil {
		panic(err)
	}

	colog.Register()
	colog.SetOutput(file)
	colog.ParseFields(true)
	colog.SetFormatter(&colog.JSONFormatter{
		TimeFormat: time.RFC3339,
		Flag:       log.Lshortfile,
	})

	log.Print("info: logging this to json")
	log.Print("warning: with fields foo=bar")
}

独立日志器,设置级别和固定值

package main

import (
	"log"
	"os"

	"github.com/comail/colog"
)

func main() {
	cl := colog.NewCoLog(os.Stdout, "worker ", log.LstdFlags)
	cl.SetMinLevel(colog.LInfo)
	cl.SetDefaultLevel(colog.LWarning)
	cl.FixedValue("worker_id", 42)

	logger := cl.NewLogger()
	logger.Print("this gets warning level")
	logger.Print("debug: this won't be displayed")
}

添加自定义挂钩

package main

import (
	"fmt"
	"log"

	"github.com/comail/colog"
)

type myHook struct {
	levels []colog.Level
}

func (h *myHook) Levels() []colog.Level {
	return h.levels
}

func (h *myHook) Fire(e *colog.Entry) error {
	fmt.Printf("We got an entry: \n%#v", e)
	return nil
}

func main() {
	colog.Register()
	colog.ParseFields(true)

	hook := &myHook{
		levels: []colog.Level{
			colog.LInfo,    // 只接收这些级别
			colog.LWarning, 
		},
	}

	colog.AddHook(hook)

	colog.SetMinLevel(colog.LError) 
	log.Print("info: something foo=bar")
}

API稳定性保证

CoLog的API改动可能性极小,但无法做出无修改承诺。不过,CoLog只会被最终应用导入,如果你有这样一个应用,你应该已经将依赖库进行了版本管理。CoLog自身没有任何外部依赖,只需克隆此仓库,即可开始使用。

在你的Go项目中引入CoLog,享受这个强大的日志库带来的便捷吧!

cologCoLog is a prefix-based leveled execution log for Go项目地址:https://gitcode.com/gh_mirrors/co/colog

创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

周澄诗Flourishing

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值