use micro logger instead of hclog.Logger

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2020-12-09 13:15:36 +03:00
parent e4dca0ce20
commit 883757b7f8
4 changed files with 141 additions and 4 deletions

View File

@ -5,7 +5,7 @@ import (
"encoding/json"
"errors"
api "github.com/hashicorp/consul/api"
"github.com/hashicorp/consul/api"
"github.com/unistack-org/micro/v3/config"
)
@ -30,12 +30,22 @@ func (c *consulConfig) Init(opts ...config.Option) error {
o(&c.opts)
}
cfg := api.DefaultConfig()
cfg := api.DefaultConfigWithLogger(&consulLogger{logger: c.opts.Logger})
path := ""
if c.opts.Context != nil {
if v, ok := c.opts.Context.Value(configKey{}).(*api.Config); ok {
cfg = v
cfg.Address = v.Address
cfg.Scheme = v.Scheme
cfg.Datacenter = v.Datacenter
cfg.Transport = v.Transport
cfg.HttpClient = v.HttpClient
cfg.HttpAuth = v.HttpAuth
cfg.WaitTime = v.WaitTime
cfg.Token = v.Token
cfg.TokenFile = v.TokenFile
cfg.Namespace = v.Namespace
cfg.TLSConfig = v.TLSConfig
}
if v, ok := c.opts.Context.Value(addrKey{}).(string); ok {
@ -49,7 +59,6 @@ func (c *consulConfig) Init(opts ...config.Option) error {
if v, ok := c.opts.Context.Value(pathKey{}).(string); ok {
path = v
}
}
cli, err := api.NewClient(cfg)

1
go.mod
View File

@ -4,6 +4,7 @@ go 1.15
require (
github.com/hashicorp/consul/api v1.8.0
github.com/hashicorp/go-hclog v0.15.0
github.com/pkg/errors v0.9.1 // indirect
github.com/unistack-org/micro/v3 v3.0.2-0.20201207213837-b7b28f6b9add
)

2
go.sum
View File

@ -149,6 +149,8 @@ github.com/hashicorp/go-cleanhttp v0.5.1 h1:dH3aiDG9Jvb5r5+bYHsikaOUIpcM0xvgMXVo
github.com/hashicorp/go-cleanhttp v0.5.1/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtngrth3wmdIIUrZ80=
github.com/hashicorp/go-hclog v0.12.0 h1:d4QkX8FRTYaKaCZBoXYY8zJX2BXjWxurN/GA2tkrmZM=
github.com/hashicorp/go-hclog v0.12.0/go.mod h1:whpDNt7SSdeAju8AWKIWsul05p54N/39EeqMAyrmvFQ=
github.com/hashicorp/go-hclog v0.15.0 h1:qMuK0wxsoW4D0ddCCYwPSTm4KQv1X1ke3WmPWZ0Mvsk=
github.com/hashicorp/go-hclog v0.15.0/go.mod h1:whpDNt7SSdeAju8AWKIWsul05p54N/39EeqMAyrmvFQ=
github.com/hashicorp/go-immutable-radix v1.0.0 h1:AKDB1HM5PWEA7i4nhcpwOrO2byshxBjXVn/J/3+z5/0=
github.com/hashicorp/go-immutable-radix v1.0.0/go.mod h1:0y9vanUI8NX6FsYoO3zeMjhV/C5i9g4Q3DwcSNZ4P60=
github.com/hashicorp/go-msgpack v0.5.3 h1:zKjpN5BK/P5lMYrLmBHdBULWbJ0XpYR+7NGzqkZzoD4=

125
logger.go Normal file
View File

@ -0,0 +1,125 @@
package consul
import (
"bytes"
"fmt"
"io"
"log"
"os"
hclog "github.com/hashicorp/go-hclog"
"github.com/unistack-org/micro/v3/logger"
)
var (
// to check implementation
_ hclog.Logger = &consulLogger{}
)
type consulLogger struct {
logger logger.Logger
}
func (l *consulLogger) Name() string {
return l.logger.String()
}
func (l *consulLogger) With(args ...interface{}) hclog.Logger {
fields := make(map[string]interface{}, int(len(args)/2))
for i := 0; i < int(len(args)/2); i = i + 2 {
fields[fmt.Sprintf("%v", args[i])] = args[i+1]
}
return &consulLogger{logger: l.logger.Fields(fields)}
}
func (l *consulLogger) Debug(format string, msg ...interface{}) {
l.logger.Debugf(format, msg...)
}
func (l *consulLogger) Error(format string, msg ...interface{}) {
l.logger.Errorf(format, msg...)
}
func (l *consulLogger) Info(format string, msg ...interface{}) {
l.logger.Infof(format, msg...)
}
func (l *consulLogger) Warn(format string, msg ...interface{}) {
l.logger.Warnf(format, msg...)
}
func (l *consulLogger) Trace(format string, msg ...interface{}) {
l.logger.Tracef(format, msg...)
}
func (l *consulLogger) ImpliedArgs() []interface{} {
fields := make([]interface{}, len(l.logger.Options().Fields)*2)
for k, v := range l.logger.Options().Fields {
fields = append(fields, k, v)
}
return fields
}
func (l *consulLogger) Named(name string) hclog.Logger {
var newname string
if oldname, ok := l.logger.Options().Fields["name"]; ok {
newname = fmt.Sprintf("%s.%s", oldname, name)
} else {
newname = fmt.Sprintf("%s", name)
}
return &consulLogger{logger: l.logger.Fields(map[string]interface{}{"name": newname})}
}
func (l *consulLogger) ResetNamed(name string) hclog.Logger {
return &consulLogger{logger: l.logger.Fields(map[string]interface{}{"name": name})}
}
func (l *consulLogger) SetLevel(level hclog.Level) {
// TODO: add logic when logger.Logger supports this method
}
func (l *consulLogger) StandardLogger(opts *hclog.StandardLoggerOptions) *log.Logger {
// TODO: add logic
return log.New(os.Stderr, "", log.Ldate|log.Ltime|log.Llongfile|log.LUTC)
}
func (l *consulLogger) StandardWriter(opts *hclog.StandardLoggerOptions) io.Writer {
buf := bytes.NewBuffer(nil)
return buf
}
func (l *consulLogger) IsDebug() bool {
return l.logger.V(logger.DebugLevel)
}
func (l *consulLogger) IsError() bool {
return l.logger.V(logger.ErrorLevel)
}
func (l *consulLogger) IsInfo() bool {
return l.logger.V(logger.InfoLevel)
}
func (l *consulLogger) IsTrace() bool {
return l.logger.V(logger.TraceLevel)
}
func (l *consulLogger) IsWarn() bool {
return l.logger.V(logger.WarnLevel)
}
func (l *consulLogger) Log(level hclog.Level, msg string, args ...interface{}) {
switch level {
case hclog.Trace:
l.Trace(msg, args...)
case hclog.Debug:
l.Debug(msg, args...)
case hclog.Info:
l.Info(msg, args...)
case hclog.Warn:
l.Warn(msg, args...)
case hclog.Error:
l.Error(msg, args...)
case hclog.NoLevel:
l.Info(msg, args...)
}
}