-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
martian/log: streamline logger and make it context aware
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
Showing
2 changed files
with
26 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) {} |