Browse Source

修改fslog接口

yjp 3 years ago
parent
commit
f4439589d4
2 changed files with 13 additions and 36 deletions
  1. 9 32
      fslog_instance.go
  2. 4 4
      fslog_instance_test.go

+ 9 - 32
fslog_instance.go

@@ -6,7 +6,6 @@ import (
 	"go.uber.org/zap/zapcore"
 	"os"
 	"path/filepath"
-	"strings"
 	"sync"
 )
 
@@ -62,51 +61,29 @@ func lazyInitialize() {
 			zapcore.NewCore(encoder, zapcore.AddSync(os.Stdout), chooseLoggerLever()),
 		)
 
-		logger = zap.New(core, zap.AddCaller())
+		logger = zap.New(core, zap.AddCaller(),zap.AddCallerSkip(1))
 	})
 }
 
-func Info(serviceName string, funcName string, callName string, message string) {
+func Info(message string) {
 	lazyInitialize()
-	callerFields := getCallerInfoForLog(serviceName, funcName, callName)
-	logger.Info(message, callerFields...)
+	logger.Info(message)
 }
 
-func Error(serviceName string, funcName string, callName string, err error) {
+func Error(err error) {
 	lazyInitialize()
-	callerFields := getCallerInfoForLog(serviceName, funcName, callName)
 	message:= fmt.Sprintf("\n%+v\n", err)
-	logger.Error(message, callerFields...)
+	logger.Error(message)
 }
 
-func Debug(serviceName string, funcName string, callName string, message string) {
+func Debug(message string) {
 	lazyInitialize()
-	callerFields := getCallerInfoForLog(serviceName, funcName, callName)
-	logger.Debug(message, callerFields...)
+	logger.Debug(message)
 }
 
-func Warn(serviceName string, funcName string, callName string, message string) {
+func Warn(message string) {
 	lazyInitialize()
-	callerFields := getCallerInfoForLog(serviceName, funcName, callName)
-	logger.Warn(message, callerFields...)
-}
-
-func getCallerInfoForLog(serviceName string, funcName string, callName string) []zap.Field {
-	fields := make([]zap.Field, 0)
-
-	if strings.Trim(serviceName, " ") != "" {
-		fields = append(fields, zap.String("service", serviceName))
-	}
-
-	if strings.Trim(funcName, " ") != "" {
-		fields = append(fields, zap.String("function", funcName))
-	}
-
-	if strings.Trim(callName, " ") != "" {
-		fields = append(fields, zap.String("call", callName))
-	}
-
-	return fields
+	logger.Warn(message)
 }
 
 func pathExists(path string) bool {

+ 4 - 4
fslog_instance_test.go

@@ -8,8 +8,8 @@ import (
 func TestFSLogInstanceToConsole(t *testing.T) {
 	LoggerLever = LoggerLevelDebug
 
-	Error("demo", "TestFSLogInstanceToConsole", "test", errors.New("error test"))
-	Debug("demo", "TestFSLogInstanceToConsole", "test", "debug test")
-	Info("demo", "TestFSLogInstanceToConsole", "test", "info test")
-	Warn("demo", "TestFSLogInstanceToConsole", "test", "warning test")
+	Error(errors.New("error test"))
+	Debug("debug test")
+	Info("info test")
+	Warn("warning test")
 }