Plugins: Introduce plugin package specific logger (#62204)

* refactor

* implement with infra log for now

* undo moving

* update package name

* update name

* fix tests

* update pretty signature

* update naming

* simplify

* fix typo

* delete comment

* fix import

* retrigger
This commit is contained in:
Will Browne
2023-02-28 16:10:27 +01:00
committed by GitHub
parent ab8de1a0e3
commit ec82719372
38 changed files with 259 additions and 140 deletions
+48
View File
@@ -0,0 +1,48 @@
package log
var _ Logger = (*TestLogger)(nil)
type TestLogger struct {
DebugLogs Logs
InfoLogs Logs
WarnLogs Logs
ErrorLogs Logs
}
func NewTestLogger() *TestLogger {
return &TestLogger{}
}
func (f *TestLogger) New(_ ...interface{}) Logger {
return NewTestLogger()
}
func (f *TestLogger) Info(msg string, ctx ...interface{}) {
f.InfoLogs.Calls++
f.InfoLogs.Message = msg
f.InfoLogs.Ctx = ctx
}
func (f *TestLogger) Warn(msg string, ctx ...interface{}) {
f.WarnLogs.Calls++
f.WarnLogs.Message = msg
f.WarnLogs.Ctx = ctx
}
func (f *TestLogger) Debug(msg string, ctx ...interface{}) {
f.DebugLogs.Calls++
f.DebugLogs.Message = msg
f.DebugLogs.Ctx = ctx
}
func (f *TestLogger) Error(msg string, ctx ...interface{}) {
f.ErrorLogs.Calls++
f.ErrorLogs.Message = msg
f.ErrorLogs.Ctx = ctx
}
type Logs struct {
Calls int
Message string
Ctx []interface{}
}
+35
View File
@@ -0,0 +1,35 @@
package log
// Logger is the default logger
type Logger interface {
// New returns a new contextual Logger that has this logger's context plus the given context.
New(ctx ...interface{}) Logger
// Debug logs a message with debug level and key/value pairs, if any.
Debug(msg string, ctx ...interface{})
// Info logs a message with info level and key/value pairs, if any.
Info(msg string, ctx ...interface{})
// Warn logs a message with warning level and key/value pairs, if any.
Warn(msg string, ctx ...interface{})
// Error logs a message with error level and key/value pairs, if any.
Error(msg string, ctx ...interface{})
}
// PrettyLogger is used primarily to facilitate logging/user feedback for both
// the grafana-cli and the grafana backend when managing plugin installs
type PrettyLogger interface {
Successf(format string, args ...interface{})
Failuref(format string, args ...interface{})
Info(args ...interface{})
Infof(format string, args ...interface{})
Debug(args ...interface{})
Debugf(format string, args ...interface{})
Warn(args ...interface{})
Warnf(format string, args ...interface{})
Error(args ...interface{})
Errorf(format string, args ...interface{})
}
+57
View File
@@ -0,0 +1,57 @@
package log
import (
"fmt"
)
var _ PrettyLogger = (*prettyLogger)(nil)
type prettyLogger struct {
log Logger
}
func NewPrettyLogger(name string) *prettyLogger {
return &prettyLogger{
log: New(name),
}
}
func (l *prettyLogger) Successf(format string, args ...interface{}) {
l.log.Info(fmt.Sprintf(format, args...))
}
func (l *prettyLogger) Failuref(format string, args ...interface{}) {
l.log.Error(fmt.Sprintf(format, args...))
}
func (l *prettyLogger) Info(args ...interface{}) {
l.log.Info(fmt.Sprint(args...))
}
func (l *prettyLogger) Infof(format string, args ...interface{}) {
l.log.Info(fmt.Sprintf(format, args...))
}
func (l *prettyLogger) Debug(args ...interface{}) {
l.log.Debug(fmt.Sprint(args...))
}
func (l *prettyLogger) Debugf(format string, args ...interface{}) {
l.log.Debug(fmt.Sprintf(format, args...))
}
func (l *prettyLogger) Warn(args ...interface{}) {
l.log.Warn(fmt.Sprint(args...))
}
func (l *prettyLogger) Warnf(format string, args ...interface{}) {
l.log.Warn(fmt.Sprintf(format, args...))
}
func (l *prettyLogger) Error(args ...interface{}) {
l.log.Error(fmt.Sprint(args...))
}
func (l *prettyLogger) Errorf(format string, args ...interface{}) {
l.log.Error(fmt.Sprintf(format, args...))
}
+44
View File
@@ -0,0 +1,44 @@
package log
import (
"github.com/grafana/grafana/pkg/infra/log"
)
func New(name string) Logger {
return &grafanaInfraLogWrapper{
l: log.New(name),
}
}
type grafanaInfraLogWrapper struct {
l *log.ConcreteLogger
}
func (d *grafanaInfraLogWrapper) New(ctx ...interface{}) Logger {
if len(ctx) == 0 {
return &grafanaInfraLogWrapper{
l: d.l.New(),
}
}
ctx = append([]interface{}{"logger"}, ctx...)
return &grafanaInfraLogWrapper{
l: d.l.New(ctx...),
}
}
func (d *grafanaInfraLogWrapper) Debug(msg string, ctx ...interface{}) {
d.l.Debug(msg, ctx...)
}
func (d *grafanaInfraLogWrapper) Info(msg string, ctx ...interface{}) {
d.l.Info(msg, ctx...)
}
func (d *grafanaInfraLogWrapper) Warn(msg string, ctx ...interface{}) {
d.l.Warn(msg, ctx...)
}
func (d *grafanaInfraLogWrapper) Error(msg string, ctx ...interface{}) {
d.l.Error(msg, ctx...)
}