Skip to content

Commit

Permalink
Merge pull request #195 from guoming0000/main
Browse files Browse the repository at this point in the history
add prom for err log
  • Loading branch information
luduoxin authored Feb 20, 2025
2 parents c06480e + 23cec74 commit ff39d6f
Showing 1 changed file with 85 additions and 0 deletions.
85 changes: 85 additions & 0 deletions glog/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,35 @@ package glog
import (
"context"
"fmt"
"runtime"
"strconv"
"sync"

"github.com/sunmi-OS/gocore/v2/glog/logx"
"github.com/sunmi-OS/gocore/v2/glog/zap"

"github.com/prometheus/client_golang/prometheus"
)

var (
Logger sync.Map
// statistic log err count
metricErrCount = prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: "gocore",
Name: "log_error_total",
Help: "statistic log err count",
}, []string{"source"})
)

const (
_callerSkip = "caller_skip"
_defaultCallSkip = 2
)

// 默认加入zap组件
func init() {
prometheus.MustRegister(metricErrCount)
Logger.Store("zap", &zap.Zap{})
}

Expand All @@ -23,6 +40,12 @@ func SetLogger(name string, logger logx.GLog) {
Logger.Store(name, logger)
}

// WithCallerSkipCtx 提供设置日志自定义source位置的入口
// 默认是3,当需要增加该值,可以设置获得更外层的
func WithCallerSkipCtx(ctx context.Context, skip int) context.Context {
return context.WithValue(ctx, _callerSkip, skip)
}

// DelLogger删除日志插件
func DelLogger(name string) {
Logger.Delete(name)
Expand Down Expand Up @@ -71,27 +94,39 @@ func WarnF(format string, args ...interface{}) {
}

func Error(args ...interface{}) {
fn := funcName(_defaultCallSkip)
metricErrCount.WithLabelValues(fn).Inc()

Logger.Range(func(k, v interface{}) bool {
v.(logx.GLog).Error(args...)
return true
})
}

func ErrorF(format string, args ...interface{}) {
fn := funcName(_defaultCallSkip)
metricErrCount.WithLabelValues(fn).Inc()

Logger.Range(func(k, v interface{}) bool {
v.(logx.GLog).ErrorF(format, args...)
return true
})
}

func Fatal(args ...interface{}) {
fn := funcName(_defaultCallSkip)
metricErrCount.WithLabelValues(fn).Inc()

Logger.Range(func(k, v interface{}) bool {
v.(logx.GLog).Fatal(args...)
return true
})
}

func FatalF(format string, args ...interface{}) {
fn := funcName(_defaultCallSkip)
metricErrCount.WithLabelValues(fn).Inc()

Logger.Range(func(k, v interface{}) bool {
v.(logx.GLog).FatalF(format, args...)
return true
Expand Down Expand Up @@ -120,13 +155,19 @@ func WarnW(keyvals ...interface{}) {
}

func ErrorW(keyvals ...interface{}) {
fn := funcName(_defaultCallSkip)
metricErrCount.WithLabelValues(fn).Inc()

Logger.Range(func(k, v interface{}) bool {
v.(logx.GLog).CommonLog(logx.LevelError, context.TODO(), keyvals...)
return true
})
}

func FatalW(keyvals ...interface{}) {
fn := funcName(_defaultCallSkip)
metricErrCount.WithLabelValues(fn).Inc()

Logger.Range(func(k, v interface{}) bool {
v.(logx.GLog).CommonLog(logx.LevelFatal, context.TODO(), keyvals...)
return true
Expand Down Expand Up @@ -155,13 +196,31 @@ func WarnC(ctx context.Context, format string, args ...interface{}) {
}

func ErrorC(ctx context.Context, format string, args ...interface{}) {
funcSkip := 0
if value := ctx.Value(_callerSkip); value != nil {
if i, ok := value.(int); ok {
funcSkip = i
}
}
fn := funcName(_defaultCallSkip + funcSkip)
metricErrCount.WithLabelValues(fn).Inc()

Logger.Range(func(k, v interface{}) bool {
v.(logx.GLog).CommonLog(logx.LevelError, ctx, fmt.Sprintf(format, args...))
return true
})
}

func FatalC(ctx context.Context, format string, args ...interface{}) {
funcSkip := 0
if value := ctx.Value(_callerSkip); value != nil {
if i, ok := value.(int); ok {
funcSkip = i
}
}
fn := funcName(_defaultCallSkip + funcSkip)
metricErrCount.WithLabelValues(fn).Inc()

Logger.Range(func(k, v interface{}) bool {
v.(logx.GLog).CommonLog(logx.LevelFatal, ctx, fmt.Sprintf(format, args...))
return true
Expand Down Expand Up @@ -190,15 +249,41 @@ func WarnV(ctx context.Context, keyvals ...interface{}) {
}

func ErrorV(ctx context.Context, keyvals ...interface{}) {
funcSkip := 0
if value := ctx.Value(_callerSkip); value != nil {
if i, ok := value.(int); ok {
funcSkip = i
}
}
fn := funcName(_defaultCallSkip + funcSkip)
metricErrCount.WithLabelValues(fn).Inc()

Logger.Range(func(k, v interface{}) bool {
v.(logx.GLog).CommonLog(logx.LevelError, ctx, keyvals...)
return true
})
}

func FatalV(ctx context.Context, keyvals ...interface{}) {
funcSkip := 0
if value := ctx.Value(_callerSkip); value != nil {
if i, ok := value.(int); ok {
funcSkip = i
}
}
fn := funcName(_defaultCallSkip + funcSkip)
metricErrCount.WithLabelValues(fn).Inc()

Logger.Range(func(k, v interface{}) bool {
v.(logx.GLog).CommonLog(logx.LevelFatal, ctx, keyvals...)
return true
})
}

// funcName get func name.
func funcName(skip int) (name string) {
if _, file, lineNo, ok := runtime.Caller(skip); ok {
return file + ":" + strconv.Itoa(lineNo)
}
return "unknown:0"
}

0 comments on commit ff39d6f

Please sign in to comment.