Skip to content

Commit 8b7d855

Browse files
committed
fix: add logf reference
1 parent 10d412f commit 8b7d855

File tree

1 file changed

+46
-1
lines changed

1 file changed

+46
-1
lines changed

README.md

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,52 @@ Install the package with:
1818
go get github.com/ssgreg/journald
1919
```
2020

21-
## Usage
21+
## Usage: The Best Way
22+
23+
The Best Way to use structured logs (systemd Journal, etc.) is [logf](https://github.com/ssgreg/logf) - the fast, asynchronous, structured logger in Go with zero allocation count and it's journald driver [logfjournald](https://github.com/ssgreg/logfjournald). This driver uses `journald` package.
24+
The following example creates the new `logf` logger with `logfjournald` appender.
25+
26+
```go
27+
package main
28+
29+
import (
30+
"runtime"
31+
32+
"github.com/ssgreg/logf"
33+
"github.com/ssgreg/logfjournald"
34+
)
35+
36+
func main() {
37+
// Create journald Appender with default journald Encoder.
38+
appender, appenderClose := logfjournald.NewAppender(logfjournald.NewEncoder.Default())
39+
defer appenderClose()
40+
41+
// Create ChannelWriter with journald Encoder.
42+
writer, writerClose := logf.NewChannelWriter(logf.ChannelWriterConfig{
43+
Appender: appender,
44+
})
45+
defer writerClose()
46+
47+
// Create Logger with ChannelWriter.
48+
logger := logf.NewLogger(logf.LevelInfo, writer)
49+
50+
logger.Info("got cpu info", logf.Int("count", runtime.NumCPU()))
51+
}
52+
```
53+
54+
The JSON representation of the journal entry this generates:
55+
56+
```json
57+
{
58+
"TS": "2018-11-01T07:25:18Z",
59+
"PRIORITY": "6",
60+
"LEVEL": "info",
61+
"MESSAGE": "got cpu info",
62+
"COUNT": "4",
63+
}
64+
```
65+
66+
## Usage: AS-IS
2267

2368
Let's look at what the `journald` provides as Go APIs for logging:
2469

0 commit comments

Comments
 (0)