mirror of
https://github.com/go-ping/ping.git
synced 2025-07-13 13:54:11 +00:00
Add Logger interface to avoid logging directly (#161)
This commit adds a simple level-based log interface that allows users of the library to control and customize the output, for example by providing a standard Go logger with different settings (timestamps, prefixes, etc). Closes #16. Closes #103. Signed-off-by: Marcelo E. Magallon <marcelo.magallon@grafana.com>
This commit is contained in:
parent
80a5113803
commit
1726e5ede5
53
logger.go
Normal file
53
logger.go
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
package ping
|
||||||
|
|
||||||
|
import "log"
|
||||||
|
|
||||||
|
type Logger interface {
|
||||||
|
Fatalf(format string, v ...interface{})
|
||||||
|
Errorf(format string, v ...interface{})
|
||||||
|
Warnf(format string, v ...interface{})
|
||||||
|
Infof(format string, v ...interface{})
|
||||||
|
Debugf(format string, v ...interface{})
|
||||||
|
}
|
||||||
|
|
||||||
|
type StdLogger struct {
|
||||||
|
Logger *log.Logger
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l StdLogger) Fatalf(format string, v ...interface{}) {
|
||||||
|
l.Logger.Printf("FATAL: "+format, v...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l StdLogger) Errorf(format string, v ...interface{}) {
|
||||||
|
l.Logger.Printf("ERROR: "+format, v...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l StdLogger) Warnf(format string, v ...interface{}) {
|
||||||
|
l.Logger.Printf("WARN: "+format, v...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l StdLogger) Infof(format string, v ...interface{}) {
|
||||||
|
l.Logger.Printf("INFO: "+format, v...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l StdLogger) Debugf(format string, v ...interface{}) {
|
||||||
|
l.Logger.Printf("DEBUG: "+format, v...)
|
||||||
|
}
|
||||||
|
|
||||||
|
type NoopLogger struct {
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l NoopLogger) Fatalf(format string, v ...interface{}) {
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l NoopLogger) Errorf(format string, v ...interface{}) {
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l NoopLogger) Warnf(format string, v ...interface{}) {
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l NoopLogger) Infof(format string, v ...interface{}) {
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l NoopLogger) Debugf(format string, v ...interface{}) {
|
||||||
|
}
|
18
ping.go
18
ping.go
@ -57,6 +57,7 @@ import (
|
|||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log"
|
||||||
"math"
|
"math"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"net"
|
"net"
|
||||||
@ -102,6 +103,7 @@ func New(addr string) *Pinger {
|
|||||||
network: "ip",
|
network: "ip",
|
||||||
protocol: "udp",
|
protocol: "udp",
|
||||||
awaitingSequences: map[int]struct{}{},
|
awaitingSequences: map[int]struct{}{},
|
||||||
|
logger: StdLogger{Logger: log.New(log.Writer(), log.Prefix(), log.Flags())},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -192,6 +194,8 @@ type Pinger struct {
|
|||||||
network string
|
network string
|
||||||
// protocol is "icmp" or "udp".
|
// protocol is "icmp" or "udp".
|
||||||
protocol string
|
protocol string
|
||||||
|
|
||||||
|
logger Logger
|
||||||
}
|
}
|
||||||
|
|
||||||
type packet struct {
|
type packet struct {
|
||||||
@ -367,10 +371,20 @@ func (p *Pinger) Privileged() bool {
|
|||||||
return p.protocol == "icmp"
|
return p.protocol == "icmp"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SetLogger sets the logger to be used to log events from the pinger.
|
||||||
|
func (p *Pinger) SetLogger(logger Logger) {
|
||||||
|
p.logger = logger
|
||||||
|
}
|
||||||
|
|
||||||
// Run runs the pinger. This is a blocking function that will exit when it's
|
// Run runs the pinger. This is a blocking function that will exit when it's
|
||||||
// done. If Count or Interval are not specified, it will run continuously until
|
// done. If Count or Interval are not specified, it will run continuously until
|
||||||
// it is interrupted.
|
// it is interrupted.
|
||||||
func (p *Pinger) Run() error {
|
func (p *Pinger) Run() error {
|
||||||
|
logger := p.logger
|
||||||
|
if logger == nil {
|
||||||
|
logger = NoopLogger{}
|
||||||
|
}
|
||||||
|
|
||||||
var conn *icmp.PacketConn
|
var conn *icmp.PacketConn
|
||||||
var err error
|
var err error
|
||||||
if p.ipaddr == nil {
|
if p.ipaddr == nil {
|
||||||
@ -432,7 +446,7 @@ func (p *Pinger) Run() error {
|
|||||||
err := p.processPacket(r)
|
err := p.processPacket(r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// FIXME: this logs as FATAL but continues
|
// FIXME: this logs as FATAL but continues
|
||||||
fmt.Println("FATAL:", err)
|
logger.Fatalf("processing received packet: %s", err)
|
||||||
}
|
}
|
||||||
case <-interval.C:
|
case <-interval.C:
|
||||||
if p.Count > 0 && p.PacketsSent >= p.Count {
|
if p.Count > 0 && p.PacketsSent >= p.Count {
|
||||||
@ -442,7 +456,7 @@ func (p *Pinger) Run() error {
|
|||||||
err = p.sendICMP(conn)
|
err = p.sendICMP(conn)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// FIXME: this logs as FATAL but continues
|
// FIXME: this logs as FATAL but continues
|
||||||
fmt.Println("FATAL:", err)
|
logger.Fatalf("sending packet: %s", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if p.Count > 0 && p.PacketsRecv >= p.Count {
|
if p.Count > 0 && p.PacketsRecv >= p.Count {
|
||||||
|
Loading…
Reference in New Issue
Block a user