package logger
import (
"bytes"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
ctrl "sigs.k8s.io/controller-runtime"
)
var _ = Describe("Logger", func() {
It("Test Default Log Level", func() {
buf := &bytes.Buffer{}
testLog := ctrl.Log.WithName("setup")
SetLogger()
testLog.Info("This is a test")
Expect(buf.String(),"This is a test")
})
})
这就是SetLogger
生产中也使用的功能:
package logger
import (
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
...
)
func SetLogger() {
opts := zap.Options{
Development: developmentFlag,
StacktraceLevel: stacktraceLevel,
Level: isLevelEnabler,
Encoder: logFmtEncoder,
}
ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts)))
}
如何更改testLog.Info
缓冲区的输出?