Skip to content

Commit

Permalink
Updating migration guide
Browse files Browse the repository at this point in the history
  • Loading branch information
Red-GV committed Apr 12, 2022
1 parent e3a30f2 commit b919a4b
Showing 1 changed file with 42 additions and 25 deletions.
67 changes: 42 additions & 25 deletions MIGRATION.md
Original file line number Diff line number Diff line change
@@ -1,37 +1,54 @@
# 2.0.0

As of `logr@v1.0.0`, the `logr.Logger` is considered to be a defined `struct` instead of an `interface`. The implementation layer (now referred to as `logr.LogSink`) has been entirely restructured. Now, the `logerr` library will provide `logr.Logger` objects and ways to affect the underlying `Sink` operations.
## Removal of singleton behavior

- Instead of `log.V()`, `log.Info()`, `log.Error()` methods, use `log.DefaultLogger().V()`, `log.DefaultLogger().Info()`, `log.DefaultLogger().Error()`
Due to the structural changes of `logr@v1`, several features of this library have been changed to better reflect the structure of the `logr` library. Most notable of these changes include the lack of a singleton logger. It is recommended that developers create an instance a logger with `NewLogger` or `NewLoggerWithOptions` and either keep it as their own singleton or pass the instance throughout the application to use where needed. Methods like `Info`, `Error`, and `V` are still callable with a `logr.Logger` instance.

- Instead of `log.SetOutput()`, `log.SetLogLevel()`, use either of the following methods:
ex:

Method 1:
```go
l := log.DefaultLogger()
s, err := log.GetSink(l)
```golang
import (
"github.com/ViaQ/logerr/v2/log"
)

if err != nil {
// Some action
}
var logger := log.NewLogger("")

s.SetVerbosity(1)
s.SetOutput(ioutil.Discard)

l.Info("hello world")
logger.Info("Now logging info message")
logger.Error(errors.New("New error"), "Now logging new error")
logger.V(1).Info("And logging a second info message")
```

Method 2:
```go
l := log.DefaultLogger()
// This method panics, but DefaultLogger will not
// panic because it uses Sink.
log.MustGetSink(l).SetVerbosity(1)
log.MustGetSink(l).SetOutput(ioutil.Discard)
## Removal of explicit `SetOutput` and `SetLevel`

l.Info("hello world")
```
As a byproduct of the changes in `logr@v1`, these methods have been moved into the internal logging implementation of `logr.Sink`: `Sink`. Changing the internal implementation is considered to be an "advanced" setting. There are two ways to set a logger with a different writer or log level:

```golang
// Method 1

import (
"bytes"
"github.com/ViaQ/logerr/v2/log"
)

buf := bytes.NewBuffer(nil)
logger := log.NewLogger("", &log.Options{Writer: buf, LogLevel: 1})

- Instead of `log.UseLogger`, `log.GetLogger`, keep the logger instance until it is no longer needed
logger.Info("I will log at level 1")

- Instead of `log.Init` or `log.InitWithOptions`, use `log.NewLogger` or `log.NewLoggerWithOption`
// Method 2

import (
"bytes"
"github.com/ViaQ/logerr/v2/log"
"github.com/ViaQ/logerr/v2/internal/sink"
)

buf := bytes.NewBuffer(nil)
logger := log.NewLogger("")

sink := logger.GetSink().(*sink.Sink)
sink.SetOutput(buf)
sink.SetVerbosity(1)

logger.Info("I will log at level 1")
```

0 comments on commit b919a4b

Please sign in to comment.