move to untimestamped logs, tag the lots INFO and DEBUG. Update tests
This commit is contained in:
@ -4,43 +4,49 @@ package logger
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
|
||||
"govpp-snmp-agentx/config"
|
||||
)
|
||||
|
||||
// logf logs a message with automatic caller information (file:function)
|
||||
func logf(format string, args ...interface{}) {
|
||||
pc, file, _, ok := runtime.Caller(2)
|
||||
// getCallerInfo returns caller information in the format "file.go:function"
|
||||
func getCallerInfo() string {
|
||||
pc, file, _, ok := runtime.Caller(2) // Skip getCallerInfo and Printf/Debugf
|
||||
if !ok {
|
||||
log.Printf(format, args...)
|
||||
return
|
||||
return "unknown:unknown"
|
||||
}
|
||||
|
||||
fn := runtime.FuncForPC(pc)
|
||||
if fn == nil {
|
||||
log.Printf(format, args...)
|
||||
return
|
||||
return "unknown:unknown"
|
||||
}
|
||||
|
||||
funcName := filepath.Base(fn.Name())
|
||||
fileName := filepath.Base(file)
|
||||
|
||||
prefix := fmt.Sprintf("%s:%s", fileName, funcName)
|
||||
message := fmt.Sprintf(format, args...)
|
||||
log.Printf("%s %s", prefix, message)
|
||||
return fmt.Sprintf("%s:%s", fileName, funcName)
|
||||
}
|
||||
|
||||
// Printf logs a message with caller information
|
||||
// Printf logs a message with caller information in SYSLOG style
|
||||
func Printf(format string, args ...interface{}) {
|
||||
logf(format, args...)
|
||||
caller := getCallerInfo()
|
||||
message := fmt.Sprintf(format, args...)
|
||||
syslogMessage := fmt.Sprintf("INFO %s %s", caller, message)
|
||||
fmt.Println(syslogMessage)
|
||||
}
|
||||
|
||||
// Debugf logs a debug message with caller information if global debug is enabled
|
||||
func Debugf(format string, args ...interface{}) {
|
||||
if config.Debug {
|
||||
logf(format, args...)
|
||||
caller := getCallerInfo()
|
||||
message := fmt.Sprintf(format, args...)
|
||||
syslogMessage := fmt.Sprintf("DEBUG %s %s", caller, message)
|
||||
fmt.Println(syslogMessage)
|
||||
}
|
||||
}
|
||||
|
||||
// Sync flushes any buffered log entries (no-op for fmt.Println)
|
||||
func Sync() {
|
||||
// No buffering with fmt.Println, so this is a no-op
|
||||
}
|
@ -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