Skip to content

Commit

Permalink
Remove juju debug-log from test logs when a test fails (#1416)
Browse files Browse the repository at this point in the history
* set logging level for connsuite

* add envar to test config to avoid connsuite logs
  • Loading branch information
SimoneDutto authored Nov 5, 2024
1 parent 152d43c commit 93e5eec
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 53 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
],
"go.lintOnSave": "workspace",
"go.testEnvVars": {
"JIMM_TEST_LOG_SQL": "false"
"JIMM_TEST_LOG_SQL": "false",
"TEST_LOGGING_CONFIG": "ERROR" // this is the logging level for juju connsuite logs
}
}
40 changes: 40 additions & 0 deletions internal/logger/gocheck_logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright 2024 Canonical.

package logger

import (
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
gc "gopkg.in/check.v1"
)

// NewGoCheckLogger create a logger to be used by the gocheck test library.
// The logs are shown only when the test fails.
func NewGoCheckLogger(c *gc.C) *zap.Logger {
output := gocheckZapWriter{c}

devConfig := zap.NewDevelopmentEncoderConfig()
devConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder
devConfig.EncodeTime = shortTimeEncoder

return zap.New(
zapcore.NewCore(
zapcore.NewConsoleEncoder(devConfig),
output,
zap.DebugLevel,
),
)
}

type gocheckZapWriter struct {
c *gc.C
}

func (w gocheckZapWriter) Write(buf []byte) (int, error) {
w.c.Logf(string(buf))
return len(buf), nil
}

func (w gocheckZapWriter) Sync() error {
return nil
}
56 changes: 4 additions & 52 deletions internal/testutils/jimmtest/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,10 @@
package jimmtest

import (
"os"
"strings"

"github.com/juju/loggo"
"github.com/juju/zaputil"
"github.com/juju/zaputil/zapctx"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
gc "gopkg.in/check.v1"

"github.com/canonical/jimm/v3/internal/logger"
)

// LoggingSuite is a replacement for github.com/juju/testing.LoggingSuite
Expand All @@ -34,49 +29,6 @@ func (s *LoggingSuite) TearDownTest(c *gc.C) {
}

func (s *LoggingSuite) setUp(c *gc.C) {
output := gocheckZapWriter{c}
logger := zap.New(zapcore.NewCore(
zapcore.NewConsoleEncoder(zapcore.EncoderConfig{
LevelKey: "level",
MessageKey: "msg",
EncodeLevel: zapcore.CapitalLevelEncoder,
EncodeTime: zapcore.ISO8601TimeEncoder,
}),
output,
zap.DebugLevel,
))

zapctx.Default = logger

loggo.ResetLogging()
// Don't use the default writer for the test logging, which
// means we can still get logging output from tests that
// replace the default writer.
err := loggo.RegisterWriter(loggo.DefaultWriterName, discardWriter{})
c.Assert(err, gc.IsNil)
err = loggo.RegisterWriter("loggingsuite", zaputil.NewLoggoWriter(logger))
c.Assert(err, gc.IsNil)
level := "DEBUG"
if envLevel := os.Getenv("TEST_LOGGING_CONFIG"); envLevel != "" {
level = envLevel
}
err = loggo.ConfigureLoggers(level)
c.Assert(err, gc.Equals, nil)
}

type discardWriter struct{}

func (discardWriter) Write(entry loggo.Entry) {
}

type gocheckZapWriter struct {
c *gc.C
}

func (w gocheckZapWriter) Write(buf []byte) (int, error) {
return len(buf), w.c.Output(1, strings.TrimSuffix(string(buf), "\n"))
}

func (w gocheckZapWriter) Sync() error {
return nil
goCheckLogger := logger.NewGoCheckLogger(c)
zapctx.Default = goCheckLogger
}

0 comments on commit 93e5eec

Please sign in to comment.