https://github.com/go-slog-handler/slog-handler
Pretty handler for structured Logging with slog
https://github.com/go-slog-handler/slog-handler
golang logger logging slog
Last synced: 5 months ago
JSON representation
Pretty handler for structured Logging with slog
- Host: GitHub
- URL: https://github.com/go-slog-handler/slog-handler
- Owner: go-slog-handler
- License: bsd-3-clause
- Created: 2024-01-14T02:06:46.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2025-11-30T14:22:32.000Z (7 months ago)
- Last Synced: 2025-12-02T21:04:20.634Z (7 months ago)
- Topics: golang, logger, logging, slog
- Language: Go
- Homepage:
- Size: 302 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: docs/README.md
- License: LICENSE
Awesome Lists containing this project
README
# Pretty handler for structured Logging with slog
The handler is ready to use:
```go
package main
import (
"log/slog"
logger "gopkg.in/slog-handler.v1"
)
func main() {
slog.Info("test", "example 1", map[int]string{
0: "test",
})
slog.Info("example of raw output", "raw;example 2", map[int]string{
0: "test",
})
slog.Error("example of raw output", "raw;example 3", map[int]string{
0: "test",
})
slog.Warn("test 2", "example 4", map[int]string{
0: "test",
})
}
func init() {
logger.SetLogger(
logger.Options{
AddSource: true,
Format: "text",
Level: "debug",
Pretty: true,
},
)
}
```
Output:

## NullHandler
The `NullHandler` is a special handler that discards all log records. It's useful for:
- Testing where you don't want log output
- Disabling logging in production without code changes
- Benchmarking code without logging overhead
### Usage
```go
package main
import (
"log/slog"
logger "gopkg.in/slog-handler.v1"
)
func main() {
// Create logger with NullHandler
log := logger.NewLogger(logger.Options{
Null: true, // Enable NullHandler
})
// These logs will be discarded
log.Info("this will not be logged")
log.Error("this will also be discarded")
log.With("key", "value").Debug("nothing here")
}
```
You can also set it as the global logger:
```go
func init() {
logger.SetGlobalLogger(logger.Options{
Null: true, // All slog calls will be discarded
})
}
```