Skip to content

Commit

Permalink
Move the structure logging code to logger.go file
Browse files Browse the repository at this point in the history
  • Loading branch information
EmadMokhtar authored and timvaillancourt committed Jul 9, 2024
1 parent 03a7cfe commit fbb19d3
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 197 deletions.
47 changes: 47 additions & 0 deletions go/vt/logutil/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ import (
"sync"
"time"

noglog "github.com/slok/noglog"
"go.uber.org/zap"

"vitess.io/vitess/go/protoutil"
"vitess.io/vitess/go/vt/log"
logutilpb "vitess.io/vitess/go/vt/proto/logutil"
)

Expand Down Expand Up @@ -383,3 +387,46 @@ func fileAndLine(depth int) (string, int64) {
}
return file, int64(line)
}

type VTSLogger zap.SugaredLogger

// SetVTStructureLogger in-place noglog replacement with Zap's logger.
func SetVTStructureLogger(conf *zap.Config) (vtSLogger *zap.SugaredLogger, err error) {
var l *zap.Logger

// Use the passed configuration instead of the default configuration
if conf == nil {
defaultProdConf := zap.NewProductionConfig()
conf = &defaultProdConf
}

// Build configuration and generate a sugared logger
l, err = conf.Build()
vtSLogger = l.Sugar()

noglog.SetLogger(&noglog.LoggerFunc{
DebugfFunc: func(f string, a ...interface{}) { vtSLogger.Debugf(f, a...) },
InfofFunc: func(f string, a ...interface{}) { vtSLogger.Infof(f, a...) },
WarnfFunc: func(f string, a ...interface{}) { vtSLogger.Warnf(f, a...) },
ErrorfFunc: func(f string, a ...interface{}) { vtSLogger.Errorf(f, a...) },
})

log.Flush = noglog.Flush
log.Info = noglog.Info
log.Infof = noglog.Infof
log.InfoDepth = noglog.InfoDepth
log.Warning = noglog.Warning
log.Warningf = noglog.Warningf
log.WarningDepth = noglog.WarningDepth
log.Error = noglog.Error
log.Errorf = noglog.Errorf
log.ErrorDepth = noglog.ErrorDepth
log.Exit = noglog.Exit
log.Exitf = noglog.Exitf
log.ExitDepth = noglog.ExitDepth
log.Fatal = noglog.Fatal
log.Fatalf = noglog.Fatalf
log.FatalDepth = noglog.FatalDepth

return
}
106 changes: 106 additions & 0 deletions go/vt/logutil/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,15 @@ limitations under the License.
package logutil

import (
"bytes"
"encoding/json"
"github.com/stretchr/testify/assert"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"net/url"
"testing"
"time"
vtlog "vitess.io/vitess/go/vt/log"

"vitess.io/vitess/go/protoutil"
"vitess.io/vitess/go/race"
Expand Down Expand Up @@ -152,3 +159,102 @@ func TestTeeLogger(t *testing.T) {
}
}
}

// MemorySink implements zap.Sink by writing all messages to a buffer.
// It's used to capture the logs.
type MemorySink struct {
*bytes.Buffer
}

// Implement Close and Sync as no-ops to satisfy the interface. The Write
// method is provided by the embedded buffer.
func (s *MemorySink) Close() error { return nil }
func (s *MemorySink) Sync() error { return nil }

func SetupLoggerWithMemSink() (sink *MemorySink, err error) {
// Create a sink instance, and register it with zap for the "memory" protocol.
sink = &MemorySink{new(bytes.Buffer)}
err = zap.RegisterSink("memory", func(*url.URL) (zap.Sink, error) {
return sink, nil
})
if err != nil {
return nil, err
}

testLoggerConf := NewMemorySinkConfig()
_, err = SetVTStructureLogger(&testLoggerConf)
if err != nil {
return nil, err
}

return
}

func NewMemorySinkConfig() zap.Config {
return zap.Config{
Level: zap.NewAtomicLevelAt(zap.InfoLevel),
Development: false,
Sampling: &zap.SamplingConfig{
Initial: 100,
Thereafter: 100,
},
Encoding: "json",
EncoderConfig: zap.NewProductionEncoderConfig(),
OutputPaths: []string{"memory://"},
ErrorOutputPaths: []string{"memory://"},
}
}

func TestVTSLogger_Replacing_glog(t *testing.T) {
type logMsg struct {
Level string `json:"level"`
Msg string `json:"msg"`
}

type testCase struct {
name string
logLevel zapcore.Level
}

dummyLogMessage := "testing log"
testCases := []testCase{
{"log info", zap.InfoLevel},
{"log warn", zap.WarnLevel},
{"log error", zap.ErrorLevel},
}

sink, err := SetupLoggerWithMemSink()
assert.NoError(t, err)

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
var loggingFunc func(format string, args ...interface{})
var expectedLevel string

switch tc.logLevel {
case zapcore.InfoLevel:
loggingFunc = vtlog.Infof
expectedLevel = "info"
case zapcore.ErrorLevel:
loggingFunc = vtlog.Errorf
expectedLevel = "error"
case zapcore.WarnLevel:
loggingFunc = vtlog.Warningf
expectedLevel = "warn"
}

loggingFunc(dummyLogMessage)

// Unmarshal the captured log. This means we're getting a struct log.
actualLog := logMsg{}
err = json.Unmarshal(sink.Bytes(), &actualLog)
assert.NoError(t, err)
// Reset the sink so that it'll contain one log per test case.
sink.Reset()

assert.Equal(t, expectedLevel, actualLog.Level)
assert.Equal(t, dummyLogMessage, actualLog.Msg)

})
}
}
67 changes: 0 additions & 67 deletions go/vt/logutil/vts_logger.go

This file was deleted.

130 changes: 0 additions & 130 deletions go/vt/logutil/vts_logger_test.go

This file was deleted.

0 comments on commit fbb19d3

Please sign in to comment.