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
|
||||
}
|
Reference in New Issue
Block a user