wgevents
is a logging package for wireguard-go
. It transforms format strings and args passed to logger methods into concrete events.
- Event-based Logging: Converts format strings and arguments into concrete logging events.
- Structured Logging Support: logs format arguments separate slog records. Integrates with
slog.Logger
. - Wireguard-go Version Pinning: The package is designed as a separate module to pin to a specific version of
wireguard-go
. This ensures compatibility and stability even whenwireguard-go
messages change.
To use wgevents in your Go project, install it using go get
:
go get github.com/point-c/wgevents
The Events
function creates a new device.Logger
capable of recognizing names and types of arguments passed to logging methods.
func Events(fn func(Event)) *device.Logger
fn func(Event)
: A callback function that is called for each event logged.
*device.Logger
: A logger compatible withwireguard-go
.
Each logged event is handled through the provided callback function.
The package handles known events with specific types, and unknown events are categorized as EventAny
.
logger := wgevents.Events(func(e wgevents.Event) {
// Handle logging event
})
// Use logger in your application
A type switch can be used to recognize specific events:
switch ev := ev.(type) {
case *wgevents.EventAssumingDefaultMTU:
case *wgevents.EventBindCloseFailed:
case *wgevents.EventBindUpdated:
// And so on...
}
logger := wgevents.Events(func(e wgevents.Event) {
e.Slog(slog.Default())
})
This package allows for updates and additions of events. Modify internal/events-generate/events.yml
and regenerate events_generated.go
to update event handling.
Each section under the events
key corresponds to a specific event that can be logged:
<event name>:
type: string
level: string
nice: string (optional)
format: string
args: list (optional)
custom: bool (optional)
-
type
: The type of logging or messaging method to be used. Onlyerrorf
for error logging, andverbosef
for verbose logging are valid. -
level
: The level at which the logging should occur. Valid values aredebug
,info
,warn
, anderror
. -
nice
: An optional human-readable message for the event. If not specifiedformat
will be used. -
format
: The format string used by thewireguard-go
library. -
args
: The arguments used in the format string in the following format:- <name>: <type> - <name>: <type>
-
custom
: An optional flag indicating that parsing this event requires custom logic to be implemented.
The final section of the configuration file lists the external packages used as part of the event processing mechanism. These are mentioned under the imports
key.
The package includes tests that demonstrate its functionality. Use Go's testing tools to run the tests:
go test ./...
To regenerate godocs:
go generate -tags docs ./...
To regenerate generated packages:
go generate ./...