mirror of
https://github.com/yv1ing/gin-admin.git
synced 2025-10-24 10:12:05 +08:00
67 lines
1.7 KiB
Go
67 lines
1.7 KiB
Go
package logger
|
|
|
|
import (
|
|
"go.uber.org/zap"
|
|
"go.uber.org/zap/zapcore"
|
|
"os"
|
|
"sync"
|
|
)
|
|
|
|
// @Author: yv1ing
|
|
// @Email: me@yvling.cn
|
|
// @Date: 2025/8/28 14:53
|
|
// @Desc: 日志器封装
|
|
|
|
var (
|
|
sugar *zap.SugaredLogger
|
|
baseLogger *zap.Logger
|
|
initOnce sync.Once
|
|
)
|
|
|
|
func SetupLogger(filename string) {
|
|
initOnce.Do(func() {
|
|
var syncers []zapcore.WriteSyncer
|
|
|
|
syncers = append(syncers, zapcore.AddSync(os.Stdout))
|
|
|
|
if filename != "" {
|
|
f, err := os.OpenFile(filename, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
|
|
if err == nil {
|
|
syncers = append(syncers, zapcore.AddSync(f))
|
|
}
|
|
}
|
|
|
|
multi := zapcore.NewMultiWriteSyncer(syncers...)
|
|
|
|
encCfg := zap.NewProductionEncoderConfig()
|
|
encCfg.TimeKey = "time"
|
|
encCfg.LevelKey = "level"
|
|
encCfg.CallerKey = "caller"
|
|
encCfg.EncodeTime = zapcore.ISO8601TimeEncoder
|
|
encCfg.EncodeLevel = zapcore.CapitalLevelEncoder
|
|
encCfg.EncodeCaller = zapcore.ShortCallerEncoder
|
|
|
|
encoder := zapcore.NewConsoleEncoder(encCfg)
|
|
core := zapcore.NewCore(encoder, multi, zapcore.DebugLevel)
|
|
|
|
baseLogger = zap.New(core, zap.AddCaller(), zap.AddCallerSkip(1))
|
|
sugar = baseLogger.Sugar()
|
|
})
|
|
}
|
|
|
|
func Close() {
|
|
if baseLogger != nil {
|
|
_ = baseLogger.Sync()
|
|
}
|
|
}
|
|
|
|
func Debug(args ...any) { sugar.Debug(args...) }
|
|
func Info(args ...any) { sugar.Info(args...) }
|
|
func Warn(args ...any) { sugar.Warn(args...) }
|
|
func Error(args ...any) { sugar.Error(args...) }
|
|
|
|
func Debugf(template string, args ...any) { sugar.Debugf(template, args...) }
|
|
func Infof(template string, args ...any) { sugar.Infof(template, args...) }
|
|
func Warnf(template string, args ...any) { sugar.Warnf(template, args...) }
|
|
func Errorf(template string, args ...any) { sugar.Errorf(template, args...) }
|