Add a logger that auto-includes file+callsite

This commit is contained in:
Pim van Pelt
2025-06-09 18:06:59 +02:00
parent 6371e8eee2
commit 29d0417452
3 changed files with 57 additions and 24 deletions

View File

@ -2,7 +2,6 @@ package ifmib
import (
"fmt"
"log"
"sync"
"time"
@ -10,6 +9,8 @@ import (
"github.com/posteo/go-agentx/pdu"
"github.com/posteo/go-agentx/value"
"go.fd.io/govpp/api"
"govpp-snmp-example/logger"
)
// IF-MIB OID bases:
@ -85,9 +86,7 @@ func (m *InterfaceMIB) UpdateStats(interfaceStats *api.InterfaceStats) {
m.mutex.Lock()
defer m.mutex.Unlock()
if m.debug {
log.Printf("Updating IF-MIB with %d interfaces", len(interfaceStats.Interfaces))
}
logger.Debugf(m.debug, "Updating IF-MIB with %d interfaces", len(interfaceStats.Interfaces))
// Clear existing entries
m.handler = &agentx.ListHandler{}
@ -95,9 +94,7 @@ func (m *InterfaceMIB) UpdateStats(interfaceStats *api.InterfaceStats) {
// Add new entries
for _, iface := range interfaceStats.Interfaces {
if m.debug {
log.Printf("Processing interface %d (%s)", iface.InterfaceIndex, iface.InterfaceName)
}
logger.Debugf(m.debug, "Processing interface %d (%s)", iface.InterfaceIndex, iface.InterfaceName)
m.stats[iface.InterfaceIndex] = &iface
m.addInterfaceToMIB(&iface)
}
@ -108,12 +105,10 @@ func (m *InterfaceMIB) UpdateStats(interfaceStats *api.InterfaceStats) {
}
if m.ifXTableSession != nil {
m.ifXTableSession.Handler = m.handler
log.Printf("Updated session handlers with new IF-MIB data")
logger.Printf("Updated session handlers with new IF-MIB data")
}
if m.debug {
log.Printf("IF-MIB now contains %d interfaces", len(m.stats))
}
logger.Debugf(m.debug, "IF-MIB now contains %d interfaces", len(m.stats))
}
func (m *InterfaceMIB) addInterfaceToMIB(iface *api.InterfaceCounters) {
@ -125,9 +120,7 @@ func (m *InterfaceMIB) addInterfaceToMIB(iface *api.InterfaceCounters) {
// Add ifXTable (extended interface table) entries
m.addIfXTable(iface, idx)
if m.debug {
log.Printf("Added interface %d (%s) to IF-MIB with SNMP index %d", iface.InterfaceIndex, iface.InterfaceName, idx)
}
logger.Debugf(m.debug, "Added interface %d (%s) to IF-MIB with SNMP index %d", iface.InterfaceIndex, iface.InterfaceName, idx)
}
func (m *InterfaceMIB) addIfEntry(iface *api.InterfaceCounters, idx int) {
@ -344,9 +337,7 @@ func (m *InterfaceMIB) RegisterWithClient(client *agentx.Client) error {
return fmt.Errorf("failed to register ifXTable: %v", err)
}
if m.debug {
log.Printf("Registered IF-MIB ifEntry at OID %s", ifEntryOID)
log.Printf("Registered IF-MIB ifXTable at OID %s", ifXTableOID)
}
logger.Debugf(m.debug, "Registered IF-MIB ifEntry at OID %s", ifEntryOID)
logger.Debugf(m.debug, "Registered IF-MIB ifXTable at OID %s", ifXTableOID)
return nil
}