Skip to content

Commit

Permalink
Reverting to v1.1.1; Removing vendor directory; Reapplying ci changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Red-GV committed Apr 14, 2022
1 parent f1848d2 commit c53ff49
Show file tree
Hide file tree
Showing 16 changed files with 1,208 additions and 370 deletions.
6 changes: 3 additions & 3 deletions .bingo/Variables.mk
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ GO ?= $(shell which go)
# @echo "Running golangci-lint"
# @$(GOLANGCI_LINT) <flags/args..>
#
GOLANGCI_LINT := $(GOBIN)/golangci-lint-v1.32.2
GOLANGCI_LINT := $(GOBIN)/golangci-lint-v1.45.2
$(GOLANGCI_LINT): $(BINGO_DIR)/golangci-lint.mod
@# Install binary/ries using Go 1.14+ build command. This is using bwplotka/bingo-controlled, separate go module with pinned dependencies.
@echo "(re)installing $(GOBIN)/golangci-lint-v1.32.2"
@cd $(BINGO_DIR) && $(GO) build -mod=mod -modfile=golangci-lint.mod -o=$(GOBIN)/golangci-lint-v1.32.2 "github.com/golangci/golangci-lint/cmd/golangci-lint"
@echo "(re)installing $(GOBIN)/golangci-lint-v1.45.2"
@cd $(BINGO_DIR) && $(GO) build -mod=mod -modfile=golangci-lint.mod -o=$(GOBIN)/golangci-lint-v1.45.2 "github.com/golangci/golangci-lint/cmd/golangci-lint"

4 changes: 2 additions & 2 deletions .bingo/golangci-lint.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module _ // Auto generated by https://github.com/bwplotka/bingo. DO NOT EDIT

go 1.15
go 1.17

require github.com/golangci/golangci-lint v1.32.2 // cmd/golangci-lint
require github.com/golangci/golangci-lint v1.45.2 // cmd/golangci-lint
959 changes: 959 additions & 0 deletions .bingo/golangci-lint.sum

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion .bingo/variables.env
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ if [ -z "$GOBIN" ]; then
fi


GOLANGCI_LINT="${GOBIN}/golangci-lint-v1.32.2"
GOLANGCI_LINT="${GOBIN}/golangci-lint-v1.45.2"

2 changes: 1 addition & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:
- name: Lint
uses: golangci/golangci-lint-action@v2
with:
version: v1.45.2
version: v1.45

- name: Test
run: go test -v -coverprofile=.coverprofile ./...
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# Main

