mirror of https://github.com/knative/pkg.git
Add common test logging module (#45)
* Add common test logging module * Update references to logging lib * Add error functions
This commit is contained in:
parent
a3bc2db77a
commit
450739d6f8
|
@ -24,6 +24,8 @@ import (
|
|||
"os"
|
||||
"os/user"
|
||||
"path"
|
||||
|
||||
"github.com/knative/pkg/test/logging"
|
||||
)
|
||||
|
||||
// Flags holds the command line flags or defaults for settings in the user's environment.
|
||||
|
@ -67,10 +69,10 @@ func initializeFlags() *EnvironmentFlags {
|
|||
|
||||
flag.Parse()
|
||||
flag.Set("alsologtostderr", "true")
|
||||
initializeLogger(f.LogVerbose)
|
||||
logging.InitializeLogger(f.LogVerbose)
|
||||
|
||||
if f.EmitMetrics {
|
||||
initializeMetricExporter()
|
||||
logging.InitializeMetricExporter()
|
||||
}
|
||||
return &f
|
||||
}
|
||||
|
|
|
@ -17,7 +17,7 @@ limitations under the License.
|
|||
// logging.go contains the logic to configure and interact with the
|
||||
// logging and metrics libraries.
|
||||
|
||||
package test
|
||||
package logging
|
||||
|
||||
import (
|
||||
"flag"
|
||||
|
@ -39,7 +39,7 @@ const (
|
|||
metricViewReportingPeriod = 1 * time.Second
|
||||
)
|
||||
|
||||
var baseLogger *zap.SugaredLogger
|
||||
var baseLogger *BaseLogger
|
||||
|
||||
var exporter *zapMetricExporter
|
||||
|
||||
|
@ -50,6 +50,11 @@ type zapMetricExporter struct {
|
|||
logger *zap.SugaredLogger
|
||||
}
|
||||
|
||||
// BaseLogger is a common knative test files logger.
|
||||
type BaseLogger struct {
|
||||
Logger *zap.SugaredLogger
|
||||
}
|
||||
|
||||
// ExportView will emit the view data vd (i.e. the stats that have been
|
||||
// recorded) to the zap logger.
|
||||
func (e *zapMetricExporter) ExportView(vd *view.Data) {
|
||||
|
@ -65,7 +70,7 @@ func (e *zapMetricExporter) ExportSpan(vd *trace.SpanData) {
|
|||
e.logger.Infof("metric %s %d %d %s", vd.Name, vd.StartTime.UnixNano(), vd.EndTime.UnixNano(), duration)
|
||||
}
|
||||
|
||||
func newLogger(logLevel string) *zap.SugaredLogger {
|
||||
func newLogger(logLevel string) *BaseLogger {
|
||||
configJSONTemplate := `{
|
||||
"level": "%s",
|
||||
"encoding": "console",
|
||||
|
@ -87,15 +92,17 @@ func newLogger(logLevel string) *zap.SugaredLogger {
|
|||
}`
|
||||
configJSON := fmt.Sprintf(configJSONTemplate, logLevel)
|
||||
l, _ := logging.NewLogger(string(configJSON), logLevel)
|
||||
return l
|
||||
return &BaseLogger{Logger: l}
|
||||
}
|
||||
|
||||
func initializeMetricExporter() {
|
||||
// InitializeMetricExporter initializes the metric exporter logger
|
||||
func InitializeMetricExporter() {
|
||||
trace.ApplyConfig(trace.Config{DefaultSampler: trace.AlwaysSample()})
|
||||
view.SetReportingPeriod(metricViewReportingPeriod)
|
||||
}
|
||||
|
||||
func initializeLogger(logVerbose bool) {
|
||||
// InitializeLogger initializes the base logger
|
||||
func InitializeLogger(logVerbose bool) {
|
||||
logLevel := "info"
|
||||
if logVerbose {
|
||||
// Both gLog and "go test" use -v flag. The code below is a work around so that we can still set v value for gLog
|
||||
|
@ -112,7 +119,7 @@ func initializeLogger(logVerbose bool) {
|
|||
// using the provided context as a name. This will also register the logger as a metric exporter,
|
||||
// which is unfortunately global, so calling `GetContextLogger` will have the side effect of
|
||||
// changing the context in which all metrics are logged from that point forward.
|
||||
func GetContextLogger(context string) *zap.SugaredLogger {
|
||||
func GetContextLogger(context string) *BaseLogger {
|
||||
// If there was a previously registered exporter, unregister it so we only emit
|
||||
// the metrics in the current context.
|
||||
if exporter != nil {
|
||||
|
@ -120,11 +127,51 @@ func GetContextLogger(context string) *zap.SugaredLogger {
|
|||
trace.UnregisterExporter(exporter)
|
||||
}
|
||||
|
||||
logger := baseLogger.Named(context)
|
||||
logger := baseLogger.Logger.Named(context)
|
||||
|
||||
exporter = &zapMetricExporter{logger: logger}
|
||||
view.RegisterExporter(exporter)
|
||||
trace.RegisterExporter(exporter)
|
||||
|
||||
return logger
|
||||
return &BaseLogger{Logger: logger}
|
||||
}
|
||||
|
||||
// Infof logs a templated message.
|
||||
func (b *BaseLogger) Infof(template string, args ...interface{}) {
|
||||
b.Logger.Infof(template, args...)
|
||||
}
|
||||
|
||||
// Info logs an info message.
|
||||
func (b *BaseLogger) Info(args ...interface{}) {
|
||||
b.Logger.Info(args...)
|
||||
}
|
||||
|
||||
// Fatal logs a fatal message.
|
||||
func (b *BaseLogger) Fatal(args ...interface{}) {
|
||||
b.Logger.Fatal(args...)
|
||||
}
|
||||
|
||||
// Fatalf logs a templated fatal message.
|
||||
func (b *BaseLogger) Fatalf(template string, args ...interface{}) {
|
||||
b.Logger.Fatalf(template, args...)
|
||||
}
|
||||
|
||||
// Debugf logs a templated debug message.
|
||||
func (b *BaseLogger) Debugf(template string, args ...interface{}) {
|
||||
b.Logger.Debugf(template, args...)
|
||||
}
|
||||
|
||||
// Debug logs a debug message.
|
||||
func (b *BaseLogger) Debug(args ...interface{}) {
|
||||
b.Logger.Debug(args...)
|
||||
}
|
||||
|
||||
// Errorf logs a templated error message.
|
||||
func (b *BaseLogger) Errorf(template string, args ...interface{}) {
|
||||
b.Logger.Errorf(template, args...)
|
||||
}
|
||||
|
||||
// Error logs an error message.
|
||||
func (b *BaseLogger) Error(args ...interface{}) {
|
||||
b.Logger.Error(args...)
|
||||
}
|
Loading…
Reference in New Issue