Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add CronLogAdapter #88 #96

Merged
merged 13 commits into from
Mar 17, 2021
21 changes: 21 additions & 0 deletions cronopts/log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package cronopts

import (
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
)

// CronLogAdapter is an adapter between kitlog and cron logger interface
type CronLogAdapter struct {
Logging log.Logger
}

// Info implements cron.Logger
func (c CronLogAdapter) Info(msg string, keysAndValues ...interface{}) {
_ = level.Info(c.Logging).Log(append([]interface{}{"msg", msg}, keysAndValues...)...)
}

// Error implements cron.Logger
func (c CronLogAdapter) Error(err error, msg string, keysAndValues ...interface{}) {
_ = level.Error(c.Logging).Log(append([]interface{}{"msg", msg, "err", err}, keysAndValues...)...)
}
23 changes: 23 additions & 0 deletions cronopts/log_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package cronopts

import (
"bytes"
"github.com/go-kit/kit/log"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
"testing"
)

func TestCronLogAdapter_Info(t *testing.T) {
var buf bytes.Buffer
l := CronLogAdapter{Logging: log.NewLogfmtLogger(&buf)}
l.Info("msg", "key","value")
assert.Equal(t, "level=info msg=msg key=value\n", buf.String())
}

func TestCronLogAdapter_Error(t *testing.T) {
var buf bytes.Buffer
l := CronLogAdapter{Logging: log.NewLogfmtLogger(&buf)}
l.Error(errors.New("err"),"msg", "key","value")
assert.Equal(t, "level=error msg=msg err=err key=value\n",buf.String())
}
6 changes: 4 additions & 2 deletions otes/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,9 @@ type in struct {

Logger log.Logger
Conf contract.ConfigAccessor
Interceptor EsConfigInterceptor `optional:"true"`
Tracer opentracing.Tracer `optional:"true"`
Interceptor EsConfigInterceptor `optional:"true"`
Tracer opentracing.Tracer `optional:"true"`
Options []elastic.ClientOptionFunc `optional:"true"`
}

// out is the result of Provide.
Expand Down Expand Up @@ -125,6 +126,7 @@ func provideEsFactory(p in) (out, func()) {
elastic.SetErrorLog(esLogAdapter{level.Error(logger)}),
elastic.SetTraceLog(esLogAdapter{level.Debug(logger)}),
)
options = append(options, p.Options...)

client, err := elastic.NewClient(options...)
if err != nil {
Expand Down
23 changes: 23 additions & 0 deletions otes/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package otes
import (
"github.com/DoNewsCode/core/config"
"github.com/go-kit/kit/log"
"github.com/olivere/elastic/v7"
esConfig "github.com/olivere/elastic/v7/config"
"github.com/stretchr/testify/assert"
"testing"
Expand All @@ -29,6 +30,28 @@ func TestNewEsFactory(t *testing.T) {
cleanup()
}

func TestNewEsFactoryWithOptions(t *testing.T) {
var called bool
esFactory, cleanup := provideEsFactory(in{
Conf: config.MapAdapter{"es": map[string]esConfig.Config{
"default": {URL: "http://localhost:9200"},
}},
Logger: log.NewNopLogger(),
Options: []elastic.ClientOptionFunc{
func(client *elastic.Client) error {
called = true
return nil
},
},
Tracer: nil,
})
def, err := esFactory.Maker.Make("default")
assert.NoError(t, err)
assert.NotNil(t, def)
assert.True(t, called)
cleanup()
}

func TestProvideConfigs(t *testing.T) {
c := provideConfig()
assert.NotEmpty(t, c.Config)
Expand Down
2 changes: 1 addition & 1 deletion otgorm/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Then you can invoke gorm from the application.
// use client
})

Sometimes there are valid reasons to connect to more than one mongo server.
Sometimes there are valid reasons to connect to more than one mysql server.
Inject otgorm.Maker to factory a *gorm.DB with a specific configuration entry.

c.Invoke(function(maker otgorm.Maker) {
Expand Down
4 changes: 3 additions & 1 deletion serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package core
import (
"context"
"github.com/DoNewsCode/core/events"
"github.com/DoNewsCode/core/cronopts"
"net"
"net/http"
"os"
Expand Down Expand Up @@ -123,8 +124,9 @@ func newServeCmd(p serveIn) *cobra.Command {
// Start cron runner
if !p.Config.Bool("cron.disable") {
if p.Cron == nil {
p.Cron = cron.New()
p.Cron = cron.New(cron.WithLogger(cronopts.CronLogAdapter{Logging: l}))
}

p.Container.ApplyCron(p.Cron)
g.Add(func() error {
l.Info("cron runner started")
Expand Down