Skip to content

Repository files navigation

xlog

一个轻量的 Go 日志 SDK,基于 logrus,默认输出带调用位置的文本日志,并支持:

  • 设置服务名(service 字段)
  • 控制日志级别
  • 输出到本地文件(按大小滚动 + 保留数量)
  • 推送到 Grafana Loki

安装

go get github.com/xswitch-cn/xlog@latest

快速开始

package main

import "github.com/xswitch-cn/xlog"

func main() {
	xlog.Name("demo-service")
	xlog.SetLevel("info")

	xlog.Info("hello")
	xlog.Warnf("user=%d not found", 42)
}

重要说明

  • xlog是单例的不要重复初始化

方法列表(对外 API)

基础配置

  • Name(name string):设置服务名(写入 service 字段)
  • SetLevel(l string):设置日志等级(trace/debug/info/warn/error/fatal)
  • GetLevel() logrus.Level:获取当前日志等级
  • GetLogger() *logrus.Logger:获取底层 logger(便于自定义 hook/formatter)
  • SetCaller(isReport bool):是否输出调用位置

输出到 Loki

  • SetLoki(uri string) error:配置 Loki 推送(默认 appName 为空)
  • SetLoki2(uri, appName string) error:配置 Loki 推送并指定 appName

输出到文件

  • SetLogFile(config string) error:配置文件落盘
    config 格式:logPath:fileSizeMB:maxRetentionCount

日志输出

  • Log(v ...interface{})
  • Logf(format string, v ...interface{})
  • WithLevel(l logrus.Level, v ...interface{})
  • WithLevelf(l logrus.Level, format string, v ...interface{})
  • Trace/Tracef
  • Debug/Debugf
  • Info/Infof
  • Warn/Warnf
  • Error/Errorf
  • Panic/Panicf
  • Fatal/Fatalf

结构化/调试辅助

  • Pretty(v ...interface{})
  • Prettyf(format string, v ...interface{})
  • PrettyLog(l logrus.Level, v ...interface{})
  • PrettyLogf(l logrus.Level, format string, v ...interface{})
  • RpcTrace(method string, request interface{}, response interface{})

License

Apache-2.0

About

xlog

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages