Skip to content

Commit

Permalink
martian/log: streamline logger and make it context aware
Browse files Browse the repository at this point in the history
Remove all logic from logger the package just map calls from package level function to the provided Logger impl.
Add context to the Logger interface, this allows to add custom logger wrappers using the context information.
  • Loading branch information
mmatczuk committed Feb 9, 2024
1 parent 1a8ef06 commit 151dc0c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 42 deletions.
51 changes: 9 additions & 42 deletions internal/martian/log/log.go
Original file line number Diff line number Diff line change
@@ -1,70 +1,37 @@
// Copyright 2015 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Copyright 2023 Sauce Labs Inc., all rights reserved.

// Package log provides a universal logger for martian packages.
package log

import (
"context"
"fmt"
)

type Logger interface {
Infof(format string, args ...any)
Debugf(format string, args ...any)
Errorf(format string, args ...any)
Infof(ctx context.Context, format string, args ...any)
Debugf(ctx context.Context, format string, args ...any)
Errorf(ctx context.Context, format string, args ...any)
}

var currLogger Logger = nopLogger{}
var log Logger = nopLogger{}

// SetLogger changes the default logger. This must be called very first,
// before interacting with rest of the martian package. Changing it at
// runtime is not supported.
func SetLogger(l Logger) {
currLogger = l
log = l
}

type contextKey string

const TraceContextKey contextKey = "trace"

// Infof logs an info message.
func Infof(ctx context.Context, format string, args ...any) {
currLogger.Infof(withTrace(ctx, format), args...)
log.Infof(ctx, format, args...)
}

// Debugf logs a debug message.
func Debugf(ctx context.Context, format string, args ...any) {
currLogger.Debugf(withTrace(ctx, format), args...)
log.Debugf(ctx, format, args...)
}

// Errorf logs an error message.
func Errorf(ctx context.Context, format string, args ...any) {
currLogger.Errorf(withTrace(ctx, format), args...)
}

func withTrace(ctx context.Context, format string) string {
if v := ctx.Value(TraceContextKey); v != nil {
format = fmt.Sprintf("[%s] %s", v, format)
}
return format
log.Errorf(ctx, format, args...)
}

type nopLogger struct{}

func (nopLogger) Infof(_ string, _ ...any) {}

func (nopLogger) Debugf(_ string, _ ...any) {}

func (nopLogger) Errorf(_ string, _ ...any) {}
17 changes: 17 additions & 0 deletions internal/martian/log/nop.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright 2023 Sauce Labs Inc., all rights reserved.

package log

import (
"context"
)

type nopLogger struct{}

var _ Logger = nopLogger{}

func (nopLogger) Infof(_ context.Context, _ string, _ ...any) {}

func (nopLogger) Debugf(_ context.Context, _ string, _ ...any) {}

func (nopLogger) Errorf(_ context.Context, _ string, _ ...any) {}

0 comments on commit 151dc0c

Please sign in to comment.