Skip to content

Commit

Permalink
more restructuring
Browse files Browse the repository at this point in the history
  • Loading branch information
arianvp committed Jul 12, 2024
1 parent fd20db1 commit 8833926
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 11 deletions.
13 changes: 7 additions & 6 deletions journal.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import (
"strconv"
)

type Priority int
type priority int

const (
priEmerg Priority = iota
priEmerg priority = iota
priAlert
priCrit
priErr
Expand All @@ -30,7 +30,7 @@ const (
LevelEmergency slog.Level = slog.LevelError + 3
)

func levelToPriority(l slog.Level) Priority {
func levelToPriority(l slog.Level) priority {
switch l {
case slog.LevelDebug:
return priDebug
Expand All @@ -46,14 +46,15 @@ func levelToPriority(l slog.Level) Priority {
return priCrit
case LevelAlert:
return priAlert
case LevelEmergency:
return priEmerg
default:
panic("unreachable")
panic("invalid log level")
}
}

type Options struct {
Level slog.Leveler
Addr string // Address of the journal socket. If not set defaults to /run/systemd/journal/socket. This is useful for testing.
}

type Handler struct {
Expand All @@ -76,7 +77,7 @@ func NewHandler(opts *Options) (*Handler, error) {
h.opts.Level = slog.LevelInfo
}

w, err := newJournalWriter(h.opts.Addr)
w, err := newJournalWriter()
if err != nil {
return nil, err
}
Expand Down
61 changes: 61 additions & 0 deletions journal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"log/slog"
"net"
"os"
"strconv"
"strings"
"syscall"
"testing"
Expand Down Expand Up @@ -258,5 +259,65 @@ func TestCanWriteMessageToSocket(t *testing.T) {
}

})
}

func TestNewlineBehaviour(t *testing.T) {

buf := new(bytes.Buffer)
handler, err := NewHandler(nil)
handler.w = buf
if err != nil {
t.Fatal("Error creating new handler")
}
multiline := "Hello, World!\nI am a multline\nstring"
record := slog.NewRecord(time.Now(), slog.LevelInfo, multiline, 0)
record.AddAttrs(slog.Attr{Key: "key", Value: slog.StringValue("value")})

if err := handler.Handle(context.TODO(), record); err != nil {
t.Fatal(err)
}

kv, err := deserializeKeyValue(buf)
if err != nil {
t.Fatal(err)
}
if kv["MESSAGE"] != multiline {
t.Error("Unexpected message")
}

}

func TestLogLevels(t *testing.T) {
buf := new(bytes.Buffer)
handler, err := NewHandler(nil)
handler.w = buf
if err != nil {
t.Fatal("Error creating new handler")
}

priorities := map[slog.Level]priority{
slog.LevelDebug: priDebug,
slog.LevelInfo: priInfo,
LevelNotice: priNotice,
slog.LevelWarn: priWarning,
slog.LevelError: priErr,
LevelCritical: priCrit,
LevelAlert: priAlert,
LevelEmergency: priEmerg,
}

for level, priority := range priorities {
if err := handler.Handle(context.TODO(), slog.Record{Level: level, Message: "Hello, World!"}); err != nil {
t.Error(err)
}
kv, err := deserializeKeyValue(buf)
if err != nil {
t.Fatal(err)
}
if kv["PRIORITY"] != strconv.Itoa(int(priority)) {
t.Error("Unexpected priority", kv, priority, int(level))
}
buf.Reset()
}

}
7 changes: 3 additions & 4 deletions journal_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@ type journalWriter struct {
conn *net.UnixConn
}

func newJournalWriter(path string) (*journalWriter, error) {
if path == "" {
path = "/run/systemd/journal/socket"
}
const path = "/run/systemd/journal/socket"

func newJournalWriter() (*journalWriter, error) {
// The "net" library in Go really wants me to either Dial or Listen a UnixConn,
// which would respectively bind() an address or connect() to a remote address,
// but we want neither. We want to create a datagram socket and write to it directly
Expand Down
2 changes: 1 addition & 1 deletion journal_writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
)

func TestJournalWriter(t *testing.T) {
_, err := newJournalWriter("")
_, err := newJournalWriter()
if err != nil {
t.Fatal(err)
}
Expand Down

0 comments on commit 8833926

Please sign in to comment.