一个轻量的 Go 日志 SDK,基于 logrus,默认输出带调用位置的文本日志,并支持:
- 设置服务名(service 字段)
- 控制日志级别
- 输出到本地文件(按大小滚动 + 保留数量)
- 推送到 Grafana Loki
go get github.com/xswitch-cn/xlog@latestpackage 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是单例的不要重复初始化
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):是否输出调用位置
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/TracefDebug/DebugfInfo/InfofWarn/WarnfError/ErrorfPanic/PanicfFatal/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{})
Apache-2.0