No description
- Go 100%
|
|
||
|---|---|---|
| .forgejo/workflows | ||
| debian | ||
| example_test.go | ||
| go.mod | ||
| go.sum | ||
| LICENSE.txt | ||
| README.md | ||
| slog.go | ||
| transport.go | ||
go-slog-syslog
go-slog-syslog is a Go library that provides a slog.Handler-compatible syslog handler. This library is capable of transmiting logs locally or over the network, using TCP (optionally with TLS), UDP, or Unix datagram sockets. This library adhers to the syslog protocol described in RFC 5424.
Install
go get codeberg.org/git-pages/go-slog-syslog
Example
package main
import (
"bufio"
"errors"
"fmt"
"io"
"log/slog"
"net"
"net/netip"
"strconv"
"strings"
"time"
syslog "codeberg.org/git-pages/go-slog-syslog"
)
var currentTime = time.Unix(2524608000, 0)
func main() {
address, done := listen()
strptr := func(s string) *string { return &s }
handler, err := syslog.NewHandler(&syslog.HandlerOptions{
Address: address,
Hostname: strptr("miyuko"),
AppName: "catware",
ProcID: strptr("1"),
StructuredDataID: "catware@65535",
})
if err != nil {
panic(err)
}
l := slog.New(handler).WithGroup("cat").With("name", nil)
l = l.With(slog.Group("emotion",
"mood", "inquisitive",
"approachable", true,
))
l = l.With(slog.Group("social",
"accepts-pets", true,
))
l = l.WithGroup("wake")
l.Debug(
"cat waking up",
"waketime", currentTime,
"reason", "external-disturbance",
)
handler.Flush(1 * time.Second)
handler.Close()
<-done
}
func listen() (address string, done chan struct{}) {
// implementation omitted
}
Output consisting of one syslog message on a single line (broken up into multiple lines here to improve readability):
<7>1 2050-01-01T00:00:00Z miyuko catware 1 -
[catware@65535
cat.emotion.approachable="true"
cat.emotion.mood="inquisitive"
cat.name="<nil>"
cat.social.accepts-pets="true"
cat.wake.reason="external-disturbance"
cat.wake.waketime="2050-01-01 00:00:00 +0000 UTC"]
cat waking up