Skip to content

Commit edf016b

Browse files
committed
add devlog/log example to readme
1 parent 40de445 commit edf016b

1 file changed

Lines changed: 38 additions & 10 deletions

File tree

README.md

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,39 +11,39 @@ Run `go get hermannm.dev/devlog` to add it to your project!
1111
output for `slog`'s logging functions. It can be configured as follows:
1212

1313
```go
14-
logger := slog.New(devlog.NewHandler(os.Stdout, nil))
15-
slog.SetDefault(logger)
14+
logHandler := devlog.NewHandler(os.Stdout, nil)
15+
slog.SetDefault(slog.New(logHandler))
1616
```
1717

1818
Logging with `slog` will now use this handler:
1919

2020
```go
21-
slog.Warn("No value found for 'PORT' in env, defaulting to 8000")
22-
slog.Info("Server started", slog.Int("port", 8000), slog.String("environment", "DEV"))
21+
slog.Warn("no value found for 'PORT' in env, defaulting to 8000")
22+
slog.Info("server started", slog.Int("port", 8000), slog.String("environment", "DEV"))
2323
slog.Error(
24-
"Database query failed",
24+
"database query failed",
2525
slog.Group("dbError", slog.Int("code", 60), slog.String("message", "UNKNOWN_TABLE")),
2626
)
2727
```
2828

2929
...giving the following output (using a gruvbox terminal color scheme):
3030

31-
![Screenshot of 3 log messages in a terminal](https://github.com/hermannm/devlog/blob/assets/devlog-example-output.png?raw=true)
31+
![Screenshot of log messages in a terminal](https://github.com/hermannm/devlog/blob/ac14f0dc1823316c983fb9cef6f1cf73a0bbb923/devlog-example-output.png?raw=true)
3232

3333
This output is meant to be easily read by a developer working locally. However, you may want a more
3434
structured format for production, such as JSON, to make log analysis easier. You can get both by
3535
conditionally choosing the log handler for your application, e.g.:
3636

3737
```go
38-
var handler slog.Handler
38+
var logHandler slog.Handler
3939
switch os.Getenv("ENVIRONMENT") {
4040
case "PROD":
41-
handler = slog.NewJSONHandler(os.Stdout, nil)
41+
logHandler = slog.NewJSONHandler(os.Stdout, nil)
4242
case "DEV":
43-
handler = devlog.NewHandler(os.Stdout, nil)
43+
logHandler = devlog.NewHandler(os.Stdout, nil)
4444
}
4545

46-
slog.SetDefault(slog.New(handler))
46+
slog.SetDefault(slog.New(logHandler))
4747
```
4848

4949
## devlog/log
@@ -52,6 +52,34 @@ To complement `devlog`'s output handling, the
5252
[`devlog/log`](https://pkg.go.dev/hermannm.dev/devlog/log) subpackage provides input handling. It is
5353
a thin wrapper over the `slog` package, with utility functions for log message formatting.
5454

55+
Example using `devlog` and `devlog/log` together:
56+
57+
```go
58+
import (
59+
"errors"
60+
"log/slog"
61+
"os"
62+
63+
"hermannm.dev/devlog"
64+
"hermannm.dev/devlog/log"
65+
)
66+
67+
func main() {
68+
logHandler := devlog.NewHandler(os.Stdout, &devlog.Options{Level: slog.LevelDebug})
69+
slog.SetDefault(slog.New(logHandler))
70+
71+
err := errors.New("invalid username")
72+
log.ErrorCause(err, "failed to create user") // Uses slog.Default()
73+
74+
userJSON := map[string]any{"id": 1, "username": "hermannm"}
75+
log.DebugJSON(userJSON, "user")
76+
}
77+
```
78+
79+
This gives the following output:
80+
81+
![Screenshot of log messages in a terminal](https://github.com/hermannm/devlog/blob/ac14f0dc1823316c983fb9cef6f1cf73a0bbb923/devlog-example-output-2.png?raw=true)
82+
5583
## Credits
5684

5785
- [Jonathan Amsterdam](https://github.com/jba) for his fantastic

0 commit comments

Comments
 (0)