1
0
Fork 0
No description
Find a file
2026-03-13 05:58:45 +01:00
.forgejo/workflows Adding upstream version 0.0~git20251207.892f654. 2026-03-13 05:58:03 +01:00
debian Releasing fastforward version 0.0~git20251207.892f654-2~ffwd13+u1. 2026-03-13 05:58:45 +01:00
example_test.go Adding upstream version 0.0~git20251207.892f654. 2026-03-13 05:58:03 +01:00
go.mod Adding upstream version 0.0~git20251207.892f654. 2026-03-13 05:58:03 +01:00
go.sum Adding upstream version 0.0~git20251207.892f654. 2026-03-13 05:58:03 +01:00
LICENSE.txt Adding upstream version 0.0~git20251207.892f654. 2026-03-13 05:58:03 +01:00
README.md Adding upstream version 0.0~git20251207.892f654. 2026-03-13 05:58:03 +01:00
slog.go Adding upstream version 0.0~git20251207.892f654. 2026-03-13 05:58:03 +01:00
transport.go Adding upstream version 0.0~git20251207.892f654. 2026-03-13 05:58:03 +01:00

go-slog-syslog

Go Reference

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

License

0-clause BSD