Files
micro_layout/internal/logger/logger.go
T
2025-12-05 17:23:29 +08:00

58 lines
1.9 KiB
Go

package logger
import (
"context"
log "github.com/sirupsen/logrus"
)
func Trace(ctx context.Context, keyword string, args ...interface{}) {
log.WithFields(WithContext(ctx)).WithField("keyword", keyword).Trace(args...)
}
func Debug(ctx context.Context, keyword string, args ...interface{}) {
log.WithFields(WithContext(ctx)).WithField("keyword", keyword).Debug(args...)
}
func Info(ctx context.Context, keyword string, args ...interface{}) {
log.WithFields(WithContext(ctx)).WithField("keyword", keyword).Info(args...)
}
func Warn(ctx context.Context, keyword string, args ...interface{}) {
log.WithFields(WithContext(ctx)).WithField("keyword", keyword).Warn(args...)
}
func Error(ctx context.Context, keyword string, args ...interface{}) {
log.WithFields(WithContext(ctx)).WithField("keyword", keyword).Error(args...)
}
func Tracef(ctx context.Context, keyword, format string, args ...interface{}) {
log.WithFields(WithContext(ctx)).WithField("keyword", keyword).Tracef(format, args...)
}
func Debugf(ctx context.Context, keyword, format string, args ...interface{}) {
log.WithFields(WithContext(ctx)).WithField("keyword", keyword).Debugf(format, args...)
}
func Infof(ctx context.Context, keyword, format string, args ...interface{}) {
log.WithFields(WithContext(ctx)).WithField("keyword", keyword).Infof(format, args...)
}
func Warnf(ctx context.Context, keyword, format string, args ...interface{}) {
log.WithFields(WithContext(ctx)).WithField("keyword", keyword).Warnf(format, args...)
}
func Errorf(ctx context.Context, keyword, format string, args ...interface{}) {
log.WithFields(WithContext(ctx)).WithField("keyword", keyword).Errorf(format, args...)
}
func WithContext(ctx context.Context) log.Fields {
logFields := log.Fields{}
if ctx.Value("req_id") != nil {
if v, ok := ctx.Value("req_id").(string); ok {
logFields["metadata.req_id"] = v
}
}
return logFields
}