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

feat: add custom logger for nacos v2 #111

Merged
merged 4 commits into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
28 changes: 28 additions & 0 deletions nacos/v2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)**
Expand Down Expand Up @@ -78,6 +84,28 @@ client, err := client.NewClient()

```

### 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.InitLogger(logger.Config{
Level: "debug",
})
logger.SetLogger(common.NewCustomNacosLogger())
logger.Info("info")
}

```

## How to run example?

### run docker
Expand Down
29 changes: 29 additions & 0 deletions nacos/v2/README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)**
Expand Down Expand Up @@ -76,6 +83,28 @@ 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.InitLogger(logger.Config{
Level: "debug",
})
logger.SetLogger(common.NewCustomNacosLogger())
logger.Info("info")
}

```

## 如何运行示例 ?

### docker 运行 nacos-server
Expand Down
58 changes: 58 additions & 0 deletions nacos/v2/common/logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Copyright 2021 CloudWeGo Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package common

import (
"github.com/cloudwego/hertz/pkg/common/hlog"
v2 "github.com/nacos-group/nacos-sdk-go/v2/common/logger"
)

type customNacosLogger struct{}

func NewCustomNacosLogger() v2.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)
}
28 changes: 28 additions & 0 deletions nacos/v2/examples/logger/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright 2021 CloudWeGo Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import (
"github.com/hertz-contrib/registry/nacos/v2/common"
"github.com/nacos-group/nacos-sdk-go/v2/common/logger"
)

func main() {
logger.InitLogger(logger.Config{
Level: "debug",
})
logger.SetLogger(common.NewCustomNacosLogger())
logger.Info("info")
}
Loading