picgo/corelib/log.go

104 lines
2.2 KiB
Go
Raw Permalink Normal View History

2024-07-12 20:32:33 +08:00
package corelib
2024-07-14 20:31:27 +08:00
//import (
// "go.uber.org/zap"
// "go.uber.org/zap/zapcore"
//)
//var Logger *zap.SugaredLogger
//
//func NewLogger() {
// writeSyncer := getLogWriter()
// encoder := getEncoder()
// core := zapcore.NewCore(encoder, writeSyncer, zapcore.DebugLevel)
// logger := zap.New(core, zap.AddCaller())
// Logger = logger.Sugar()
//}
//
//func getEncoder() zapcore.Encoder {
// encoderConfig := zap.NewProductionEncoderConfig()
// encoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
// encoderConfig.EncodeLevel = zapcore.CapitalLevelEncoder
// return zapcore.NewConsoleEncoder(encoderConfig)
//}
//
//func getLogWriter() zapcore.WriteSyncer {
// var (
// err error
// currentDir string
// )
// // 获取当前工作目录
// if currentDir, err = os.Getwd(); err != nil {
// log.Panic("获取当前工作目录失败: ", err)
// }
// logPath := path.Join(currentDir, configs.Settings.Server.LogPath)
// // Filename: 日志文件的位置
// // MaxSize在进行切割之前日志文件的最大大小以 MB 为单位)
// // MaxBackups保留旧文件的最大个数
// // MaxAges保留旧文件的最大天数
// // Compress是否压缩 / 归档旧文件
// lumberJackLogger := &lumberjack.Logger{
// Filename: logPath,
// MaxSize: 50,
// MaxBackups: 10,
// MaxAge: 1,
// Compress: false,
// }
// return zapcore.AddSync(lumberJackLogger)
//}
2024-07-12 20:32:33 +08:00
import (
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
var Logger *zap.SugaredLogger
func NewLogger() {
2024-07-14 20:31:27 +08:00
config := zap.NewProductionConfig()
config.OutputPaths = []string{
"stdout",
"tmp/picgo-info.log",
}
config.ErrorOutputPaths = []string{
"stderr",
"tmp/picgo-error.log",
}
config.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
logger, err := config.Build()
if err != nil {
panic(err)
}
2024-07-12 20:32:33 +08:00
Logger = logger.Sugar()
2024-07-14 20:31:27 +08:00
2024-07-12 20:32:33 +08:00
}
2024-07-14 20:31:27 +08:00
func Panic(args ...interface{}) {
Logger.Panic(args...)
}
func Debug(args ...interface{}) {
Logger.Debug(args...)
2024-07-12 20:32:33 +08:00
}
2024-07-14 20:31:27 +08:00
func Info(args ...interface{}) {
Logger.Info(args...)
}
func Warn(args ...interface{}) {
Logger.Warn(args...)
}
func Error(args ...interface{}) {
Logger.Error(args...)
}
func Fatal(args ...interface{}) {
Logger.Fatal(args...)
}
func Sync() {
Logger.Sync()
2024-07-12 20:32:33 +08:00
}