Skip to content

Commit

Permalink
Fix #6 use zap logger (#7)
Browse files Browse the repository at this point in the history
* Fix #6 use zap logger
  • Loading branch information
zhangxu19830126 authored Aug 6, 2021
1 parent 3c588c9 commit 5d8f81b
Show file tree
Hide file tree
Showing 119 changed files with 10,442 additions and 266 deletions.
25 changes: 25 additions & 0 deletions .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
**What type of PR is this?**

- [ ] API-change
- [ ] BUG
- [ ] Improvement
- [ ] Documentation
- [ ] Feature
- [ ] Test and CI
- [ ] Code Refactoring

**Which issue(s) this PR fixes:**

Fixes #

**What this PR does / why we need it:**

Not Available

**Special notes for your reviewer:**

Not Available

**Additional documentation (e.g. design docs, usage docs, etc.):**

Not Available
38 changes: 17 additions & 21 deletions application.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"sync"
"sync/atomic"
"time"

"go.uber.org/zap"
)

var (
Expand Down Expand Up @@ -113,7 +115,7 @@ func (s *server) Start() error {
select {
case <-s.startCh:
atomic.StoreInt32(&s.state, stateStarted)
s.opts.sessionOpts.logger.Infof("net application started")
s.opts.sessionOpts.logger.Info("goetty application started")
return nil
case err := <-c:
return err
Expand Down Expand Up @@ -235,9 +237,9 @@ func (s *server) doStart() error {
const size = 64 << 10
rBuf := make([]byte, size)
rBuf = rBuf[:runtime.Stack(rBuf, false)]
s.opts.sessionOpts.logger.Errorf("goetty: connection painc %+v, stack:\n%s",
err,
rBuf)
s.opts.sessionOpts.logger.Error("connection painc",
zap.Any("err", err),
zap.String("stack", string(rBuf)))
}
}()

Expand All @@ -257,38 +259,32 @@ func (s *server) doStart() error {
}

func (s *server) doConnection(rs IOSession) error {
received := uint64(0)
logger := s.opts.sessionOpts.logger.With(zap.Uint64("session-id", rs.ID()),
zap.String("addr", rs.RemoteAddr()))

logger.Info("session connected")

s.opts.sessionOpts.logger.Infof("session %d[%s] connected",
rs.ID(),
rs.RemoteAddr())
received := uint64(0)
for {
msg, err := rs.Read()
if err != nil {
if err == io.EOF {
return nil
}

s.opts.sessionOpts.logger.Errorf("session %d[%s] read failed with %+v",
rs.ID(),
rs.RemoteAddr(),
err)
logger.Info("session read failed",
zap.Error(err))
return err
}

s.opts.sessionOpts.logger.Debugf("session %d[%s] read %+v",
rs.ID(),
rs.RemoteAddr(),
msg)

logger.Debug("session read", zap.Any("msg", msg))
received++

err = s.handleFunc(rs, msg, received)
if err != nil {
if s.opts.errorMsgFactory == nil {
s.opts.sessionOpts.logger.Errorf("session %d[%s] handle failed with %+v, close session",
rs.ID(),
rs.RemoteAddr(),
err)
logger.Error("session handle failed, close this session",
zap.Error(err))
return err
}

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ go 1.16
require (
github.com/fagongzi/util v0.0.0-20201116094402-221cc40c4593
github.com/stretchr/testify v1.7.0
go.uber.org/zap v1.18.1
)
43 changes: 40 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,13 +1,50 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8=
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fagongzi/util v0.0.0-20201116094402-221cc40c4593 h1:l7QIx7pHtn00neDaA61F1QYVSx/GxRXeuOQjwWyhHqc=
github.com/fagongzi/util v0.0.0-20201116094402-221cc40c4593/go.mod h1:jYDIbpaqHXCCQ7QIDXRVfsQYAGKSNNb6N8BPTgdpcdE=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
go.uber.org/atomic v1.7.0 h1:ADUqmZGgLDDfbSL9ZmPxKTybcoEYHgpYfELNoN+7hsw=
go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
go.uber.org/goleak v1.1.10 h1:z+mqJhf6ss6BSfSM671tgKyZBFPTTJM+HLxnhPC3wu0=
go.uber.org/goleak v1.1.10/go.mod h1:8a7PlsEVH3e/a/GLqe5IIrQx6GzcnRmZEufDUTk4A7A=
go.uber.org/multierr v1.6.0 h1:y6IPFStTAIT5Ytl7/XYmHvzXQ7S3g/IeZW9hyZ5thw4=
go.uber.org/multierr v1.6.0/go.mod h1:cdWPpRnG4AhwMwsgIHip0KRBQjJy5kYEpYjJxpXp9iU=
go.uber.org/zap v1.18.1 h1:CSUJ2mjFszzEWt4CdKISEuChVIXGBn3lAPwkRGyVrc4=
go.uber.org/zap v1.18.1/go.mod h1:xg/QME4nWcxGxrpdeYfq7UvYrLh66cuVKdrbD1XF/NI=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/lint v0.0.0-20190930215403-16217165b5de h1:5hukYrvBGR8/eNkX5mdUezrA6JiaEZDtJb9Ei+1LlBs=
golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
golang.org/x/tools v0.0.0-20191108193012-7d206e10da11 h1:Yq9t9jnGoR+dBuitxdo9l6Q7xh/zOyNnYUtDKaQ3x0E=
golang.org/x/tools v0.0.0-20191108193012-7d206e10da11/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
33 changes: 5 additions & 28 deletions log.go
Original file line number Diff line number Diff line change
@@ -1,35 +1,12 @@
package goetty

import (
"log"
"go.uber.org/zap"
)

// Logger logger
type Logger interface {
Infof(format string, v ...interface{})
Debugf(format string, v ...interface{})
Errorf(format string, v ...interface{})
Fatalf(format string, v ...interface{})
}

func newStdLog() Logger {
return &stdLog{}
}

type stdLog struct{}

func (l *stdLog) Debugf(format string, v ...interface{}) {
log.Printf(format, v...)
}

func (l *stdLog) Infof(format string, v ...interface{}) {
log.Printf(format, v...)
}

func (l *stdLog) Errorf(format string, v ...interface{}) {
log.Printf(format, v...)
}
var logger = zap.NewNop()

func (l *stdLog) Fatalf(format string, v ...interface{}) {
log.Panicf(format, v...)
// UseLogger use logger
func UseLogger(zapLogger *zap.Logger) {
logger = zapLogger
}
7 changes: 4 additions & 3 deletions option.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"time"

"github.com/fagongzi/goetty/codec"
"go.uber.org/zap"
)

const (
Expand Down Expand Up @@ -76,7 +77,7 @@ func (opts *appOptions) adjust() {
type Option func(*options)

type options struct {
logger Logger
logger *zap.Logger
decoder codec.Decoder
encoder codec.Encoder
readBufSize, writeBufSize int
Expand Down Expand Up @@ -105,12 +106,12 @@ func (opts *options) adjust() {
}

if opts.logger == nil {
opts.logger = newStdLog()
opts.logger = logger
}
}

// WithLogger set logger
func WithLogger(value Logger) Option {
func WithLogger(value *zap.Logger) Option {
return func(opts *options) {
opts.logger = value
}
Expand Down
18 changes: 11 additions & 7 deletions session.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/fagongzi/goetty/buf"
"github.com/fagongzi/goetty/queue"
"go.uber.org/zap"
)

var (
Expand Down Expand Up @@ -71,6 +72,7 @@ type baseIO struct {
attrs sync.Map
disableConnect bool
asyncQueue queue.Queue
logger *zap.Logger
}

// NewIOSession create a new io session
Expand All @@ -80,7 +82,6 @@ func NewIOSession(opts ...Option) IOSession {

func newBaseIO(id uint64, conn net.Conn, opts ...Option) IOSession {
bopts := &options{}

for _, opt := range opts {
opt(bopts)
}
Expand All @@ -91,10 +92,11 @@ func newBaseIO(id uint64, conn net.Conn, opts ...Option) IOSession {

func newBaseIOWithOptions(id uint64, conn net.Conn, opts *options) IOSession {
bio := &baseIO{
id: id,
opts: opts,
in: buf.NewByteBuf(opts.readBufSize),
out: buf.NewByteBuf(opts.writeBufSize),
id: id,
opts: opts,
in: buf.NewByteBuf(opts.readBufSize),
out: buf.NewByteBuf(opts.writeBufSize),
logger: logger,
}

if conn != nil {
Expand Down Expand Up @@ -330,7 +332,7 @@ func (bio *baseIO) writeLoop(q queue.Queue) {
for {
n, err := q.Get(bio.opts.asyncFlushBatch, items)
if nil != err {
bio.opts.logger.Fatalf("BUG: can not failed")
bio.logger.Panic("BUG: can not failed")
}

for i := int64(0); i < n; i++ {
Expand All @@ -343,7 +345,7 @@ func (bio *baseIO) writeLoop(q queue.Queue) {

err = bio.Flush()
if err != nil {
bio.opts.logger.Errorf("flush messages failed with %+v, closed this session", err)
bio.logger.Error("flush messages failed, closed this session", zap.Error(err))
return
}
}
Expand Down Expand Up @@ -389,6 +391,8 @@ func (bio *baseIO) initConn(conn net.Conn) {
bio.remoteIP = strings.Split(bio.remoteAddr, ":")[0]
}

bio.logger = bio.opts.logger.With(zap.Uint64("id", bio.id),
zap.String("conn", bio.remoteAddr))
bio.opts.connOptionFunc(bio.conn)
if bio.opts.asyncWrite {
bio.asyncQueue = queue.New(64)
Expand Down
2 changes: 1 addition & 1 deletion vendor/github.com/davecgh/go-spew/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 5d8f81b

Please sign in to comment.