[22](https://github.com/ViaQ/logerr/pull/22) **xperimental**: Updated `DefaultLogger` to work as a singleton
[21](https://github.com/ViaQ/logerr/pull/21) **xperimental**: Updated `bingo` dependencies
[20](https://github.com/ViaQ/logerr/pull/20) **Red-GV**: Updated `logr` package to 1.2.2; Refactored logerr
2 changes: 1 addition & 1 deletion MIGRATION.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# 1.1.0
# 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.

Expand Down
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ module github.com/ViaQ/logerr
go 1.17

require (
github.com/go-logr/logr v0.4.0
github.com/stretchr/testify v1.4.0
github.com/go-logr/logr v1.2.3
github.com/stretchr/testify v1.7.1
)

require (
github.com/davecgh/go-spew v1.1.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v2 v2.2.2 // indirect
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
)

retract [v1.1.0, v1.1.1] // Improper versioning of breaking changes
12 changes: 6 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-logr/logr v0.4.0 h1:K7/B1jt6fIBQVd4Owv2MqGQClcgf0R266+7C/QjRcLc=
github.com/go-logr/logr v0.4.0/go.mod h1:z6/tIYblkpsD+a4lm/fGIIU9mZ+XfAiaFtq7xTgseGU=
github.com/go-logr/logr v1.2.3 h1:2DntVwHkVopvECVRSlL5PSo9eG+cAkDCuckLubN+rq0=
github.com/go-logr/logr v1.2.3/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
17 changes: 8 additions & 9 deletions log/fakes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,25 @@ import (
"github.com/go-logr/logr"
)

type nopLogger struct{}
type nopLogSink struct{}

func (nopLogger) Enabled() bool {
return false
func (nopLogSink) Init(logr.RuntimeInfo) {
}

func (nopLogger) Info(string, ...interface{}) {
func (nopLogSink) Enabled(int) bool {
return false
}

func (nopLogger) Error(error, string, ...interface{}) {
func (nopLogSink) Info(int, string, ...interface{}) {
}

func (nopLogger) V(int) logr.Logger {
return nil
func (nopLogSink) Error(error, string, ...interface{}) {
}

func (nopLogger) WithValues(...interface{}) logr.Logger {
func (nopLogSink) WithValues(...interface{}) logr.LogSink {
return nil
}

func (nopLogger) WithName(string) logr.Logger {
func (nopLogSink) WithName(string) logr.LogSink {
return nil
}
160 changes: 32 additions & 128 deletions log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,158 +2,62 @@ package log

import (
"fmt"
"io"
"os"
"sync"

"github.com/ViaQ/logerr/kverrors"
"github.com/go-logr/logr"
)

// ErrUnknownLoggerType is returned when trying to perform a *Logger only function
// that is incompatible with logr.Logger interface
var ErrUnknownLoggerType = kverrors.New("unknown error type")

var (
defaultOutput io.Writer = os.Stdout
defautLogLevel = 0
// ErrUnknownSinkType is returned when trying to perform a *Logger only function
// that is incompatible with logr.Logger interface
ErrUnknownSinkType = kverrors.New("unknown log sink type")

// logLevel sets the level at which you want logs to be displayed
// By default the verbosity is set to 0 and all logs that do not
// use V(...) will be printed. To increase logging verbosity
logLevel = defautLogLevel
defaultOutput = os.Stdout

mtx sync.RWMutex
logger logr.Logger = NewLogger("", os.Stdout, 0, JSONEncoder{})
defaultLogger = logr.New(NewLogSink("", defaultOutput, 0, JSONEncoder{}))
)

// Init initializes the logger. This is required to use logging correctly
// component is the name of the component being used to log messages.
// Typically this is your application name.
//
// keyValuePairs are key/value pairs to be used with all logs in the future
func Init(component string, keyValuePairs ...interface{}) {
InitWithOptions(component, nil, keyValuePairs...)
// DefaultLogger creates a logger without any key value pairs
func DefaultLogger() logr.Logger {
return defaultLogger
}

// MustInit calls Init and panics if it returns an error
func MustInit(component string, keyValuePairs ...interface{}) {
Init(component, keyValuePairs...)
// NewLogger creates a logger with the provided key value pairs
func NewLogger(component string, keyValuePairs ...interface{}) logr.Logger {
return NewLoggerWithOptions(component, nil, keyValuePairs...)
}

// InitWithOptions inits the logger with the provided opts
func InitWithOptions(component string, opts []Option, keyValuePairs ...interface{}) {
mtx.Lock()
defer mtx.Unlock()

ll := NewLogger(component, defaultOutput, 0, JSONEncoder{}, keyValuePairs...)
// NewLoggerWithOptions creates a logger with the provided opts and key value pairs
func NewLoggerWithOptions(component string, opts []Option, keyValuePairs ...interface{}) logr.Logger {
s := NewLogSink(component, defaultOutput, 0, JSONEncoder{}, keyValuePairs...)

for _, opt := range opts {
opt(ll)
opt(s)
}

// don't lock because we already have a lock
useLogger(ll)
}

// MustInitWithOptions calls InitWithOptions and panics if an error is returned
func MustInitWithOptions(component string, opts []Option, keyValuePairs ...interface{}) {
InitWithOptions(component, opts, keyValuePairs...)
}

// GetLogger returns the root logger used for logging
func GetLogger() logr.Logger {
return logger
}

// UseLogger bypasses the requirement for Init and sets the logger to l
func UseLogger(l logr.Logger) {
mtx.Lock()
defer mtx.Unlock()
useLogger(l)
}

// useLogger sets the logger to l without mtx.Lock()
// To use mtx.Lock see UseLogger
func useLogger(l logr.Logger) {
logger = l
return logr.New(s)
}

// Info logs a non-error message with the given key/value pairs as context.
//
// The msg argument should be used to add some constant description to
// the log line. The key/value pairs can then be used to add additional
// variable information. The key/value pairs should alternate string
// keys and arbitrary values.
func Info(msg string, keysAndValues ...interface{}) {
mtx.RLock()
defer mtx.RUnlock()
logger.Info(msg, keysAndValues...)
}

// Error logs an error, with the given message and key/value pairs as context.
// It functions similarly to calling Info with the "error" named value, but may
// have unique behavior, and should be preferred for logging errors (see the
// package documentations for more information).
//
// The msg field should be used to add context to any underlying error,
// while the err field should be used to attach the actual error that
// triggered this log line, if present.
func Error(err error, msg string, keysAndValues ...interface{}) {
mtx.RLock()
defer mtx.RUnlock()
logger.Error(err, msg, keysAndValues...)
}

// WithValues adds some key-value pairs of context to a logger.
// See Info for documentation on how key/value pairs work.
func WithValues(keysAndValues ...interface{}) logr.Logger {
mtx.RLock()
defer mtx.RUnlock()
return logger.WithValues(keysAndValues...)
}
// GetSink return the LogSink converted as a Sink object. It returns an
// error if it cannot convert it.
func GetSink(l logr.Logger) (*Sink, error) {
s, ok := l.GetSink().(*Sink)

// SetLogLevel sets the output verbosity
func SetLogLevel(v int) {
mtx.Lock()
defer mtx.Unlock()
logLevel = v
}

// SetOutput sets the logger output to w if the root logger is *log.Logger
// otherwise it returns ErrUnknownLoggerType
func SetOutput(w io.Writer) error {
mtx.RLock()
defer mtx.RUnlock()
switch ll := logger.(type) {
case *Logger:
ll.SetOutput(w)
default:
return kverrors.Add(ErrUnknownLoggerType,
"logger_type", fmt.Sprintf("%T", logger),
"expected_type", fmt.Sprintf("%T", &Logger{}),
if !ok {
return nil, kverrors.Add(ErrUnknownSinkType,
"sink_type", fmt.Sprintf("%T", s),
"expected_type", fmt.Sprintf("%T", &Sink{}),
)
}
return nil
return s, nil
}

// WithName adds a new element to the logger's name.
// Successive calls with WithName continue to append
// suffixes to the logger's name. It's strongly recommended
// that name segments contain only letters, digits, and hyphens
// (see the package documentation for more information).
func WithName(name string) logr.Logger {
mtx.RLock()
defer mtx.RUnlock()
return logger.WithName(name)
}

// V returns an Logger value for a specific verbosity level, relative to
// this Logger. In other words, V values are additive. V higher verbosity
// level means a log message is less important.
// V(level uint8) Logger
func V(level int) logr.Logger {
mtx.RLock()
defer mtx.RUnlock()
return logger.V(level)
// MustGetSink retrieves the Sink and panics if it gets an error from GetSink
func MustGetSink(l logr.Logger) *Sink {
s, err := GetSink(l)
if err != nil {
panic(err)
}
return s
}
Loading

0 comments on commit c53ff49

Please sign in to comment.