yjp 3 年之前
父節點
當前提交
41c22b3abe
共有 3 個文件被更改,包括 16 次插入21 次删除
  1. 1 0
      .gitignore
  2. 15 5
      fslog_instance.go
  3. 0 16
      logs/log

+ 1 - 0
.gitignore

@@ -14,5 +14,6 @@
 .idea/
 .idea/
 file/
 file/
 **/bin
 **/bin
+**/logs
 
 
 /vendor/
 /vendor/

+ 15 - 5
fslog_instance.go

@@ -6,6 +6,7 @@ import (
 	"os"
 	"os"
 	"path/filepath"
 	"path/filepath"
 	"runtime/debug"
 	"runtime/debug"
+	"strings"
 )
 )
 
 
 var logger *zap.Logger
 var logger *zap.Logger
@@ -27,7 +28,6 @@ func init() {
 	}
 	}
 
 
 	developmentEncoderConfig := zap.NewDevelopmentEncoderConfig()
 	developmentEncoderConfig := zap.NewDevelopmentEncoderConfig()
-	developmentEncoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder
 
 
 	encoder := zapcore.NewConsoleEncoder(developmentEncoderConfig)
 	encoder := zapcore.NewConsoleEncoder(developmentEncoderConfig)
 	core := zapcore.NewTee(
 	core := zapcore.NewTee(
@@ -59,11 +59,21 @@ func Warning(serviceName string, funcName string, callName string, message strin
 }
 }
 
 
 func getCallerInfoForLog(serviceName string, funcName string, callName string) []zap.Field {
 func getCallerInfoForLog(serviceName string, funcName string, callName string) []zap.Field {
-	return []zap.Field{
-		zap.String("service", serviceName),
-		zap.String("function", funcName),
-		zap.String("call", callName),
+	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
 }
 }
 
 
 func pathExists(path string) bool {
 func pathExists(path string) bool {

+ 0 - 16
logs/log

@@ -1,16 +0,0 @@
-2022-09-30T15:51:10.707+0800	ERROR	fslog/fslog_instance.go:48	error test
-goroutine 6 [running]:
-runtime/debug.Stack()
-	/usr/local/go/src/runtime/debug/stack.go:24 +0x65
-git.sxidc.com/service-supports/fslog.Error({0x5fcdb3?, 0xc00004e760?}, {0x6035a4?, 0x4cbb34?}, {0x5fce4f?, 0x0?}, {0x5fe50b, 0xa})
-	/home/yjp/go-projects/src/ServiceSupports/fslog/fslog_instance.go:48 +0x252
-git.sxidc.com/service-supports/fslog.TestFSLogInstanceToConsole(0x0?)
-	/home/yjp/go-projects/src/ServiceSupports/fslog/fslog_instance_test.go:8 +0x4c
-testing.tRunner(0xc00013e680, 0x6266d0)
-	/usr/local/go/src/testing/testing.go:1446 +0x10b
-created by testing.(*T).Run
-	/usr/local/go/src/testing/testing.go:1493 +0x35f
-	{"service": "demo", "function": "TestFSLogInstanceToConsole", "call": "test"}
-2022-09-30T15:51:10.707+0800	DEBUG	fslog/fslog_instance.go:53	debug test	{"service": "demo", "function": "TestFSLogInstanceToConsole", "call": "test"}
-2022-09-30T15:51:10.707+0800	INFO	fslog/fslog_instance.go:43	info test	{"service": "demo", "function": "TestFSLogInstanceToConsole", "call": "test"}
-2022-09-30T15:51:10.707+0800	WARN	fslog/fslog_instance.go:58	warning test	{"service": "demo", "function": "TestFSLogInstanceToConsole", "call": "test"}