Skip to content

Commit

Permalink
feat: add custom logger for v2
Browse files Browse the repository at this point in the history
  • Loading branch information
Skyenought committed Jun 18, 2024
1 parent d07355f commit a213ba8
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 0 deletions.
25 changes: 25 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 @@ -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 |
Expand Down
26 changes: 26 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,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
Expand Down
44 changes: 44 additions & 0 deletions nacos/v2/common/logger.go
Original file line number Diff line number Diff line change
@@ -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)
}
11 changes: 11 additions & 0 deletions nacos/v2/examples/logger/main.go
Original file line number Diff line number Diff line change
@@ -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!")
}

0 comments on commit a213ba8

Please sign in to comment.