diff --git a/contrib/rpc/grpcx/grpcx_grpc_server_config.go b/contrib/rpc/grpcx/grpcx_grpc_server_config.go index f5e7c878ef1..f88d8ddf410 100644 --- a/contrib/rpc/grpcx/grpcx_grpc_server_config.go +++ b/contrib/rpc/grpcx/grpcx_grpc_server_config.go @@ -8,6 +8,7 @@ package grpcx import ( "context" + "fmt" "google.golang.org/grpc" @@ -75,8 +76,26 @@ func (s modServer) NewConfig() *GrpcServerConfig { // Reading configuration file and updating the configured keys. if g.Cfg().Available(ctx) { // Server attributes configuration. - if err = g.Cfg().MustGet(ctx, configNodeNameGrpcServer).Struct(&config); err != nil { + serverConfigMap := g.Cfg().MustGet(ctx, configNodeNameGrpcServer).Map() + if len(serverConfigMap) == 0 { + return config + } + if err = gconv.Struct(serverConfigMap, &config); err != nil { g.Log().Error(ctx, err) + return config + } + // Server logger configuration checks. + serverLoggerConfigMap := g.Cfg().MustGet( + ctx, + fmt.Sprintf(`%s.logger`, configNodeNameGrpcServer), + ).Map() + if len(serverLoggerConfigMap) == 0 && len(serverConfigMap) > 0 { + serverLoggerConfigMap = gconv.Map(serverConfigMap["logger"]) + } + if len(serverLoggerConfigMap) > 0 { + if err = config.Logger.SetConfigWithMap(serverLoggerConfigMap); err != nil { + panic(err) + } } } return config diff --git a/contrib/rpc/grpcx/grpcx_unit_z_grpc_server_config_test.go b/contrib/rpc/grpcx/grpcx_unit_z_grpc_server_config_test.go index 21ace494f1f..fd4f7c27816 100644 --- a/contrib/rpc/grpcx/grpcx_unit_z_grpc_server_config_test.go +++ b/contrib/rpc/grpcx/grpcx_unit_z_grpc_server_config_test.go @@ -7,9 +7,13 @@ package grpcx import ( + "fmt" "testing" "time" + "github.com/gogf/gf/v2/debug/gdebug" + "github.com/gogf/gf/v2/os/gfile" + "github.com/gogf/gf/v2/os/gtime" "github.com/gogf/gf/v2/test/gtest" "github.com/gogf/gf/v2/text/gstr" ) @@ -70,3 +74,28 @@ func Test_Grpcx_Grpc_Server_Config(t *testing.T) { } }) } + +func Test_Grpcx_Grpc_Server_Config_Logger(t *testing.T) { + var ( + pwd = gfile.Pwd() + configDir = gfile.Join(gdebug.CallerDirectory(), "testdata", "configuration") + ) + gtest.C(t, func(t *gtest.T) { + err := gfile.Chdir(configDir) + t.AssertNil(err) + defer gfile.Chdir(pwd) + + s := Server.New() + s.Start() + time.Sleep(time.Millisecond * 100) + defer s.Stop() + + var ( + logFilePath = fmt.Sprintf("/tmp/log/%s.log", gtime.Now().Format("Y-m-d")) + logFileContent = gfile.GetContents(logFilePath) + ) + t.Assert(gfile.Exists(logFilePath), true) + t.Assert(gstr.Contains(logFileContent, "TestLogger "), true) + }) + +} diff --git a/contrib/rpc/grpcx/testdata/configuration/config.yaml b/contrib/rpc/grpcx/testdata/configuration/config.yaml new file mode 100644 index 00000000000..199541bef7e --- /dev/null +++ b/contrib/rpc/grpcx/testdata/configuration/config.yaml @@ -0,0 +1,14 @@ +grpc: + name: "demo" # 服务名称 + address: ":8000" # 自定义服务监听地址 + logPath: "./log" # 日志存储目录路径 + logStdout: true # 日志是否输出到终端 + errorLogEnabled: true # 是否开启错误日志记录 + accessLogEnabled: true # 是否开启访问日志记录 + errorStack: true # 当产生错误时,是否记录错误堆栈 + logger: + path: "/tmp/log/" # 日志文件路径。默认为空,表示关闭,仅输出到终端 + file: "{Y-m-d}.log" # 日志文件格式。默认为"{Y-m-d}.log" + prefix: "TestLogger" # 日志内容输出前缀。默认为空 + level: "all" # 日志输出级别 + stdout: false # 日志是否同时输出到终端。默认true \ No newline at end of file diff --git a/os/glog/glog_logger_chaining.go b/os/glog/glog_logger_chaining.go index 29ebade2f0d..72a90fadeb9 100644 --- a/os/glog/glog_logger_chaining.go +++ b/os/glog/glog_logger_chaining.go @@ -65,6 +65,9 @@ func (l *Logger) Cat(category string) *Logger { // File is a chaining function, // which sets file name `pattern` for the current logging content output. func (l *Logger) File(file string) *Logger { + if file == "" { + return l + } logger := (*Logger)(nil) if l.parent == nil { logger = l.Clone()