-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
121 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# Custom Logger | ||
|
||
From v4.4.0, we introduced `zap.Logger` which is a widely used, production | ||
ready [logger component](https://github.com/uber-go/zap). The `log_level` field in config is deprecated, you can | ||
initialize your own logger and pass it by `context`. | ||
|
||
## Package log | ||
|
||
The logger helper methods are defined in `log` package, which contains | ||
`func ContextWithLogger(ctx context.Context, l *zap.Logger) context.Context` and | ||
`func FromContext(ctx context.Context) *zap.Logger` two methods. | ||
|
||
You can conduct your own `*zap.Logger` by methods such as `zap.New`, `zap.NewDevelopment`, `zap.NewProducton` and so on, | ||
and set it into `context` by `ContextWithLogger`, then you can pass this `context` into API function | ||
like `PutObjectWithContext`. | ||
|
||
## Default logger | ||
|
||
Usually, we get logger from `context` by method `FromContext`. If `context` is `nil` or no `logger` is set before, | ||
a default logger is returned, which is initialized by `zap.NewProduction` and `LevelWarn` is set. | ||
[Here](https://github.com/qingstor/qingstor-sdk-go/blob/master/log/context.go#L39) is the detail. | ||
|
||
## Use custom logger | ||
|
||
You can also customize you own logger depend on your scenario. Then conduct the `context` by `ContextWithLogger`. | ||
|
||
Here are some examples: | ||
|
||
```go | ||
package main | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/qingstor/qingstor-sdk-go/v4/log" | ||
"go.uber.org/zap" | ||
) | ||
|
||
func main() { | ||
// ignore the process of conducting bucketService | ||
|
||
// no context passed, will use default logger | ||
bucketService.PutObject(objectKey, input) | ||
|
||
options := []zap.Option{ | ||
// customize your own options | ||
} | ||
logger, _ := zap.NewDevelopment(options...) | ||
ctx := log.ContextWithLogger(context.Background(), logger) | ||
|
||
// logger set above will be used | ||
bucketService.PutObjectWithContext(ctx, objectKey, input) | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# 自定义日志组件 | ||
|
||
从 v4.4.0 版本开始,我们引入了 `zap.Logger` 组件,这是一个被广泛应用于生产环境的[日志组件](https://github.com/uber-go/zap). | ||
并且我们废弃了配置文件中的 `log_level` 字段,你可以初始化自定义的 logger,并且通过 `context` 来进行传递。 | ||
|
||
## log 包 | ||
|
||
SDK 中,日志组件的帮助方法被定义在了 `log` 包中,这个包主要包含了以下两个方法: | ||
`func ContextWithLogger(ctx context.Context, l *zap.Logger) context.Context` 和 | ||
`func FromContext(ctx context.Context) *zap.Logger`. | ||
|
||
你可以构建自己的 `*zap.Logger` 实例,通过 `zap.New`, `zap.NewDevelopment`, `zap.NewProducton` 等方法, | ||
然后将该实例设置在 `context` 中,通过 `ContextWithLogger` 方法,之后你可以将得到的 `context` 作为参数传至 API 请求方法中,例如 | ||
`PutObjectWithContext`. | ||
|
||
## 默认的 logger | ||
|
||
通常情况下,我们会通过 `FromContext` 方法从 `context` 中获取 `*zap.Logger` 实例。但如果 `context` 为 `nil`,或者之前没有实例被设置, | ||
我们会返回一个默认的实例,该实例是通过 `zap.NewProduction` 方法初始化的,并被设置为 `LevelWarn` 等级。 | ||
点击 [这里](https://github.com/qingstor/qingstor-sdk-go/blob/master/log/context.go#L39) 可以看到具体的函数逻辑。 | ||
|
||
## 使用自定义的 logger | ||
|
||
你也可以根据实际场景需要,自定义一个 `*zap.Logger` 实例,并通过 `ContextWithLogger` 来构建 `context`. | ||
|
||
示例代码如下: | ||
|
||
```go | ||
package main | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/qingstor/qingstor-sdk-go/v4/log" | ||
"go.uber.org/zap" | ||
) | ||
|
||
func main() { | ||
// 忽略构造 bucketService 的过程 | ||
|
||
// 没有 context 参数,将会使用默认的 logger | ||
bucketService.PutObject(objectKey, input) | ||
|
||
options := []zap.Option{ | ||
// 自定义你的 logger 选项 | ||
} | ||
logger, _ := zap.NewDevelopment(options...) | ||
ctx := log.ContextWithLogger(context.Background(), logger) | ||
|
||
// 上边构造的 logger 将会被使用 | ||
bucketService.PutObjectWithContext(ctx, objectKey, input) | ||
} | ||
``` |