From a213ba860921e479a42bcf13d8ac71a1dc9089ac Mon Sep 17 00:00:00 2001 From: Skyenought Date: Tue, 18 Jun 2024 09:31:18 +0800 Subject: [PATCH] feat: add custom logger for v2 --- nacos/v2/README.md | 25 ++++++++++++++++++ nacos/v2/README_CN.md | 26 +++++++++++++++++++ nacos/v2/common/logger.go | 44 ++++++++++++++++++++++++++++++++ nacos/v2/examples/logger/main.go | 11 ++++++++ 4 files changed, 106 insertions(+) create mode 100644 nacos/v2/common/logger.go create mode 100644 nacos/v2/examples/logger/main.go diff --git a/nacos/v2/README.md b/nacos/v2/README.md index dbc8c43..f230272 100644 --- a/nacos/v2/README.md +++ b/nacos/v2/README.md @@ -6,6 +6,12 @@ Nacos as service discovery for Hertz. ## How to use? +### +- The nacos/v2 version of hertz does not currently support creating multiple port examples in the same group multiple times. +- Service registration and discovery in nacos/v2 is compatible with previous versions. +- `CustomLogger` type in constant.ClientConfig has been removed in nacos-sdk-go v2. Instead, use the `(github.com/nacos-group/nacos-sdk-go/v2/common/logger).SetLogger` to customize the log. +- nacos/v2 only supports nacos 2.X version. + ### Server **[example/standard/server/main.go](examples/standard/server/main.go)** @@ -224,6 +230,25 @@ func main() { ``` +### Custom Logger + +**[examples/logger/main.go](examples/logger/main.go)** + +```go +package main + +import ( + "github.com/hertz-contrib/registry/nacos/v2/common" + "github.com/nacos-group/nacos-sdk-go/v2/common/logger" +) + +func main() { + logger.SetLogger(common.NewCustomNacosLogger()) + logger.Info("Hello, Nacos!") +} + +``` + ## Environment Variable | Environment Variable Name | Environment Variable Default Value | Environment Variable Introduction | diff --git a/nacos/v2/README_CN.md b/nacos/v2/README_CN.md index f7cfd23..ceb0246 100644 --- a/nacos/v2/README_CN.md +++ b/nacos/v2/README_CN.md @@ -6,6 +6,13 @@ ## 这个项目应当如何使用? +### 注意 + +- nacos/v2 版本中 hertz 目前不支持多次在同分组下创建多端口示例 +- nacos/v2 的服务注册与发现和先前的版本兼容 +- nacos-sdk-go v2 版本中 constant.ClientConfig 中 CustomLogger 类型被移除, 转而使用 (github.com/nacos-group/nacos-sdk-go/v2/common/logger).SetLogger 方法进行日志自定义 +- nacos/v2 只支持 nacos 2.X 版本 + ### 服务端 **[example/server/main.go](examples/standard/server/main.go)** @@ -76,6 +83,25 @@ func main() { } ``` +### 自定义日志 + +**[examples/logger/main.go](examples/logger/main.go)** + +```go +package main + +import ( + "github.com/hertz-contrib/registry/nacos/v2/common" + "github.com/nacos-group/nacos-sdk-go/v2/common/logger" +) + +func main() { + logger.SetLogger(common.NewCustomNacosLogger()) + logger.Info("Hello, Nacos!") +} + +``` + ## 如何运行示例 ? ### docker 运行 nacos-server diff --git a/nacos/v2/common/logger.go b/nacos/v2/common/logger.go new file mode 100644 index 0000000..afabaef --- /dev/null +++ b/nacos/v2/common/logger.go @@ -0,0 +1,44 @@ +package common + +import ( + "github.com/cloudwego/hertz/pkg/common/hlog" + "github.com/nacos-group/nacos-sdk-go/v2/common/logger" +) + +type customNacosLogger struct{} + +func NewCustomNacosLogger() logger.Logger { + return customNacosLogger{} +} + +func (c customNacosLogger) Info(args ...interface{}) { + hlog.Info(args) +} + +func (c customNacosLogger) Warn(args ...interface{}) { + hlog.Warn(args) +} + +func (c customNacosLogger) Error(args ...interface{}) { + hlog.Error(args) +} + +func (c customNacosLogger) Debug(args ...interface{}) { + hlog.Debug(args) +} + +func (c customNacosLogger) Infof(fmt string, args ...interface{}) { + hlog.Infof(fmt, args) +} + +func (c customNacosLogger) Warnf(fmt string, args ...interface{}) { + hlog.Warnf(fmt, args) +} + +func (c customNacosLogger) Errorf(fmt string, args ...interface{}) { + hlog.Errorf(fmt, args) +} + +func (c customNacosLogger) Debugf(fmt string, args ...interface{}) { + hlog.Debug(fmt, args) +} diff --git a/nacos/v2/examples/logger/main.go b/nacos/v2/examples/logger/main.go new file mode 100644 index 0000000..4b44cf8 --- /dev/null +++ b/nacos/v2/examples/logger/main.go @@ -0,0 +1,11 @@ +package main + +import ( + "github.com/hertz-contrib/registry/nacos/v2/common" + "github.com/nacos-group/nacos-sdk-go/v2/common/logger" +) + +func main() { + logger.SetLogger(common.NewCustomNacosLogger()) + logger.Info("Hello, Nacos!") +}