commit 9eb29011f300d60e58b78754c94343c297238178 Author: zhaoyupeng Date: Mon Nov 24 09:56:15 2025 +0800 🎨 start v3 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8abc79a --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +.idea +.vscode +.DS_Store + +xtest \ No newline at end of file diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..65cfee8 --- /dev/null +++ b/go.mod @@ -0,0 +1,16 @@ +module gitea.loveuer.com/yizhisec/pkg3 + +go 1.23.0 + +toolchain go1.24.3 + +require ( + github.com/fatih/color v1.18.0 + github.com/google/uuid v1.6.0 +) + +require ( + github.com/mattn/go-colorable v0.1.13 // indirect + github.com/mattn/go-isatty v0.0.20 // indirect + golang.org/x/sys v0.31.0 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..99fe179 --- /dev/null +++ b/go.sum @@ -0,0 +1,13 @@ +github.com/fatih/color v1.18.0 h1:S8gINlzdQ840/4pfAwic/ZE0djQEH3wM94VfqLTZcOM= +github.com/fatih/color v1.18.0/go.mod h1:4FelSpRwEGDpQ12mAdzqdOukCy4u8WUtOY6lkT/6HfU= +github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= +github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= +github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= +github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= +github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= +github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= +golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.31.0 h1:ioabZlmFYtWhL+TRYpcnNlLwhyxaM9kWTDEmfnprqik= +golang.org/x/sys v0.31.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k= diff --git a/logger/ctx.go b/logger/ctx.go new file mode 100644 index 0000000..47b8d47 --- /dev/null +++ b/logger/ctx.go @@ -0,0 +1,53 @@ +package logger + +import ( + "context" + uuid2 "github.com/google/uuid" +) + +type _traceId struct{} + +var TraceId = _traceId{} + +func traceId(ctx context.Context) string { + if ctx == nil { + uuid, _ := uuid2.NewV7() + return uuid.String() + } + + if id, _ := ctx.Value(TraceId).(string); id != "" { + return id + } + + uuid, _ := uuid2.NewV7() + return uuid.String() +} + +func DebugCtx(ctx context.Context, msg string, data ...any) { + msg = traceId(ctx) + " | " + msg + DefaultLogger.Debug(msg, data...) +} +func InfoCtx(ctx context.Context, msg string, data ...any) { + msg = traceId(ctx) + " | " + msg + DefaultLogger.Info(msg, data...) +} + +func WarnCtx(ctx context.Context, msg string, data ...any) { + msg = traceId(ctx) + " | " + msg + DefaultLogger.Warn(msg, data...) +} + +func ErrorCtx(ctx context.Context, msg string, data ...any) { + msg = traceId(ctx) + " | " + msg + DefaultLogger.Error(msg, data...) +} + +func PanicCtx(ctx context.Context, msg string, data ...any) { + msg = traceId(ctx) + " | " + msg + DefaultLogger.Panic(msg, data...) +} + +func FatalCtx(ctx context.Context, msg string, data ...any) { + msg = traceId(ctx) + " | " + msg + DefaultLogger.Fatal(msg, data...) +} diff --git a/logger/ctx_test.go b/logger/ctx_test.go new file mode 100644 index 0000000..8948cac --- /dev/null +++ b/logger/ctx_test.go @@ -0,0 +1,17 @@ +package logger + +import ( + "context" + "testing" +) + +func TestCtxLog(t *testing.T) { + DebugCtx(nil, "hello %s", "world") + InfoCtx(nil, "hello %s", "world") + WarnCtx(context.Background(), "hello %s", "world") + ctx := context.Background() + ctx = context.WithValue(ctx, TraceId, "value") + SetLogLevel(LogLevelDebug) + DebugCtx(ctx, "hello %s", "world") + ErrorCtx(ctx, "hello %s", "world") +} diff --git a/logger/default.go b/logger/default.go new file mode 100644 index 0000000..ecd0911 --- /dev/null +++ b/logger/default.go @@ -0,0 +1,67 @@ +package logger + +import ( + "fmt" + "os" + "sync" +) + +var ( + nilLogger = func(prefix, timestamp, msg string, data ...any) {} + normalLogger = func(prefix, timestamp, msg string, data ...any) { + fmt.Printf(prefix+"| "+timestamp+" | "+msg+"\n", data...) + } + + panicLogger = func(prefix, timestamp, msg string, data ...any) { + panic(fmt.Sprintf(prefix+"| "+timestamp+" | "+msg+"\n", data...)) + } + + fatalLogger = func(prefix, timestamp, msg string, data ...any) { + fmt.Printf(prefix+"| "+timestamp+" | "+msg+"\n", data...) + os.Exit(1) + } + + DefaultLogger = &logger{ + Mutex: sync.Mutex{}, + timeFormat: "2006-01-02T15:04:05", + writer: os.Stdout, + level: LogLevelInfo, + debug: nilLogger, + info: normalLogger, + warn: normalLogger, + error: normalLogger, + panic: panicLogger, + fatal: fatalLogger, + } +) + +func SetTimeFormat(format string) { + DefaultLogger.SetTimeFormat(format) +} + +func SetLogLevel(level LogLevel) { + DefaultLogger.SetLogLevel(level) +} + +func Debug(msg string, data ...any) { + DefaultLogger.Debug(msg, data...) +} +func Info(msg string, data ...any) { + DefaultLogger.Info(msg, data...) +} + +func Warn(msg string, data ...any) { + DefaultLogger.Warn(msg, data...) +} + +func Error(msg string, data ...any) { + DefaultLogger.Error(msg, data...) +} + +func Panic(msg string, data ...any) { + DefaultLogger.Panic(msg, data...) +} + +func Fatal(msg string, data ...any) { + DefaultLogger.Fatal(msg, data...) +} diff --git a/logger/logger.go b/logger/logger.go new file mode 100644 index 0000000..f8a2e83 --- /dev/null +++ b/logger/logger.go @@ -0,0 +1,115 @@ +package logger + +import ( + "github.com/fatih/color" + "io" + "sync" + "time" +) + +type LogLevel uint32 + +const ( + LogLevelDebug = iota + LogLevelInfo + LogLevelWarn + LogLevelError + LogLevelPanic + LogLevelFatal +) + +type logger struct { + sync.Mutex + timeFormat string + writer io.Writer + level LogLevel + debug func(prefix, timestamp, msg string, data ...any) + info func(prefix, timestamp, msg string, data ...any) + warn func(prefix, timestamp, msg string, data ...any) + error func(prefix, timestamp, msg string, data ...any) + panic func(prefix, timestamp, msg string, data ...any) + fatal func(prefix, timestamp, msg string, data ...any) +} + +var ( + red = color.New(color.FgRed) + hired = color.New(color.FgHiRed) + green = color.New(color.FgGreen) + yellow = color.New(color.FgYellow) + white = color.New(color.FgWhite) +) + +func (l *logger) SetTimeFormat(format string) { + l.Lock() + defer l.Unlock() + l.timeFormat = format +} + +func (l *logger) SetLogLevel(level LogLevel) { + l.Lock() + defer l.Unlock() + + if level > LogLevelDebug { + l.debug = nilLogger + } else { + l.debug = normalLogger + } + + if level > LogLevelInfo { + l.info = nilLogger + } else { + l.info = normalLogger + } + + if level > LogLevelWarn { + l.warn = nilLogger + } else { + l.warn = normalLogger + } + + if level > LogLevelError { + l.error = nilLogger + } else { + l.error = normalLogger + } + + if level > LogLevelPanic { + l.panic = nilLogger + } else { + l.panic = panicLogger + } + + if level > LogLevelFatal { + l.fatal = nilLogger + } else { + l.fatal = fatalLogger + } +} + +func (l *logger) Debug(msg string, data ...any) { + l.debug(white.Sprint("Debug "), time.Now().Format(l.timeFormat), msg, data...) +} + +func (l *logger) Info(msg string, data ...any) { + l.info(green.Sprint("Info "), time.Now().Format(l.timeFormat), msg, data...) +} + +func (l *logger) Warn(msg string, data ...any) { + l.warn(yellow.Sprint("Warn "), time.Now().Format(l.timeFormat), msg, data...) +} + +func (l *logger) Error(msg string, data ...any) { + l.error(red.Sprint("Error "), time.Now().Format(l.timeFormat), msg, data...) +} + +func (l *logger) Panic(msg string, data ...any) { + l.panic(hired.Sprint("Panic "), time.Now().Format(l.timeFormat), msg, data...) +} + +func (l *logger) Fatal(msg string, data ...any) { + l.fatal(hired.Sprint("Fatal "), time.Now().Format(l.timeFormat), msg, data...) +} + +type WroteLogger interface { + Info(msg string, data ...any) +} diff --git a/logger/new.go b/logger/new.go new file mode 100644 index 0000000..e684914 --- /dev/null +++ b/logger/new.go @@ -0,0 +1,21 @@ +package logger + +import ( + "os" + "sync" +) + +func New() *logger { + return &logger{ + Mutex: sync.Mutex{}, + timeFormat: "2006-01-02T15:04:05", + writer: os.Stdout, + level: LogLevelInfo, + debug: nilLogger, + info: normalLogger, + warn: normalLogger, + error: normalLogger, + panic: panicLogger, + fatal: fatalLogger, + } +}