Skip to content

Commit

Permalink
[go-client] integrated logrus, testify and go mod for pulsar-cl…
Browse files Browse the repository at this point in the history
…ient-go (apache#3443)

* [go-clint]add producer.GetLastSequenceID in Go client

Signed-off-by: xiaolong.ran <[email protected]>

* fix comments and use LastSequenceID instead of GetLastSequenceID

Signed-off-by: xiaolong.ran <[email protected]>

* fix consumer and producer test error

Signed-off-by: xiaolong.ran <[email protected]>

* [pulsar-client-go]provide setSequenceID interface

Signed-off-by: xiaolong.ran <[email protected]>

* add test case for setSequenceID

Signed-off-by: xiaolong.ran <[email protected]>

* rename ID -> SequenceID

Signed-off-by: xiaolong.ran <[email protected]>

* support issue apache#3417

Signed-off-by: xiaolong.ran <[email protected]>

* fix some issues about log and go mod

Signed-off-by: xiaolong.ran <[email protected]>
  • Loading branch information
wolfstudy authored and merlimat committed Jan 29, 2019
1 parent 7e1c804 commit 8b74640
Show file tree
Hide file tree
Showing 16 changed files with 419 additions and 213 deletions.
11 changes: 11 additions & 0 deletions pulsar-client-go/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# IDE generated files
.idea/

# dep generated files
vendor

# Mac swap file
*.DS_Store

# Linux swap file
*.swp
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ package main

import (
"fmt"
log "github.com/apache/pulsar/pulsar-client-go/logutil"
"github.com/apache/pulsar/pulsar-client-go/pulsar"
"log"
)

func main() {
Expand Down
2 changes: 1 addition & 1 deletion pulsar-client-go/examples/consumer/consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ package main
import (
"context"
"fmt"
log "github.com/apache/pulsar/pulsar-client-go/logutil"
"github.com/apache/pulsar/pulsar-client-go/pulsar"
"log"
)

func main() {
Expand Down
3 changes: 2 additions & 1 deletion pulsar-client-go/examples/producer/producer.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ package main
import (
"context"
"fmt"

log "github.com/apache/pulsar/pulsar-client-go/logutil"
"github.com/apache/pulsar/pulsar-client-go/pulsar"
"log"
)

func main() {
Expand Down
2 changes: 1 addition & 1 deletion pulsar-client-go/examples/reader/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ package main
import (
"context"
"fmt"
log "github.com/apache/pulsar/pulsar-client-go/logutil"
"github.com/apache/pulsar/pulsar-client-go/pulsar"
"log"
)

func main() {
Expand Down
9 changes: 9 additions & 0 deletions pulsar-client-go/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module github.com/apache/pulsar/pulsar-client-go

require (
github.com/BurntSushi/toml v0.3.1 // indirect
github.com/sirupsen/logrus v1.3.0
github.com/stretchr/testify v1.3.0
gopkg.in/natefinch/lumberjack.v2 v2.0.0
gopkg.in/yaml.v2 v2.2.2 // indirect
)
26 changes: 26 additions & 0 deletions pulsar-client-go/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
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/konsorten/go-windows-terminal-sequences v1.0.1 h1:mweAR1A6xJ3oS2pRaGiHgQ4OO8tzTaLawm8vnODuwDk=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
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/sirupsen/logrus v1.3.0 h1:hI/7Q+DtNZ2kINb6qt/lS+IyXnHQe9e90POfeewL/ME=
github.com/sirupsen/logrus v1.3.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793 h1:u+LnwYTOOW7Ukr/fppxEb1Nwz0AtPflrblfvUudpo+I=
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33 h1:I6FyU15t786LL7oL/hn43zqTuEGr4PN7F4XJ1p4E3Y8=
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/natefinch/lumberjack.v2 v2.0.0 h1:1Lc07Kr7qY4U2YPouBjpCLxpiyxIVoxqXgkXLknAOE8=
gopkg.in/natefinch/lumberjack.v2 v2.0.0/go.mod h1:l0ndWWf7gzL7RNwBG7wST/UCcT4T24xpD6X8LsfU/+k=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
222 changes: 222 additions & 0 deletions pulsar-client-go/logutil/log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,222 @@
//
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
//

package logutil

import (
"bytes"
"fmt"
"path"
"runtime"
"sort"
"strings"

log "github.com/sirupsen/logrus"
"gopkg.in/natefinch/lumberjack.v2"
)

type LoggerLevel int

const (
DEBUG LoggerLevel = iota
INFO
WARN
ERROR
)

func (l LoggerLevel) String() string {
switch l {
case DEBUG:
return "DEBUG"
case INFO:
return "INFO"
case WARN:
return "WARN"
case ERROR:
return "ERROR"

default:
return fmt.Sprintf("UNKNOWN: %d", l)
}
}

const (
defaultLogLevel = log.InfoLevel
defaultLogTimeFormat = "2006/01/02 15:04:05.000"
)

func Info(v ...interface{}) {
log.Info(v...)
}

func Infof(format string, v ...interface{}) {
log.Infof(format, v...)
}

func Debug(v ...interface{}) {
log.Debug(v...)
}

func Debugf(format string, v ...interface{}) {
log.Debugf(format, v...)
}

func Warn(v ...interface{}) {
log.Warn(v...)
}

func Warnf(format string, v ...interface{}) {
log.Warnf(format, v...)
}

func Error(v ...interface{}) {
log.Error(v...)
}

func Errorf(format string, v ...interface{}) {
log.Errorf(format, v...)
}

func Fatal(v ...interface{}) {
log.Fatal(v...)
}

func Fatalf(format string, v ...interface{}) {
log.Fatalf(format, v...)
}

func stringToLogLevel(level string) log.Level {
switch strings.ToLower(level) {
case "fatal":
return log.FatalLevel
case "error":
return log.ErrorLevel
case "warn", "warning":
return log.WarnLevel
case "debug":
return log.DebugLevel
case "info":
return log.InfoLevel
}
return defaultLogLevel
}

// hook injects file name and line pos into log entry.
type hook struct{}

// Fire implements logrus.Hook interface
func (h *hook) Fire(entry *log.Entry) error {
// these two num are set by manually testing
pc := make([]uintptr, 4)
cnt := runtime.Callers(10, pc)

for i := 0; i < cnt; i++ {
fu := runtime.FuncForPC(pc[i] - 1)
name := fu.Name()
if !isSkippedPackageName(name) {
file, line := fu.FileLine(pc[i] - 1)
entry.Data["file"] = path.Base(file)
entry.Data["line"] = line
break
}
}
return nil
}

// Levels implements logrus.Hook interface.
func (h *hook) Levels() []log.Level {
return log.AllLevels
}

// isSKippedPackageName tests wether path name is on log library calling stack.
func isSkippedPackageName(name string) bool {
return strings.Contains(name, "github.com/sirupsen/logrus") ||
strings.Contains(name, "github.com/apache/pulsar/pulsar-client-go/logutil")
}

// formatter is for compatibility with ngaut/log
type formatter struct {
DisableTimestamp, EnableEntryOrder bool
}

// Format implements logrus.Formatter
func (f *formatter) Format(entry *log.Entry) ([]byte, error) {
var b *bytes.Buffer
if entry.Buffer != nil {
b = entry.Buffer
} else {
b = &bytes.Buffer{}
}

if !f.DisableTimestamp {
fmt.Fprintf(b, "%s ", entry.Time.Format(defaultLogTimeFormat))
}
if file, ok := entry.Data["file"]; ok {
fmt.Fprintf(b, "%s:%v:", file, entry.Data["line"])
}
fmt.Fprintf(b, " [%s] %s", entry.Level.String(), entry.Message)

if f.EnableEntryOrder {
keys := make([]string, 0, len(entry.Data))
for k := range entry.Data {
if k != "file" && k != "line" {
keys = append(keys, k)
}
}
sort.Strings(keys)
for _, k := range keys {
fmt.Fprintf(b, " %v=%v", k, entry.Data[k])
}
} else {
for k, v := range entry.Data {
if k != "file" && k != "line" {
fmt.Fprintf(b, " %v=%v", k, v)
}
}
}

b.WriteByte('\n')

return b.Bytes(), nil
}

// SetLevel sets log's level by a level string.
func SetLevel(level string) {
log.SetLevel(stringToLogLevel(level))
}

// GetLevel gets current log's level as a level string.
func GetLevel() string {
return log.GetLevel().String()
}

// SetOutput sets the filename for the log.
func SetOutput(filename string) {
output := &lumberjack.Logger{
Filename: filename,
LocalTime: true,
}
log.SetOutput(output)
}

func init() {
log.SetLevel(defaultLogLevel)
log.SetFormatter(&formatter{})
log.AddHook(&hook{})
}
13 changes: 7 additions & 6 deletions pulsar-client-go/pulsar/c_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,18 @@ package pulsar
*/
import "C"
import (
"log"
"runtime"
"strings"
"unsafe"

log "github.com/apache/pulsar/pulsar-client-go/logutil"
)

//export pulsarClientLoggerProxy
func pulsarClientLoggerProxy(level C.pulsar_logger_level_t, file *C.char, line C.int, message *C.char, ctx unsafe.Pointer) {
logger := restorePointerNoDelete(ctx).(func(LoggerLevel, string, int, string))
logger := restorePointerNoDelete(ctx).(func(log.LoggerLevel, string, int, string))

logger(LoggerLevel(level), C.GoString(file), int(line), C.GoString(message))
logger(log.LoggerLevel(level), C.GoString(file), int(line), C.GoString(message))
}

func newClient(options ClientOptions) (Client, error) {
Expand Down Expand Up @@ -63,8 +64,8 @@ func newClient(options ClientOptions) (Client, error) {

if options.Logger == nil {
// Configure a default logger with same date format as Go logs
options.Logger = func(level LoggerLevel, file string, line int, message string) {
log.Printf("%-5s | %s:%d | %s", level, file, line, message)
options.Logger = func(level log.LoggerLevel, file string, line int, message string) {
log.Infof("%-5s | %s:%d | %s", level, file, line, message)
}
}

Expand Down Expand Up @@ -144,7 +145,7 @@ func pulsarClientTokenSupplierProxy(ctx unsafe.Pointer) *C.char {
tokenSupplier := restorePointerNoDelete(ctx).(func() string)
token := tokenSupplier()
// The C string will be freed from within the C wrapper itself
return C.CString(token);
return C.CString(token)
}

func newAuthenticationTokenSupplier(tokenSupplier func() string) Authentication {
Expand Down
8 changes: 6 additions & 2 deletions pulsar-client-go/pulsar/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@

package pulsar

import "time"
import (
"time"

log "github.com/apache/pulsar/pulsar-client-go/logutil"
)

func NewClient(options ClientOptions) (Client, error) {
return newClient(options)
Expand Down Expand Up @@ -74,7 +78,7 @@ type ClientOptions struct {
// By default, log messages will be printed on standard output. By passing a logger function, application
// can determine how to print logs. This function will be called each time the Pulsar client library wants
// to write any logs.
Logger func(level LoggerLevel, file string, line int, message string)
Logger func(level log.LoggerLevel, file string, line int, message string)

// Set the path to the trusted TLS certificate file
TLSTrustCertsFilePath string
Expand Down
Loading

0 comments on commit 8b74640

Please sign in to comment.