1 Star 0 Fork 0

szmaozi/go-utils

Create your Gitee Account
Explore and code with more than 12 million developers,Free private repositories !:)
Sign up
Clone or Download
xlog.go 3.64 KB
Copy Edit Raw Blame History
/*
* @Descripttion:
* @version:
* @Author: moo
* @Date: 2020-10-20 17:49:17
* @LastEditors: moo
* @LastEditTime: 2023-08-31 18:04:57
*/
package xlog
import (
"fmt"
"strings"
"gitee.com/szmaozi/go-utils/logbase/logcore"
"go.uber.org/zap"
)
/*
* 说明: log有i几个有用法, 这几个用法不要混用
* 1. Init 用法
* 2. MustGetLogger用法 // 不建议使用。
* // 如果使用要注意,因为初始化顺序问题。这种情况下一般在init中写好defaultcfg,或者将log初始化放在setting模块中,保障地一个运行
* 3. SetLog, 使用外部传递过来的log
*/
type Config logcore.Config
var gLogger *logcore.AppLogger = nil
var zapSkip *zap.SugaredLogger
var gDefaultCfg Config = Config{
Name: "main",
WriteFile: false,
FilePath: "./logs/log.log",
LevelConsole: "debug",
LevelFile: "info",
}
func init() {
gLogger = Init(&gDefaultCfg)
}
// var atomConsole = zap.NewAtomicLevel()
// var atomFile = zap.NewAtomicLevel()
func SetDefaultFilePath(path string) {
gDefaultCfg.FilePath = path
}
func Init(cfg *Config) *logcore.AppLogger {
if gLogger != nil {
gLogger.Sync()
}
c := logcore.Config{
Name: cfg.Name,
WriteFile: cfg.WriteFile,
FilePath: cfg.FilePath,
LevelConsole: cfg.LevelConsole,
LevelFile: cfg.LevelFile,
}
gLogger = logcore.New(&c)
zapSkip = gLogger.GetZapLogger().Sugar()
return gLogger
}
func MustGetLogger(names ...string) *logcore.AppLogger {
if gLogger == nil {
Init(nil)
}
if len(names) > 0 {
return gLogger.GetNameLogger(names[0])
}
return gLogger
}
func Exit() {
if gLogger == nil {
return
}
gLogger.Sync()
fmt.Println("logBase exit")
}
func ZapLog(logger *zap.Logger, level string, msg string) {
switch strings.ToLower(level) {
case "debug":
logger.Debug(msg)
case "info":
logger.Info(msg)
case "warning":
logger.Warn(msg)
case "error":
logger.Error(msg)
case "panic", "fatal", "dpanic", "dfatal":
logger.DPanic(msg)
default:
logger.Debug(msg)
}
}
// 不初始化log,但引入外部的log
// 注意: 这里针对更改loglevel有问题。
func SetLogger(logger *logcore.AppLogger) error {
gLogger = logger
// zapSkip = Log.GetZapLogger().WithOptions(zap.AddCallerSkip(1)).Sugar()
zapSkip = gLogger.GetZapLogger().Sugar()
return nil
}
func Debug(args ...interface{}) { zapSkip.Debug(args...) }
func Debugf(template string, args ...interface{}) { zapSkip.Debugf(template, args...) }
func Info(args ...interface{}) { zapSkip.Info(args...) }
func Infof(template string, args ...interface{}) { zapSkip.Infof(template, args...) }
func Warn(args ...interface{}) { zapSkip.Warn(args...) }
func Warnf(template string, args ...interface{}) { zapSkip.Warnf(template, args...) }
func Error(args ...interface{}) { zapSkip.Error(args...) }
func Errorf(template string, args ...interface{}) { zapSkip.Errorf(template, args...) }
func DPanic(args ...interface{}) { zapSkip.DPanic(args...); zapSkip.Sync() }
func DPanicf(template string, args ...interface{}) {
zapSkip.DPanicf(template, args...)
zapSkip.Sync()
}
func Panic(args ...interface{}) { zapSkip.Panic(args...); zapSkip.Sync() }
func Panicf(template string, args ...interface{}) {
zapSkip.Panicf(template, args...)
zapSkip.Sync()
}
func Fatal(args ...interface{}) { zapSkip.Fatal(args...); zapSkip.Sync() }
func Fatalf(template string, args ...interface{}) {
zapSkip.Fatalf(template, args...)
zapSkip.Sync()
}
func Println(args ...interface{}) { fmt.Println(args...) }
func Printf(template string, args ...interface{}) {
fmt.Printf(template, args...)
fmt.Printf("\n")
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/szmaozi/go-utils.git
git@gitee.com:szmaozi/go-utils.git
szmaozi
go-utils
go-utils
29e02a007caf

Search