Skip to content

Commit d602c03

Browse files
committed
add log hooks
1 parent e0bdc53 commit d602c03

File tree

6 files changed

+50
-3
lines changed

6 files changed

+50
-3
lines changed

pkg/ggorm/test_sqlite.db

0 Bytes
Binary file not shown.

pkg/logger/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,16 @@ Logger library wrapped in [zap](https://github.com/uber-go/zap).
3434
logger.Info("this is info")
3535
logger.Warn("this is warn", logger.String("foo","bar"), logger.Int("size",10), logger.Any("obj",obj))
3636
logger.Error("this is error", logger.Err(err), logger.String("foo","bar"))
37+
38+
// (3) with hooks
39+
logger.Init(
40+
logger.WithLevel("info"),
41+
logger.WithHooks(func(entry zapcore.Entry) error {
42+
if strings.Contains(entry.Message, "error") {
43+
fmt.Println("it contains error message")
44+
}
45+
return nil
46+
}),
47+
)
48+
logger.Error("this is error", logger.Err(err), logger.String("foo","bar"))
3749
```

pkg/logger/logger.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ func getLogger() *zap.Logger {
3838
// print the debug level log in the terminal, example: Init()
3939
// print the info level log in the terminal, example: Init(WithLevel("info"))
4040
// print the json format, debug level log in the terminal, example: Init(WithFormat("json"))
41+
// log with hooks, example: Init(WithHooks(func(zapcore.Entry) error{return nil}))
4142
// output the log to the file out.log, using the default cut log-related parameters, debug-level log, example: Init(WithSave())
4243
// output the log to the specified file, custom set the log file cut log parameters, json format, debug level log, example:
4344
// Init(
@@ -72,6 +73,10 @@ func Init(opts ...Option) (*zap.Logger, error) {
7273
str = fmt.Sprintf("initialize logger finish, config is output to 'file', format=%s, level=%s, file=%s", encoding, levelName, o.fileConfig.filename)
7374
}
7475

76+
if len(o.hooks) > 0 {
77+
zapLog = zapLog.WithOptions(zap.Hooks(o.hooks...))
78+
}
79+
7580
defaultLogger = zapLog
7681
Info(str)
7782

pkg/logger/logger_test.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@ package logger
33
import (
44
"fmt"
55
"os"
6+
"strings"
67
"testing"
78
"time"
9+
10+
"go.uber.org/zap/zapcore"
811
)
912

1013
func printInfo() {
@@ -52,7 +55,7 @@ func TestInit(t *testing.T) {
5255
{
5356
name: "terminal json info",
5457
args: args{[]Option{
55-
WithFormat("json"), WithLevel(levelInfo),
58+
WithFormat("json"), WithLevel("info"),
5659
}},
5760
wantErr: false,
5861
},
@@ -63,6 +66,20 @@ func TestInit(t *testing.T) {
6366
}},
6467
wantErr: false,
6568
},
69+
{
70+
name: "with hooks info",
71+
args: args{[]Option{
72+
WithFormat("json"),
73+
WithLevel("info"),
74+
WithHooks(func(entry zapcore.Entry) error {
75+
if strings.Contains(entry.Message, "this is error") {
76+
fmt.Println("it contains error message")
77+
}
78+
return nil
79+
}),
80+
}},
81+
wantErr: false,
82+
},
6683
{
6784
name: "file json debug",
6885
args: args{[]Option{

pkg/logger/option.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package logger
22

3-
import "strings"
3+
import (
4+
"strings"
5+
6+
"go.uber.org/zap/zapcore"
7+
)
48

59
var (
610
defaultLevel = "debug" // output log levels debug, info, warn, error, default is debug
@@ -20,6 +24,8 @@ type options struct {
2024
isSave bool
2125

2226
fileConfig *fileOptions
27+
28+
hooks []func(zapcore.Entry) error
2329
}
2430

2531
func defaultOptions() *options {
@@ -73,6 +79,13 @@ func WithSave(isSave bool, opts ...FileOption) Option {
7379
}
7480
}
7581

82+
// WithHooks set the log hooks
83+
func WithHooks(hooks ...func(zapcore.Entry) error) Option {
84+
return func(o *options) {
85+
o.hooks = hooks
86+
}
87+
}
88+
7689
// ------------------------------------------------------------------------------------------
7790

7891
type fileOptions struct {

pkg/sql2code/parser/template.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ option (grpc.gateway.protoc_gen_openapiv2.options.openapiv2_swagger) = {
234234
type: TYPE_API_KEY;
235235
in: IN_HEADER;
236236
name: "Authorization";
237-
description: "Input a \"Bearer your-jwt-token\" to Value";
237+
description: "Type Bearer your-jwt-token to Value";
238238
}
239239
}
240240
}

0 commit comments

Comments
 (0)