Skip to content

Commit 3e0f2b0

Browse files
authored
Merge pull request #9 from systemd/fix-timestamps
Fix timestamps
2 parents 57f96c9 + 5d32658 commit 3e0f2b0

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,17 @@
11
# slog: Systemd journal handler
22

3+
## Usage
4+
5+
> [!NOTE]
6+
> Journald only supports keys of the form `^[A-Z_][A-Z0-9_]*$`. Any other keys will be silently dropped.
7+
8+
```go
9+
h := slogjournal.NewHandler(nil)
10+
log := slog.New(h)
11+
log.Info("Hello, world!", "EXTRA_KEY", "5")
12+
log.Info("Hello, world!", slog.Group("HTTP", "METHOD", "put", "URL", "http://example.com"))
13+
```
14+
15+
316
> [!CAUTION]
417
> This is pre-release software. No releases have been tagged yet.

journal.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -198,8 +198,8 @@ func (h *Handler) appendAttr(b []byte, prefix string, a slog.Attr) []byte {
198198
if a.Equal(slog.Attr{}) {
199199
return b
200200
}
201-
202-
if a.Value.Kind() == slog.KindGroup {
201+
switch a.Value.Kind() {
202+
case slog.KindGroup:
203203
attrs := a.Value.Group()
204204
// If a group has no Attrs (even if it has a non-empty key), ignore it.
205205
if len(attrs) == 0 {
@@ -216,9 +216,14 @@ func (h *Handler) appendAttr(b []byte, prefix string, a slog.Attr) []byte {
216216
for _, a := range attrs {
217217
b = h.appendAttr(b, prefix, a)
218218
}
219-
} else {
219+
case slog.KindDuration:
220+
b = h.appendKV(b, prefix+a.Key, []byte(strconv.FormatInt(a.Value.Duration().Microseconds(), 10)))
221+
case slog.KindTime:
222+
b = h.appendKV(b, prefix+a.Key, []byte(strconv.FormatInt(a.Value.Time().UnixMicro(), 10)))
223+
default:
220224
b = h.appendKV(b, prefix+a.Key, []byte(a.Value.String()))
221225
}
226+
222227
return b
223228
}
224229

0 commit comments

Comments
 (0)