|
1 | 1 | ## Logger interface with implementations (Zap and Logrus) |
2 | 2 |
|
| 3 | +[](https://travis-ci.com/arun0009/go-logger) |
| 4 | + |
3 | 5 | When we create go libraries in general we shouldn't be logging but at times we do have to log, debug what the |
4 | 6 | library is doing or trace the log. |
5 | 7 |
|
6 | 8 | We cannot implement a library with one log library and expect applications to use the same log library. We use two |
7 | 9 | of the popular log libraries [logrus](https://github.com/sirupsen/logrus) and [zap](https://github.com/uber-go/zap) |
8 | | -and this `go-logger` library allows you to use either by using an interface. |
| 10 | +and this `go-logger` library allows you to use either one by using an interface. |
| 11 | + |
| 12 | +You can add your implementation if you want to add more log libraries (e.g. zerolog). |
| 13 | + |
| 14 | +## Installation |
| 15 | + |
| 16 | +go get -u github.com/arun0009/go-logger |
| 17 | + |
| 18 | +## Quick Start |
| 19 | + |
| 20 | +[logrus](https://github.com/sirupsen/logrus) example |
| 21 | + |
| 22 | +```go |
| 23 | +package main |
| 24 | + |
| 25 | +import ( |
| 26 | + "os" |
| 27 | + |
| 28 | + "github.com/arun0009/go-logger/pkg/logger" |
| 29 | + "github.com/sirupsen/logrus" |
| 30 | +) |
| 31 | + |
| 32 | +func main() { |
| 33 | + logrusLog := logrus.New() |
| 34 | + logrusLog.SetFormatter(&logrus.JSONFormatter{}) |
| 35 | + logrusLog.SetOutput(os.Stdout) |
| 36 | + logrusLog.SetLevel(logrus.DebugLevel) |
| 37 | + log, _ := logger.NewLogrusLogger(logrusLog) |
| 38 | + log.WithFields(logger.Fields{ |
| 39 | + "foo": "bar", |
| 40 | + }).Info("direct") |
| 41 | +} |
| 42 | +``` |
| 43 | + |
| 44 | +[zap](https://github.com/uber-go/zap) example |
| 45 | + |
| 46 | +```go |
| 47 | +package main |
| 48 | + |
| 49 | +import ( |
| 50 | + "os" |
| 51 | + |
| 52 | + "github.com/arun0009/go-logger/pkg/logger" |
| 53 | + "go.uber.org/zap" |
| 54 | + "go.uber.org/zap/zapcore" |
| 55 | +) |
| 56 | + |
| 57 | +func main() { |
| 58 | + consoleEncoder := zapcore.NewJSONEncoder(zap.NewDevelopmentEncoderConfig()) |
| 59 | + core := zapcore.NewCore(consoleEncoder, |
| 60 | + zapcore.Lock(zapcore.AddSync(os.Stderr)), |
| 61 | + zapcore.DebugLevel) |
| 62 | + zapLogger := zap.New(core) |
| 63 | + log, _ := logger.NewZapLogger(zapLogger) |
| 64 | + log.WithFields(logger.Fields{ |
| 65 | + "foo": "bar", |
| 66 | + }).Info("direct") |
| 67 | +} |
9 | 68 |
|
10 | | -You can add your implementation if you want to add more log libraries (e.g. zerolog). |
| 69 | +``` |
0 commit comments