Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

github.com/sirupsen/logrus usage refactoring #2960

Merged
merged 1 commit into from
Mar 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ linters-settings:
- '^os\.(.*)$(# Using anything except Signal and SyscallError from the os package is forbidden )?'
# Forbid everything in syscall except the uppercase constants
- '^syscall\.[^A-Z_]+$(# Using anything except constants from the syscall package is forbidden )?'
- '^logrus\.Logger$'

linters:
disable-all: true
Expand Down
2 changes: 0 additions & 2 deletions api/v1/group_routes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"net/http/httptest"
"testing"

"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

Expand All @@ -22,7 +21,6 @@ import (
func getTestPreInitState(tb testing.TB) *lib.TestPreInitState {
reg := metrics.NewRegistry()
logger := testutils.NewLogger(tb)
logger.SetLevel(logrus.DebugLevel)
return &lib.TestPreInitState{
Logger: logger,
RuntimeOptions: lib.RuntimeOptions{},
Expand Down
8 changes: 4 additions & 4 deletions cloudapi/logs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func TestMSGLog(t *testing.T) {

logger := logrus.New()
logger.Out = ioutil.Discard
hook := &testutils.SimpleLogrusHook{HookedLevels: logrus.AllLevels}
hook := testutils.NewLogHook()
logger.AddHook(hook)
expectMsg.Log(logger)
logLines := hook.Drain()
Expand Down Expand Up @@ -250,7 +250,7 @@ func TestStreamLogsToLogger(t *testing.T) {

logger := logrus.New()
logger.Out = ioutil.Discard
hook := &testutils.SimpleLogrusHook{HookedLevels: logrus.AllLevels}
hook := testutils.NewLogHook()
logger.AddHook(hook)

c := configFromHTTPMultiBin(tb)
Expand Down Expand Up @@ -322,7 +322,7 @@ func TestStreamLogsToLogger(t *testing.T) {

logger := logrus.New()
logger.Out = ioutil.Discard
hook := &testutils.SimpleLogrusHook{HookedLevels: logrus.AllLevels}
hook := testutils.NewLogHook()
logger.AddHook(hook)

c := configFromHTTPMultiBin(tb)
Expand Down Expand Up @@ -390,7 +390,7 @@ func TestStreamLogsToLogger(t *testing.T) {

logger := logrus.New()
logger.Out = ioutil.Discard
hook := &testutils.SimpleLogrusHook{HookedLevels: logrus.AllLevels}
hook := testutils.NewLogHook()
logger.AddHook(hook)

c := configFromHTTPMultiBin(tb)
Expand Down
10 changes: 5 additions & 5 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -355,19 +355,19 @@ func getCmdRun(gs *state.GlobalState) *cobra.Command {
exampleText := getExampleText(gs, `
# Run a single VU, once.
{{.}} run script.js

# Run a single VU, 10 times.
{{.}} run -i 10 script.js

# Run 5 VUs, splitting 10 iterations between them.
{{.}} run -u 5 -i 10 script.js

# Run 5 VUs for 10s.
{{.}} run -u 5 -d 10s script.js

# Ramp VUs from 0 to 100 over 10s, stay there for 60s, then 10s down to 0.
{{.}} run -u 0 -s 10s:100 -s 60s -s 10s:0

# Send metrics to an influxdb server
{{.}} run -o influxdb=http://1.2.3.4:8086/k6`[1:])

Expand Down
3 changes: 1 addition & 2 deletions cmd/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"io"
"io/fs"
"io/ioutil"
"log"
"path"
"path/filepath"
"runtime"
Expand Down Expand Up @@ -318,7 +317,7 @@ func TestThresholdsRuntimeBehavior(t *testing.T) {
}

if tc.expStdoutNotContains != "" {
log.Println(ts.Stdout.String())
t.Log(ts.Stdout.String())
assert.NotContains(t, ts.Stdout.String(), tc.expStdoutNotContains)
}
})
Expand Down
2 changes: 1 addition & 1 deletion cmd/test_load.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ func loadAndConfigureTest(
}

// loadSystemCertPool attempts to load system certificates.
func loadSystemCertPool(logger *logrus.Logger) {
func loadSystemCertPool(logger logrus.FieldLogger) {
if _, err := x509.SystemCertPool(); err != nil {
logger.WithError(err).Warning("Unable to load system cert pool")
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/tests/test_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func NewGlobalTestState(tb testing.TB) *GlobalTestState {
logger := logrus.New()
logger.SetLevel(logrus.InfoLevel)
logger.Out = testutils.NewTestOutput(tb)
hook := &testutils.SimpleLogrusHook{HookedLevels: logrus.AllLevels}
hook := testutils.NewLogHook()
na-- marked this conversation as resolved.
Show resolved Hide resolved
logger.AddHook(hook)

ts := &GlobalTestState{
Expand Down
16 changes: 8 additions & 8 deletions execution/scheduler_ext_exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ func TestExecutionInfoVUSharing(t *testing.T) {

logger := logrus.New()
logger.SetOutput(ioutil.Discard)
logHook := testutils.SimpleLogrusHook{HookedLevels: []logrus.Level{logrus.InfoLevel}}
logger.AddHook(&logHook)
logHook := testutils.NewLogHook(logrus.InfoLevel)
logger.AddHook(logHook)

registry := metrics.NewRegistry()
builtinMetrics := metrics.RegisterBuiltinMetrics(registry)
Expand Down Expand Up @@ -177,8 +177,8 @@ func TestExecutionInfoScenarioIter(t *testing.T) {

logger := logrus.New()
logger.SetOutput(ioutil.Discard)
logHook := testutils.SimpleLogrusHook{HookedLevels: []logrus.Level{logrus.InfoLevel}}
logger.AddHook(&logHook)
logHook := testutils.NewLogHook(logrus.InfoLevel)
logger.AddHook(logHook)

registry := metrics.NewRegistry()
builtinMetrics := metrics.RegisterBuiltinMetrics(registry)
Expand Down Expand Up @@ -259,8 +259,8 @@ func TestSharedIterationsStable(t *testing.T) {

logger := logrus.New()
logger.SetOutput(ioutil.Discard)
logHook := testutils.SimpleLogrusHook{HookedLevels: []logrus.Level{logrus.InfoLevel}}
logger.AddHook(&logHook)
logHook := testutils.NewLogHook(logrus.InfoLevel)
logger.AddHook(logHook)

registry := metrics.NewRegistry()
builtinMetrics := metrics.RegisterBuiltinMetrics(registry)
Expand Down Expand Up @@ -394,8 +394,8 @@ func TestExecutionInfoAll(t *testing.T) {

logger := logrus.New()
logger.SetOutput(ioutil.Discard)
logHook := testutils.SimpleLogrusHook{HookedLevels: []logrus.Level{logrus.InfoLevel}}
logger.AddHook(&logHook)
logHook := testutils.NewLogHook(logrus.InfoLevel)
logger.AddHook(logHook)

registry := metrics.NewRegistry()
builtinMetrics := metrics.RegisterBuiltinMetrics(registry)
Expand Down
6 changes: 3 additions & 3 deletions execution/scheduler_ext_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func getTestRunState(
}

func newTestScheduler(
t *testing.T, runner lib.Runner, logger *logrus.Logger, opts lib.Options,
t *testing.T, runner lib.Runner, logger logrus.FieldLogger, opts lib.Options,
) (ctx context.Context, cancel func(), execScheduler *execution.Scheduler, samples chan metrics.SampleContainer) {
if runner == nil {
runner = &minirunner.MiniRunner{}
Expand Down Expand Up @@ -1083,8 +1083,8 @@ func TestDNSResolver(t *testing.T) {
t.Parallel()
logger := logrus.New()
logger.SetOutput(ioutil.Discard)
logHook := testutils.SimpleLogrusHook{HookedLevels: []logrus.Level{logrus.WarnLevel}}
logger.AddHook(&logHook)
logHook := testutils.NewLogHook(logrus.WarnLevel)
logger.AddHook(logHook)

registry := metrics.NewRegistry()
builtinMetrics := metrics.RegisterBuiltinMetrics(registry)
Expand Down
14 changes: 7 additions & 7 deletions js/bundle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import (

const isWindows = runtime.GOOS == "windows"

func getTestPreInitState(tb testing.TB, logger *logrus.Logger, rtOpts *lib.RuntimeOptions) *lib.TestPreInitState {
func getTestPreInitState(tb testing.TB, logger logrus.FieldLogger, rtOpts *lib.RuntimeOptions) *lib.TestPreInitState {
if logger == nil {
logger = testutils.NewLogger(tb)
}
Expand All @@ -50,14 +50,14 @@ func getTestPreInitState(tb testing.TB, logger *logrus.Logger, rtOpts *lib.Runti
func getSimpleBundle(tb testing.TB, filename, data string, opts ...interface{}) (*Bundle, error) {
fs := afero.NewMemMapFs()
var rtOpts *lib.RuntimeOptions
var logger *logrus.Logger
var logger logrus.FieldLogger
for _, o := range opts {
switch opt := o.(type) {
case afero.Fs:
fs = opt
case lib.RuntimeOptions:
rtOpts = &opt
case *logrus.Logger:
case logrus.FieldLogger:
logger = opt
default:
tb.Fatalf("unknown test option %q", opt)
Expand Down Expand Up @@ -435,10 +435,10 @@ func TestNewBundle(t *testing.T) {
logger := logrus.New()
logger.SetLevel(logrus.InfoLevel)
logger.Out = ioutil.Discard
hook := testutils.SimpleLogrusHook{
HookedLevels: []logrus.Level{logrus.WarnLevel, logrus.InfoLevel, logrus.ErrorLevel, logrus.FatalLevel, logrus.PanicLevel},
}
logger.AddHook(&hook)
hook := testutils.NewLogHook(
logrus.WarnLevel, logrus.InfoLevel, logrus.ErrorLevel, logrus.FatalLevel, logrus.PanicLevel,
)
logger.AddHook(hook)

_, err := getSimpleBundle(t, "/script.js", `
export let options = {
Expand Down
18 changes: 6 additions & 12 deletions js/compiler/compiler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,8 @@ func TestCorruptSourceMap(t *testing.T) {
logger := logrus.New()
logger.SetLevel(logrus.DebugLevel)
logger.Out = ioutil.Discard
hook := testutils.SimpleLogrusHook{
HookedLevels: []logrus.Level{logrus.InfoLevel, logrus.WarnLevel},
}
logger.AddHook(&hook)
hook := testutils.NewLogHook(logrus.InfoLevel, logrus.WarnLevel)
logger.AddHook(hook)

compiler := New(logger)
compiler.Options = Options{
Expand Down Expand Up @@ -199,10 +197,8 @@ func TestCorruptSourceMapOnlyForBabel(t *testing.T) {
logger := logrus.New()
logger.SetLevel(logrus.DebugLevel)
logger.Out = ioutil.Discard
hook := testutils.SimpleLogrusHook{
HookedLevels: []logrus.Level{logrus.InfoLevel, logrus.WarnLevel},
}
logger.AddHook(&hook)
hook := testutils.NewLogHook(logrus.InfoLevel, logrus.WarnLevel)
logger.AddHook(hook)

compiler := New(logger)
compiler.Options = Options{
Expand Down Expand Up @@ -231,10 +227,8 @@ func TestMinimalSourceMap(t *testing.T) {
logger := logrus.New()
logger.SetLevel(logrus.DebugLevel)
logger.Out = ioutil.Discard
hook := testutils.SimpleLogrusHook{
HookedLevels: []logrus.Level{logrus.InfoLevel, logrus.WarnLevel},
}
logger.AddHook(&hook)
hook := testutils.NewLogHook(logrus.InfoLevel, logrus.WarnLevel)
logger.AddHook(hook)

compiler := New(logger)
compiler.Options = Options{
Expand Down
2 changes: 1 addition & 1 deletion js/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/sirupsen/logrus"
)

// console represents a JS console implemented as a logrus.Logger.
// console represents a JS console implemented as a logrus.FieldLogger.
type console struct {
logger logrus.FieldLogger
}
Expand Down
9 changes: 4 additions & 5 deletions js/console_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func getSimpleRunner(tb testing.TB, filename, data string, opts ...interface{})
fsResolvers = opt
case lib.RuntimeOptions:
rtOpts = opt
case *logrus.Logger:
case logrus.FieldLogger:
logger = opt
default:
tb.Fatalf("unknown test option %q", opt)
Expand All @@ -82,6 +82,7 @@ func getSimpleRunner(tb testing.TB, filename, data string, opts ...interface{})
)
}

// TODO: remove the need for this function, see https://github.com/grafana/k6/issues/2968
func extractLogger(fl logrus.FieldLogger) *logrus.Logger {
switch e := fl.(type) {
case *logrus.Entry:
Expand All @@ -103,8 +104,7 @@ func TestConsoleLogWithGojaNativeObject(t *testing.T) {
err := obj.Set("text", "nativeObject")
require.NoError(t, err)

logger := testutils.NewLogger(t)
hook := logtest.NewLocal(logger)
logger, hook := testutils.NewLoggerWithHook(t)

c := newConsole(logger)
c.Log(obj)
Expand Down Expand Up @@ -160,8 +160,7 @@ func TestConsoleLogObjectsWithGoTypes(t *testing.T) {
rt.SetFieldNameMapper(common.FieldNameMapper{})
obj := rt.ToValue(tt.in)

logger := testutils.NewLogger(t)
hook := logtest.NewLocal(logger)
logger, hook := testutils.NewLoggerWithHook(t)

c := newConsole(logger)
c.Log(obj)
Expand Down
2 changes: 1 addition & 1 deletion js/modules/k6/execution/execution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func TestVUTagsSuccessOverwriteSystemTag(t *testing.T) {
func TestVUTagsErrorOutOnInvalidValues(t *testing.T) {
t.Parallel()

logHook := &testutils.SimpleLogrusHook{HookedLevels: []logrus.Level{logrus.WarnLevel}}
logHook := testutils.NewLogHook(logrus.WarnLevel)
testLog := logrus.New()
testLog.AddHook(logHook)
testLog.SetOutput(ioutil.Discard)
Expand Down
4 changes: 1 addition & 3 deletions js/modules/k6/grpc/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -949,9 +949,7 @@ func TestClientInvokeHeadersDeprecated(t *testing.T) {

registry := metrics.NewRegistry()

logHook := &testutils.SimpleLogrusHook{
HookedLevels: []logrus.Level{logrus.WarnLevel},
}
logHook := testutils.NewLogHook(logrus.WarnLevel)
testLog := logrus.New()
testLog.AddHook(logHook)
testLog.SetOutput(ioutil.Discard)
Expand Down
Loading