-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Arun Gopalpuri
committed
Dec 6, 2020
1 parent
efbb37a
commit 76d5270
Showing
1 changed file
with
61 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,69 @@ | ||
## Logger interface with implementations (Zap and Logrus) | ||
|
||
[![Build Status](https://api.travis-ci.com/arun0009/go-logger.svg?branch=master)](https://travis-ci.com/arun0009/go-logger) | ||
|
||
When we create go libraries in general we shouldn't be logging but at times we do have to log, debug what the | ||
library is doing or trace the log. | ||
|
||
We cannot implement a library with one log library and expect applications to use the same log library. We use two | ||
of the popular log libraries [logrus](https://github.com/sirupsen/logrus) and [zap](https://github.com/uber-go/zap) | ||
and this `go-logger` library allows you to use either by using an interface. | ||
and this `go-logger` library allows you to use either one by using an interface. | ||
|
||
You can add your implementation if you want to add more log libraries (e.g. zerolog). | ||
|
||
## Installation | ||
|
||
go get -u github.com/arun0009/go-logger | ||
|
||
## Quick Start | ||
|
||
[logrus](https://github.com/sirupsen/logrus) example | ||
|
||
```go | ||
package main | ||
|
||
import ( | ||
"os" | ||
|
||
"github.com/arun0009/go-logger/pkg/logger" | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
func main() { | ||
logrusLog := logrus.New() | ||
logrusLog.SetFormatter(&logrus.JSONFormatter{}) | ||
logrusLog.SetOutput(os.Stdout) | ||
logrusLog.SetLevel(logrus.DebugLevel) | ||
log, _ := logger.NewLogrusLogger(logrusLog) | ||
log.WithFields(logger.Fields{ | ||
"foo": "bar", | ||
}).Info("direct") | ||
} | ||
``` | ||
|
||
[zap](https://github.com/uber-go/zap) example | ||
|
||
```go | ||
package main | ||
|
||
import ( | ||
"os" | ||
|
||
"github.com/arun0009/go-logger/pkg/logger" | ||
"go.uber.org/zap" | ||
"go.uber.org/zap/zapcore" | ||
) | ||
|
||
func main() { | ||
consoleEncoder := zapcore.NewJSONEncoder(zap.NewDevelopmentEncoderConfig()) | ||
core := zapcore.NewCore(consoleEncoder, | ||
zapcore.Lock(zapcore.AddSync(os.Stderr)), | ||
zapcore.DebugLevel) | ||
zapLogger := zap.New(core) | ||
log, _ := logger.NewZapLogger(zapLogger) | ||
log.WithFields(logger.Fields{ | ||
"foo": "bar", | ||
}).Info("direct") | ||
} | ||
|
||
You can add your implementation if you want to add more log libraries (e.g. zerolog). | ||
``` |