mirror of https://github.com/grafana/grafana
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 * retriggerpull/63882/head
parent
ab8de1a0e3
commit
ec82719372
@ -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{} |
||||
} |
||||
@ -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{}) |
||||
} |
||||
@ -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...)) |
||||
} |
||||
@ -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...) |
||||
} |
||||
@ -1,17 +0,0 @@ |
||||
package logger |
||||
|
||||
// Logger is used primarily to facilitate logging/user feedback for both
|
||||
// the grafana-cli and the grafana backend when managing plugin installs
|
||||
type Logger 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{}) |
||||
} |
||||
@ -1,57 +0,0 @@ |
||||
package logger |
||||
|
||||
import ( |
||||
"fmt" |
||||
|
||||
"github.com/grafana/grafana/pkg/infra/log" |
||||
) |
||||
|
||||
type InfraLogWrapper struct { |
||||
log log.Logger |
||||
} |
||||
|
||||
func NewLogger(name string) *InfraLogWrapper { |
||||
return &InfraLogWrapper{ |
||||
log: log.New(name), |
||||
} |
||||
} |
||||
|
||||
func (l *InfraLogWrapper) Successf(format string, args ...interface{}) { |
||||
l.log.Info(fmt.Sprintf(format, args...)) |
||||
} |
||||
|
||||
func (l *InfraLogWrapper) Failuref(format string, args ...interface{}) { |
||||
l.log.Error(fmt.Sprintf(format, args...)) |
||||
} |
||||
|
||||
func (l *InfraLogWrapper) Info(args ...interface{}) { |
||||
l.log.Info(fmt.Sprint(args...)) |
||||
} |
||||
|
||||
func (l *InfraLogWrapper) Infof(format string, args ...interface{}) { |
||||
l.log.Info(fmt.Sprintf(format, args...)) |
||||
} |
||||
|
||||
func (l *InfraLogWrapper) Debug(args ...interface{}) { |
||||
l.log.Debug(fmt.Sprint(args...)) |
||||
} |
||||
|
||||
func (l *InfraLogWrapper) Debugf(format string, args ...interface{}) { |
||||
l.log.Debug(fmt.Sprintf(format, args...)) |
||||
} |
||||
|
||||
func (l *InfraLogWrapper) Warn(args ...interface{}) { |
||||
l.log.Warn(fmt.Sprint(args...)) |
||||
} |
||||
|
||||
func (l *InfraLogWrapper) Warnf(format string, args ...interface{}) { |
||||
l.log.Warn(fmt.Sprintf(format, args...)) |
||||
} |
||||
|
||||
func (l *InfraLogWrapper) Error(args ...interface{}) { |
||||
l.log.Error(fmt.Sprint(args...)) |
||||
} |
||||
|
||||
func (l *InfraLogWrapper) Errorf(format string, args ...interface{}) { |
||||
l.log.Error(fmt.Sprintf(format, args...)) |
||||
} |
||||
Loading…
Reference in new issue