forked from fuyibing/log
-
Notifications
You must be signed in to change notification settings - Fork 0
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
2 changed files
with
84 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# logger | ||
|
||
1. [x] 包名: `github.com/fuyibing/log`. | ||
1. [x] 版本: `v2` | ||
|
||
> 本包在导入后, 扫描 `tmp/log.yaml` 或 `config/log.yaml` 并初始化, | ||
> 详细配置参见本包的 `config/log.yaml` 注释. | ||
### 通用用法. | ||
|
||
``` | ||
log.Info("message.") | ||
log.Infof("message %d: %s.", 1, "parse", "example") | ||
``` | ||
|
||
### 带请求链 | ||
|
||
``` | ||
ctx := log.NewContext() | ||
log.Debugfc(ctx, "debug fc") | ||
log.Infofc(ctx, "info fc") | ||
log.Warnfc(ctx, "warn fc") | ||
log.Errorfc(ctx, "error fc") | ||
``` | ||
|
||
### 在IRIS框架中 | ||
|
||
> 首先通过中间件, 在入口注册请求链. | ||
```text | ||
log.BindRequest(iris.Request()) | ||
``` | ||
|
||
> 在IRIS框架中使用. | ||
```text | ||
ctx := iris.Request().Context() | ||
log.Debugfc(ctx, "debug fc") | ||
log.Infofc(ctx, "info fc") | ||
log.Warnfc(ctx, "warn fc") | ||
log.Errorfc(ctx, "error fc") | ||
``` | ||
|
||
### 自定义日志处理. | ||
|
||
```text | ||
log.Config.SetHandler(func(line interfaces.LineInterface) { | ||
println("handler: ", line.SpanVersion(), line.Content()) | ||
}) | ||
ctx := log.NewContext() | ||
log.Debugfc(ctx, "debug fc") | ||
log.Infofc(ctx, "info fc") | ||
log.Warnfc(ctx, "warn fc") | ||
log.Errorfc(ctx, "error fc") | ||
``` | ||
|
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,25 @@ | ||
// author: wsfuyibing <[email protected]> | ||
// date: 2021-02-22 | ||
|
||
package tests | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/fuyibing/log" | ||
"github.com/fuyibing/log/interfaces" | ||
) | ||
|
||
func TestHandler(t *testing.T) { | ||
|
||
log.Config.SetHandler(func(line interfaces.LineInterface) { | ||
println("handler: ", line.SpanVersion(), line.Content()) | ||
}) | ||
|
||
ctx := log.NewContext() | ||
|
||
log.Client.Debugfc(ctx, "debug fc") | ||
log.Client.Infofc(ctx, "info fc") | ||
log.Client.Warnfc(ctx, "warn fc") | ||
log.Client.Errorfc(ctx, "error fc") | ||
} |