-
Notifications
You must be signed in to change notification settings - Fork 242
ref: create debuglog package #1105
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,69 @@ | ||
package debuglog | ||
|
||
import ( | ||
"io" | ||
"log" | ||
"sync" | ||
) | ||
|
||
var ( | ||
logger *log.Logger | ||
mu sync.RWMutex | ||
) | ||
|
||
func init() { | ||
logger = log.New(io.Discard, "[Sentry] ", log.LstdFlags) | ||
} | ||
|
||
// SetLogger replaces the current debug logger with a new one. | ||
// This function is thread-safe and can be called concurrently. | ||
func SetLogger(l *log.Logger) { | ||
mu.Lock() | ||
defer mu.Unlock() | ||
logger = l | ||
} | ||
|
||
func SetOutput(w io.Writer) { | ||
logger.SetOutput(w) | ||
} | ||
cursor[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// GetLogger returns the current logger instance. | ||
// This function is thread-safe and can be called concurrently. | ||
func GetLogger() *log.Logger { | ||
mu.RLock() | ||
defer mu.RUnlock() | ||
return logger | ||
} | ||
|
||
// Printf calls Printf on the underlying logger. | ||
// This function is thread-safe and can be called concurrently. | ||
func Printf(format string, args ...interface{}) { | ||
mu.RLock() | ||
l := logger | ||
mu.RUnlock() | ||
if l != nil { | ||
l.Printf(format, args...) | ||
} | ||
} | ||
|
||
// Println calls Println on the underlying logger. | ||
// This function is thread-safe and can be called concurrently. | ||
func Println(args ...interface{}) { | ||
mu.RLock() | ||
l := logger | ||
mu.RUnlock() | ||
if l != nil { | ||
l.Println(args...) | ||
} | ||
} | ||
|
||
// Print calls Print on the underlying logger. | ||
// This function is thread-safe and can be called concurrently. | ||
func Print(args ...interface{}) { | ||
mu.RLock() | ||
l := logger | ||
mu.RUnlock() | ||
if l != nil { | ||
l.Print(args...) | ||
} | ||
} |
This file contains hidden or 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,135 @@ | ||
package debuglog | ||
|
||
import ( | ||
"bytes" | ||
"io" | ||
stdlog "log" | ||
"strings" | ||
"sync" | ||
"testing" | ||
) | ||
|
||
func TestSetLogger(t *testing.T) { | ||
original := GetLogger() | ||
defer SetLogger(original) | ||
|
||
var buf bytes.Buffer | ||
newLogger := stdlog.New(&buf, "[Test] ", stdlog.LstdFlags) | ||
SetLogger(newLogger) | ||
|
||
if GetLogger() != newLogger { | ||
t.Error("SetLogger did not set the logger correctly") | ||
} | ||
} | ||
|
||
func TestGetLogger(t *testing.T) { | ||
logger := GetLogger() | ||
if logger == nil { | ||
t.Error("GetLogger returned nil") | ||
} | ||
} | ||
|
||
func TestPrintf(t *testing.T) { | ||
original := GetLogger() | ||
defer SetLogger(original) | ||
|
||
var buf bytes.Buffer | ||
testLogger := stdlog.New(&buf, "", 0) | ||
SetLogger(testLogger) | ||
Printf("test %s %d", "message", 42) | ||
|
||
output := buf.String() | ||
if !strings.Contains(output, "test message 42") { | ||
t.Errorf("Printf output incorrect: got %q", output) | ||
} | ||
} | ||
|
||
func TestPrintln(t *testing.T) { | ||
original := GetLogger() | ||
defer SetLogger(original) | ||
|
||
var buf bytes.Buffer | ||
testLogger := stdlog.New(&buf, "", 0) | ||
SetLogger(testLogger) | ||
Println("test", "message") | ||
|
||
output := buf.String() | ||
if !strings.Contains(output, "test message") { | ||
t.Errorf("Println output incorrect: got %q", output) | ||
} | ||
} | ||
|
||
func TestPrint(t *testing.T) { | ||
original := GetLogger() | ||
defer SetLogger(original) | ||
|
||
var buf bytes.Buffer | ||
testLogger := stdlog.New(&buf, "", 0) | ||
SetLogger(testLogger) | ||
Print("test", "message") | ||
|
||
output := buf.String() | ||
if !strings.Contains(output, "testmessage") { | ||
t.Errorf("Print output incorrect: got %q", output) | ||
} | ||
} | ||
|
||
func TestConcurrentAccess(t *testing.T) { | ||
original := GetLogger() | ||
defer SetLogger(original) | ||
|
||
var wg sync.WaitGroup | ||
iterations := 100 | ||
|
||
for i := 0; i < iterations; i++ { | ||
wg.Add(1) | ||
go func(n int) { | ||
defer wg.Done() | ||
Printf("concurrent message %d", n) | ||
}(i) | ||
} | ||
|
||
for i := 0; i < iterations; i++ { | ||
wg.Add(1) | ||
go func() { | ||
defer wg.Done() | ||
_ = GetLogger() | ||
}() | ||
} | ||
|
||
for i := 0; i < 10; i++ { | ||
wg.Add(1) | ||
go func() { | ||
defer wg.Done() | ||
newLogger := stdlog.New(io.Discard, "[Test] ", stdlog.LstdFlags) | ||
SetLogger(newLogger) | ||
}() | ||
} | ||
|
||
wg.Wait() | ||
} | ||
|
||
func TestInitialization(t *testing.T) { | ||
// The logger should be initialized on package load | ||
logger := GetLogger() | ||
if logger == nil { | ||
t.Error("Logger was not initialized") | ||
} | ||
|
||
var buf bytes.Buffer | ||
testLogger := stdlog.New(&buf, "", 0) | ||
SetLogger(testLogger) | ||
Printf("test") | ||
Println("test") | ||
Print("test") | ||
} | ||
|
||
func TestNilLogger(t *testing.T) { | ||
original := GetLogger() | ||
defer SetLogger(original) | ||
|
||
SetLogger(nil) | ||
Printf("test") | ||
Println("test") | ||
Print("test") | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.