move to untimestamped logs, tag the lots INFO and DEBUG. Update tests
This commit is contained in:
@ -4,7 +4,7 @@ package logger
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"log"
|
||||
"io"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
@ -13,19 +13,33 @@ import (
|
||||
)
|
||||
|
||||
func TestPrintf(t *testing.T) {
|
||||
// Capture log output
|
||||
var buf bytes.Buffer
|
||||
log.SetOutput(&buf)
|
||||
defer log.SetOutput(os.Stderr)
|
||||
// Capture stdout
|
||||
oldStdout := os.Stdout
|
||||
r, w, _ := os.Pipe()
|
||||
os.Stdout = w
|
||||
|
||||
Printf("test message: %s", "hello")
|
||||
|
||||
// Close writer and restore stdout
|
||||
w.Close()
|
||||
os.Stdout = oldStdout
|
||||
|
||||
// Read captured output
|
||||
var buf bytes.Buffer
|
||||
io.Copy(&buf, r)
|
||||
output := buf.String()
|
||||
if !strings.Contains(output, "logger_test.go:logger.TestPrintf") {
|
||||
t.Errorf("Expected log output to contain caller info, got: %s", output)
|
||||
|
||||
// Check output format: "INFO file.go:function message"
|
||||
if !strings.HasPrefix(output, "INFO ") {
|
||||
t.Errorf("Expected output to start with 'INFO ', got: %s", output)
|
||||
}
|
||||
|
||||
if !strings.Contains(output, "logger_test.go:logger.TestPrintf") {
|
||||
t.Errorf("Expected output to contain caller info, got: %s", output)
|
||||
}
|
||||
|
||||
if !strings.Contains(output, "test message: hello") {
|
||||
t.Errorf("Expected log output to contain message, got: %s", output)
|
||||
t.Errorf("Expected output to contain message, got: %s", output)
|
||||
}
|
||||
}
|
||||
|
||||
@ -37,16 +51,29 @@ func TestDebugfWithDebugEnabled(t *testing.T) {
|
||||
// Enable debug
|
||||
config.Debug = true
|
||||
|
||||
// Capture log output
|
||||
var buf bytes.Buffer
|
||||
log.SetOutput(&buf)
|
||||
defer log.SetOutput(os.Stderr)
|
||||
// Capture stdout
|
||||
oldStdout := os.Stdout
|
||||
r, w, _ := os.Pipe()
|
||||
os.Stdout = w
|
||||
|
||||
Debugf("debug message: %s", "test")
|
||||
|
||||
// Close writer and restore stdout
|
||||
w.Close()
|
||||
os.Stdout = oldStdout
|
||||
|
||||
// Read captured output
|
||||
var buf bytes.Buffer
|
||||
io.Copy(&buf, r)
|
||||
output := buf.String()
|
||||
|
||||
// Check output format: "DEBUG file.go:function message"
|
||||
if !strings.HasPrefix(output, "DEBUG ") {
|
||||
t.Errorf("Expected output to start with 'DEBUG ', got: %s", output)
|
||||
}
|
||||
|
||||
if !strings.Contains(output, "debug message: test") {
|
||||
t.Errorf("Expected debug message to be logged when debug is enabled, got: %s", output)
|
||||
t.Errorf("Expected output to contain message, got: %s", output)
|
||||
}
|
||||
}
|
||||
|
||||
@ -58,15 +85,29 @@ func TestDebugfWithDebugDisabled(t *testing.T) {
|
||||
// Disable debug
|
||||
config.Debug = false
|
||||
|
||||
// Capture log output
|
||||
var buf bytes.Buffer
|
||||
log.SetOutput(&buf)
|
||||
defer log.SetOutput(os.Stderr)
|
||||
// Capture stdout
|
||||
oldStdout := os.Stdout
|
||||
r, w, _ := os.Pipe()
|
||||
os.Stdout = w
|
||||
|
||||
Debugf("debug message: %s", "test")
|
||||
|
||||
// Close writer and restore stdout
|
||||
w.Close()
|
||||
os.Stdout = oldStdout
|
||||
|
||||
// Read captured output
|
||||
var buf bytes.Buffer
|
||||
io.Copy(&buf, r)
|
||||
output := buf.String()
|
||||
if strings.Contains(output, "debug message: test") {
|
||||
t.Errorf("Expected debug message to NOT be logged when debug is disabled, got: %s", output)
|
||||
|
||||
// Should be empty when debug is disabled
|
||||
if output != "" {
|
||||
t.Errorf("Expected no output when debug is disabled, got: %s", output)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSync(t *testing.T) {
|
||||
// Test that Sync doesn't panic (it's a no-op now)
|
||||
Sync()
|
||||
}
|
Reference in New Issue
Block a user