Plugins: Move logs from globals to fields (#42141)

* move logs from globals to fields

* fix test
This commit is contained in:
Will Browne
2021-11-23 17:09:52 +01:00
committed by GitHub
parent 0ab4afa2b7
commit 0921037f32
6 changed files with 77 additions and 39 deletions
@@ -23,17 +23,17 @@ import (
"github.com/grafana/grafana/pkg/util"
)
var logger = log.New("plugin.initializer")
type Initializer struct {
cfg *setting.Cfg
license models.Licensing
log log.Logger
}
func New(cfg *setting.Cfg, license models.Licensing) Initializer {
return Initializer{
cfg: cfg,
license: license,
log: log.New("plugin.initializer"),
}
}
@@ -80,7 +80,7 @@ func (i *Initializer) Initialize(p *plugins.Plugin) error {
}
}
pluginLog := logger.New("pluginID", p.ID)
pluginLog := i.log.New("pluginID", p.ID)
p.SetLogger(pluginLog)
if p.Backend {
@@ -123,7 +123,7 @@ func (i *Initializer) InitializeWithFactory(p *plugins.Plugin, factory backendpl
}
p.RegisterClient(f)
} else {
logger.Warn("Could not initialize core plugin process", "pluginID", p.ID)
i.log.Warn("Could not initialize core plugin process", "pluginID", p.ID)
return fmt.Errorf("could not initialize plugin %s", p.ID)
}
@@ -5,6 +5,7 @@ import (
"path/filepath"
"testing"
"github.com/inconshreveable/log15"
"github.com/stretchr/testify/assert"
"github.com/grafana/grafana/pkg/infra/log"
@@ -37,6 +38,7 @@ func TestInitializer_Initialize(t *testing.T) {
i := &Initializer{
cfg: setting.NewCfg(),
log: &fakeLogger{},
}
err := i.Initialize(p)
@@ -71,6 +73,7 @@ func TestInitializer_Initialize(t *testing.T) {
i := &Initializer{
cfg: setting.NewCfg(),
log: fakeLogger{},
}
err := i.Initialize(p)
@@ -117,6 +120,7 @@ func TestInitializer_Initialize(t *testing.T) {
cfg: &setting.Cfg{
AppSubURL: "appSubURL",
},
log: fakeLogger{},
}
err := i.Initialize(p)
@@ -163,6 +167,7 @@ func TestInitializer_InitializeWithFactory(t *testing.T) {
cfg: &setting.Cfg{
AppSubURL: "appSubURL",
},
log: fakeLogger{},
}
factoryInvoked := false
@@ -202,6 +207,7 @@ func TestInitializer_InitializeWithFactory(t *testing.T) {
cfg: &setting.Cfg{
AppSubURL: "appSubURL",
},
log: fakeLogger{},
}
err := i.InitializeWithFactory(p, nil)
@@ -236,6 +242,7 @@ func TestInitializer_envVars(t *testing.T) {
},
},
license: licensing,
log: fakeLogger{},
}
envVars := i.envVars(p)
@@ -252,6 +259,7 @@ func TestInitializer_setPathsBasedOnApp(t *testing.T) {
t.Run("When setting paths based on core plugin on Windows", func(t *testing.T) {
i := &Initializer{
cfg: setting.NewCfg(),
log: fakeLogger{},
}
child := &plugins.Plugin{
@@ -347,3 +355,15 @@ func (t *testLicensingService) Environment() map[string]string {
type testPlugin struct {
backendplugin.Plugin
}
type fakeLogger struct {
log.Logger
}
func (f fakeLogger) New(_ ...interface{}) log15.Logger {
return fakeLogger{}
}
func (f fakeLogger) Warn(_ string, _ ...interface{}) {
}