Files
govpp-snmp-agentx/logger/logger.go

52 lines
1.3 KiB
Go

// Copyright 2025, IPng Networks GmbH, Pim van Pelt <pim@ipng.ch>
package logger
import (
"fmt"
"path/filepath"
"runtime"
"govpp-snmp-agentx/config"
)
// 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 {
return "unknown:unknown"
}
fn := runtime.FuncForPC(pc)
if fn == nil {
return "unknown:unknown"
}
funcName := filepath.Base(fn.Name())
fileName := filepath.Base(file)
return fmt.Sprintf("%s:%s", fileName, funcName)
}
// Printf logs a message with caller information in SYSLOG style
func Printf(format string, args ...interface{}) {
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 {
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
}