# go-slog-syslog

[![Go Reference](https://pkg.go.dev/badge/codeberg.org/git-pages/go-slog-syslog.svg)](https://pkg.go.dev/codeberg.org/git-pages/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].

[slog.Handler]: https://pkg.go.dev/log/slog#Handler
[RFC 5424]: https://rfc-editor.org/rfc/rfc5424.html

## Install

```
go get codeberg.org/git-pages/go-slog-syslog
```

## Example

```go
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](LICENSE.txt)
