https://github.com/dcarbone/zadapters
Some simple adapters for Zerolog so it can be used as a *log.Logger or in packages that define their own Logger
https://github.com/dcarbone/zadapters
couchbase generic go golang logger zerolog
Last synced: 5 months ago
JSON representation
Some simple adapters for Zerolog so it can be used as a *log.Logger or in packages that define their own Logger
- Host: GitHub
- URL: https://github.com/dcarbone/zadapters
- Owner: dcarbone
- License: mit
- Created: 2018-12-31T15:54:04.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2024-12-19T04:21:50.000Z (5 months ago)
- Last Synced: 2024-12-19T04:35:27.932Z (5 months ago)
- Topics: couchbase, generic, go, golang, logger, zerolog
- Language: Go
- Homepage:
- Size: 28.3 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# zadapters
Some simple adapters for Zerolog so it can be used with other packages that might define their own Logger interface## Couchbase
```go
package mainimport(
"os"
"github.com/couchbase/gocbcore"
"github.com/dcarbone/zadapters/zgocbcore"
"github.com/rs/zerolog"
)func main() {
// init a logger to use
log := zerolog.New(os.Stdout).With().
Timestamp().
Fields(map[string]interface{}{"source": "couchbase"}).
Logger()
// create the adapter.
adapter := zgocbcore.NewDefault(log)
// set logger
gocbcore.SetLogger(adapter)
// presumably do something
}
```### LevelMap
The [LevelMap](zgocbcore/adapter.go#L10) maps a `gocb.LogLevel` to a `zerolog.Level`. If you wish a given
Couchbase log level to be ignored, you may either simply not set it or set it to `zerolog.Disabled`.There is a [DefaultLevelMap](zgocbcore/adapter.go#L22) that will be used if `nil` is passed as first arg to `New()` or
if the [Adapter](zgocbcore/adapter.go#L13) is constructed using [NewDefault()](zgocbcore/adapter.go#L46)And that's pretty much it.
## Generic
Also included is a generic [Adapter](zstdlog/adapter.go) that allows a ZeroLog logger to be used with
packages expecting a `*log.Logger` type. This will also satisfy those packages who create their own `Logger` interface
type with the standard `Print`, `Println`, and `Printf` methods on them.```go
package mainimport(
"os""github.com/dcarbone/zadapters/zstdlog"
"github.com/rs/zerolog"
)func main() {
// init a logger to use
log := zerolog.New(os.Stdout)// creates a *log.Logger with no level prefix
stdLogger := zstdlog.NewStdLogger(log)
stdLogger.Println("hello world")
// results in {"message":"hello world"}// creates a *log.Logger with a level prefix
stdLeveledLogger := zstdlog.NewStdLoggerWithLevel(log, zerolog.InfoLevel)
stdLeveledLogger.Println("hello world")
// results in {"level":"info","message":"hello world"}
}
```