Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add configuration support for logger of grpcx.Server #3223

Merged
merged 3 commits into from
Jan 2, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion contrib/rpc/grpcx/grpcx_grpc_server_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package grpcx

import (
"context"
"fmt"
"google.golang.org/grpc"

"github.com/gogf/gf/v2/frame/g"
Expand Down Expand Up @@ -74,8 +75,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)
gqcn marked this conversation as resolved.
Show resolved Hide resolved
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
Expand Down
29 changes: 29 additions & 0 deletions contrib/rpc/grpcx/grpcx_unit_z_grpc_server_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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)
})

}
14 changes: 14 additions & 0 deletions contrib/rpc/grpcx/testdata/configuration/config.yaml
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions os/glog/glog_logger_chaining.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Loading