-
Notifications
You must be signed in to change notification settings - Fork 117
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Replace seelog logger with internal logger. The seelog logger had various drawbacks: - not easily user configurable (e.g. can't disable, can't write to a file, can't write to stderr, can't write to json, etc) - uses global seelog instance which can conflict w/ other packages or user's logger The replacement logger uses a simple Logger interface: type Logger interface { Log(level LogLevel, msg fmt.Stringer) } Rather than a separate method for each log level, the level is passed as an argument. This makes it easy/possible to add more log levels in the future. The lowest log level is now "debug" instead of seelog's "trace" (seemed mildly confusing to have a log method named "trace" in a cloud tracing package). We use a fmt.Stringer so any log message serialization can be deferred until the message is actually written. There is a default Logger implementation the user can use via NewDefaultLogger(io.Writer, LogLevel) to create a logger that writes log messages of at least a certain log level to a specified io.Writer. The logger defaults to a stdout writer of min level "info". The "xraylog" package is the user visible logging API. It contains "xray" in the package name so it doesn't annoyingly look like every other "logger" package (e.g. to goimports). The user can set a new logger using xray.SetLogger(). The actual logging instance and logging wrapper methods are in an internal "logger" package hidden from the user. There are Info(...interface{}) and Infof(string, ...interface{}) varieties for each log level. In addition there is a DebugDeferred(func() string) takes an arbitrary function (which won't get called unless the log message is actually logged). * Add README.md note about logger interface * Synchronize log messages in default logger. Use a mutex to synchronize calls to underling io.Writer. This avoids log message interleaving and potentially worse race conditions, depending on what the writer is. Also tweak the invalid LogLevel String() to be UNKNOWNLOGLEVEL instead of just UNKNOWN so it is easier to understand what is unknown if it ever shows up.
- Loading branch information
Muir Manders
authored and
Yogiraj Awati
committed
Feb 11, 2019
1 parent
0aa94d4
commit 852dc09
Showing
23 changed files
with
381 additions
and
184 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
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
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
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
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
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,78 @@ | ||
// Copyright 2017-2017 Amazon.com, Inc. or its affiliates. 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. A copy of the License is located at | ||
// | ||
// http://aws.amazon.com/apache2.0/ | ||
// | ||
// or in the "license" file accompanying this file. This file 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. | ||
|
||
package logger | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/aws/aws-xray-sdk-go/xraylog" | ||
) | ||
|
||
// This internal pacakge hides the actual logging functions from the user. | ||
|
||
// The Logger instance used by xray to log. Set via xray.SetLogger(). | ||
var Logger xraylog.Logger = xraylog.NewDefaultLogger(os.Stdout, xraylog.LogLevelInfo) | ||
|
||
func Debugf(format string, args ...interface{}) { | ||
Logger.Log(xraylog.LogLevelDebug, printfArgs{format, args}) | ||
} | ||
|
||
func Debug(args ...interface{}) { | ||
Logger.Log(xraylog.LogLevelDebug, printArgs(args)) | ||
} | ||
|
||
func DebugDeferred(fn func() string) { | ||
Logger.Log(xraylog.LogLevelDebug, stringerFunc(fn)) | ||
} | ||
|
||
func Infof(format string, args ...interface{}) { | ||
Logger.Log(xraylog.LogLevelInfo, printfArgs{format, args}) | ||
} | ||
|
||
func Info(args ...interface{}) { | ||
Logger.Log(xraylog.LogLevelInfo, printArgs(args)) | ||
} | ||
|
||
func Warnf(format string, args ...interface{}) { | ||
Logger.Log(xraylog.LogLevelWarn, printfArgs{format, args}) | ||
} | ||
|
||
func Warn(args ...interface{}) { | ||
Logger.Log(xraylog.LogLevelWarn, printArgs(args)) | ||
} | ||
|
||
func Errorf(format string, args ...interface{}) { | ||
Logger.Log(xraylog.LogLevelError, printfArgs{format, args}) | ||
} | ||
|
||
func Error(args ...interface{}) { | ||
Logger.Log(xraylog.LogLevelError, printArgs(args)) | ||
} | ||
|
||
type printfArgs struct { | ||
format string | ||
args []interface{} | ||
} | ||
|
||
func (p printfArgs) String() string { | ||
return fmt.Sprintf(p.format, p.args...) | ||
} | ||
|
||
type printArgs []interface{} | ||
|
||
func (p printArgs) String() string { | ||
return fmt.Sprint([]interface{}(p)...) | ||
} | ||
|
||
type stringerFunc func() string | ||
|
||
func (sf stringerFunc) String() string { | ||
return sf() | ||
} |
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,83 @@ | ||
// Copyright 2017-2017 Amazon.com, Inc. or its affiliates. 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. A copy of the License is located at | ||
// | ||
// http://aws.amazon.com/apache2.0/ | ||
// | ||
// or in the "license" file accompanying this file. This file 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. | ||
|
||
package logger | ||
|
||
import ( | ||
"bytes" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/aws/aws-xray-sdk-go/xraylog" | ||
) | ||
|
||
func TestLogger(t *testing.T) { | ||
oldLogger := Logger | ||
defer func() { Logger = oldLogger }() | ||
|
||
var buf bytes.Buffer | ||
|
||
// filter properly by level | ||
Logger = xraylog.NewDefaultLogger(&buf, xraylog.LogLevelWarn) | ||
|
||
Debug("debug") | ||
Info("info") | ||
Warn("warn") | ||
Error("error") | ||
|
||
gotLines := strings.Split(strings.TrimSpace(buf.String()), "\n") | ||
if len(gotLines) != 2 { | ||
t.Fatalf("got %d lines", len(gotLines)) | ||
} | ||
|
||
if !strings.Contains(gotLines[0], "[WARN] warn") { | ||
t.Error("expected first line to be warn") | ||
} | ||
|
||
if !strings.Contains(gotLines[1], "[ERROR] error") { | ||
t.Error("expected second line to be warn") | ||
} | ||
} | ||
|
||
func TestDeferredDebug(t *testing.T) { | ||
oldLogger := Logger | ||
defer func() { Logger = oldLogger }() | ||
|
||
var buf bytes.Buffer | ||
|
||
Logger = xraylog.NewDefaultLogger(&buf, xraylog.LogLevelInfo) | ||
|
||
var called bool | ||
DebugDeferred(func() string { | ||
called = true | ||
return "deferred" | ||
}) | ||
|
||
if called { | ||
t.Error("deferred should not have been called") | ||
} | ||
|
||
if buf.String() != "" { | ||
t.Errorf("unexpected log contents: %s", buf.String()) | ||
} | ||
|
||
Logger = xraylog.NewDefaultLogger(&buf, xraylog.LogLevelDebug) | ||
|
||
DebugDeferred(func() string { | ||
called = true | ||
return "deferred" | ||
}) | ||
|
||
if !called { | ||
t.Error("deferred should have been called") | ||
} | ||
|
||
if !strings.Contains(buf.String(), "[DEBUG] deferred") { | ||
t.Errorf("expected deferred message, got %s", buf.String()) | ||
} | ||
} |
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
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
Oops, something went wrong.