diff --git a/cronopts/log.go b/cronopts/log.go new file mode 100644 index 00000000..ce680055 --- /dev/null +++ b/cronopts/log.go @@ -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...)...) +} diff --git a/cronopts/log_test.go b/cronopts/log_test.go new file mode 100644 index 00000000..aa47e5a7 --- /dev/null +++ b/cronopts/log_test.go @@ -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()) +} \ No newline at end of file diff --git a/otes/provider.go b/otes/provider.go index cee04f1e..4f996db6 100644 --- a/otes/provider.go +++ b/otes/provider.go @@ -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. @@ -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 { diff --git a/otes/provider_test.go b/otes/provider_test.go index e1899bf0..fca29044 100644 --- a/otes/provider_test.go +++ b/otes/provider_test.go @@ -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" @@ -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) diff --git a/otgorm/doc.go b/otgorm/doc.go index 50067ed8..3f91ed4e 100644 --- a/otgorm/doc.go +++ b/otgorm/doc.go @@ -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) { diff --git a/serve.go b/serve.go index cd9f9343..329263cc 100644 --- a/serve.go +++ b/serve.go @@ -3,6 +3,7 @@ package core import ( "context" "github.com/DoNewsCode/core/events" + "github.com/DoNewsCode/core/cronopts" "net" "net/http" "os" @@ -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")