GithubHelp home page GithubHelp logo

zapr's People

Contributors

coderanger avatar dependabot[bot] avatar directxman12 avatar dongjiang1989 avatar naemono avatar pohly avatar stevenacoffman avatar tamalsaha avatar thockin avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

zapr's Issues

Allow mapping between logr V() and zap log levels

In k8s it's common to use relatively high V(2, 4, 6, 9) etc.

        l // zap logger at debug level
	zaprLogger := zapr.NewLogger(l)
	log.SetLogger(zaprLogger)
	fs := flag.NewFlagSet("dummy", flag.ContinueOnError)
	klog.InitFlags(fs)
	if err := fs.Lookup("v").Value.Set("9"); err != nil {
		panic(err)
	}
	klog.SetLogger(zaprLogger.WithName("klog"))

	klog.V(0).Infof("v: 0")
	klog.V(2).Infof("v: 2")
	klog.V(4).Infof("v: 4")
	klog.V(9).Infof("v: 9")

The only thing outputed is:

2021-08-04T20:40:00.933+0200    INFO    klog    v: 0

However I see kubernetes logs being outputted with the right V() all being info level:

2021-08-04T20:40:01.909+0200    INFO    klog    GET https://82281D6D2F9D29E40D559B8DC3DFAD76.gr7.us-east-1.eks.amazonaws.com/api?timeout=32s 200 OK in 972 milliseconds

2021-08-04T20:40:02.037+0200    INFO    klog    GET https://82281D6D2F9D29E40D559B8DC3DFAD76.gr7.us-east-1.eks.amazonaws.com/apis?timeout=32s 200 OK in 127 milliseconds

2021-08-04T20:40:02.166+0200    INFO    klog    GET https://82281D6D2F9D29E40D559B8DC3DFAD76.gr7.us-east-1.eks.amazonaws.com/apis/apps/v1?timeout=32s 200 OK in 127 milliseconds

2021-08-04T20:40:02.167+0200    INFO    klog    GET https://82281D6D2F9D29E40D559B8DC3DFAD76.gr7.us-east-1.eks.amazonaws.com/apis/coordination.k8s.io/v1?timeout=32s 200 OK in 128 milliseconds

2021-08-04T20:40:02.167+0200    INFO    klog    GET https://82281D6D2F9D29E40D559B8DC3DFAD76.gr7.us-east-1.eks.amazonaws.com/apis/apiregistration.k8s.io/v1beta1?timeout=32s 200 OK in 128 milliseconds

(I'm not entirely sure what's happening within that code).

What I'd like to have custom function/mapping how V(...) map to zap log levels, and even have filtering capabilities from provided function.

warn about malformed parameters

klog.InfoS and klog.ErrorS warn about invalid parameters:

"odd number of arguments passed as key-value pairs for logging","ignored key":"akey2"

It might make sense to add that also to zapr, for the sake of consistency.

This is relevant for WithValues, Info, Error.

DisableCaller in Zap Config

I have a zapr logger working, but it appears the logger is not respecting the DisableCaller config value that is being set. Here is the code sample:

func init() {
	// Build a zap logger.
	cfg := zap.NewDevelopmentConfig()
	// Stop annotating logs with the calling function's file name.
	cfg.DisableCaller = false
	fmt.Printf("DisableCaller=%v\n", cfg.DisableCaller)
	zapLog, err := cfg.Build()
	if err != nil {
		panic(fmt.Sprintf("error building logger: %v", err))
	}
	defer zapLog.Sync()

	// zapr defines an implementation of the Logger
	// interface built on top of Zap (go.uber.org/zap).
	Logger = zapr.NewLogger(zapLog)
	Logger.Info("started zapr logger")
}

As you can see from the logs, the calling function's file name and line number still appear.

DisableCaller=false
2019-02-20T22:26:23.561-0800	INFO	zapr/zapr.go:69	started zapr logger

Incorrect number of arguments detected

Some Background:
I've been trying to create a top level logger for an application using zap, and zapr.
The api I am aiming for is something along the lines of this:

  • log.Debug(msg, "key 1", "value 1")
  • log.Info(msg, "key 2", "value 2")
    Whenever I attempt to do this, I get the error odd number of arguments passed as key-value pairs for logging

Code to reproduce error

Here's some sample code to reproduce the error:

package main

import (
	"github.com/go-logr/logr"
	"github.com/go-logr/zapr"
	"go.uber.org/zap"
	"go.uber.org/zap/zapcore"
	"os"
)

func NewZaprLogger() logr.Logger {

	consoleWriterSyncer := zapcore.AddSync(os.Stderr)
	logConsoleEnc := zapcore.NewConsoleEncoder(zap.NewDevelopmentEncoderConfig())

	levelSelector := zap.LevelEnablerFunc(func(lvl zapcore.Level) bool {
		return lvl >= zapcore.DebugLevel
	})

	logCore := zapcore.NewTee(zapcore.NewCore(logConsoleEnc, consoleWriterSyncer, levelSelector))

	zapLog := zap.New(logCore)

	logLocal := zapr.NewLogger(zapLog)

	return logLocal
}

func Debug(msg string, keysAndValues ...interface{}) {

	logger := NewZaprLogger()

	if keysAndValues != nil {
		logger.V(1).Info(msg, keysAndValues)
	} else {
		logger.V(1).Info(msg)
	}

}

func main() {
	Debug("this is a message", "key 1", "value 1")
}

Here's the output I get:

2021-07-21T10:25:27.128-0400    DPANIC  odd number of arguments passed as key-value pairs for logging   {"ignored key": ["key 1","value 1"]}
2021-07-21T10:25:27.128-0400    DEBUG   this is a message

Questions:

  • Is this a use case that is supported by Zapr?
  • Do I need to initialize the logger differently to make it work?

I recognize that it's possible that my use case is not one supported by Zapr, but I wanted to ask before I changed my approach.

Any insights on this is greatly appreciated.

Thanks!

Wrong line reported

With the changes to fix #6 now creating loggers using WithName or WithValues now skips to many levels of callstack
e.g.

log = zapr.NewLogger(zapLog).WithValues("version", Version).WithName("test")
log.V(0).Info("Initializing")

now prints

{"level":"info","ts":1576782453.943943,"logger":"test","caller":"runtime/asm_amd64.s:1357","msg":"Initializing","version":""}```

AddCallerSkip should only skip a level on a "root" logger


Panic when using production configs and small log level

This is related with this issue.

When trying to use other log levels smaller than -1 (e.g. V(2)) with cfg := zap.NewProductionConfig() (with development config works fine) I've got the following panic error:

panic: runtime error: index out of range

goroutine 1 [running]:
github.com/presslabs/mysql-operator/vendor/go.uber.org/zap/zapcore.(*counters).get(0xc0001d2000, 0xc000151cfe, 0xbb762a, 0xc, 0xc000151be0)
	/home/x/go/src/github.com/presslabs/mysql-operator/vendor/go.uber.org/zap/zapcore/sampler.go:48 +0x79
github.com/presslabs/mysql-operator/vendor/go.uber.org/zap/zapcore.(*sampler).Check(0xc00018f560, 0xfe, 0xbee5907d06b7cfd4, 0x2a36d55, 0x11b62a0, 0xbb4baf, 0x7, 0xbb762a, 0xc, 0x0, ...)
	/home/x/go/src/github.com/presslabs/mysql-operator/vendor/go.uber.org/zap/zapcore/sampler.go:128 +0x9d
github.com/presslabs/mysql-operator/vendor/go.uber.org/zap.(*Logger).check(0xc0000a91a0, 0x40bffe, 0xbb762a, 0xc, 0xc00010e501)
	/home/x/go/src/github.com/presslabs/mysql-operator/vendor/go.uber.org/zap/logger.go:269 +0x137
github.com/presslabs/mysql-operator/vendor/go.uber.org/zap.(*Logger).Check(0xc0000a91a0, 0xb2a0fe, 0xbb762a, 0xc, 0xfffffffffffffffe)
	/home/x/go/src/github.com/presslabs/mysql-operator/vendor/go.uber.org/zap/logger.go:172 +0x48
github.com/presslabs/mysql-operator/vendor/github.com/go-logr/zapr.(*infoLogger).Info(0xc0000bba40, 0xbb762a, 0xc, 0x0, 0x0, 0x0)
	/home/x/go/src/github.com/presslabs/mysql-operator/vendor/github.com/go-logr/zapr/zapr.go:69 +0x4e
main.main()
	/home/x/go/src/github.com/presslabs/mysql-operator/cmd/mysql-operator-sidecar/main.go:119 +0x62f

Build issues on Go 1.21

Everything is fine when running on Go 1.20 but when I go vet on Go 1.21 issues are found.

$ go version
go version go1.20.14 linux/amd64
go vet ./operator/...
$ go version
go version go1.21.8 linux/amd64
$ go vet ./operator/...
# github.com/go-logr/zapr
../../../../pkg/mod/github.com/go-logr/[email protected]/slogzapr.go:61:31: undefined: zap.Inline
../../../../pkg/mod/github.com/go-logr/[email protected]/slogzapr.go:169:3: unknown field Function in struct literal of type zapcore.EntryCaller
../../../../pkg/mod/github.com/go-logr/[email protected]/slogzapr.go:175:37: undefined: zap.Inline
../../../../pkg/mod/github.com/go-logr/[email protected]/zapr_slog.go:42:14: undefined: zap.Inline

Originally found in this PR

Wrong line reported

Running the example program from the README prints this line:

2019-08-08T11:36:25.528-0400	INFO	zapr/zapr.go:69	Logr in action!	{"the answer": 42}

I would expect it to print the file and line of the program, rather than a line within zapr.go.

use zap.Inline instead of allocating []zap.Field

While looking at zapr again while adding slog support, I noticed that we unnecessarily build up a []zap.Field in WithValues and while handling Info and Error. What we can do instead is zap.Inline and there directly encode all parameters, without additional allocations.

inconsistent logging of value vs. value embedded somewhere else

When the value in a key/value pair is logged, it gets passed through zap.Any which uses certain interfaces (like Stringer) if available to serialize the value.

When that same value is contained in something else like a slice or a struct, it gets handled differently by zap.Reflect: those interfaces are ignored.

This is why []klog.ObjectRef worked as intended (logged as array of structs) whereas kobj.ObjectRef didn't (logged as string).

I'm not sure whether this is a problem, but it seems inconsistent.

Logr to Zap Level Confusion

Hi,

I'm trying to understand how I can use Logr to set the log levels for logging verbosity. I'm pretty new to this library and Zap in general, so I'll probably ask some pretty fundamental questions here.

My requirement is that I want to be able to show/hide logs depending on verbosity. For example, I want to set a setting in the logger to only show V(2) or above (in this case it is Warning and above for Zap).

The problem is that with using the Logr interface, the V() function only applies non-negative values. According to this StackOverflow post, all the levels from Logr are inverted in Zap. This means to use the Warning level in Zap, I must pass in V(-2). However, this is not possible given that the function implementation applies all negative values as 0: https://github.com/go-logr/logr/blob/master/logr.go#L282-L292

Is it correct my understanding of the mapping is this:

Logr Zap
1 -1 (Debug)
0 0 (Info)
-1 1 (Warning)
-2 2 (Error)

and so on...

How can I set negative verbosity levels if I am restricted to only using the Logr interface?

Thanks

Data race in WithName() implementation?

Hi! I'm using v1.2.3 with zap v1.19.0 and it seems like there's a data race in WithName() method. Here's the Go's data race detector output:

==================                                                                                                                                                                                                                 
WARNING: DATA RACE
Read at 0x00c0006a0180 by goroutine 21:
  runtime.racereadrange()
      <autogenerated>:1 +0x1b
  github.com/go-logr/zapr.(*zapLogger).WithName()
      /Users/pachanga/go/pkg/mod/github.com/go-logr/[email protected]/zapr.go:228 +0xc6
  github.com/go-logr/logr.Logger.WithName()
  ...

Previous write at 0x00c0006a0180 by goroutine 19:
  go.uber.org/zap.(*Logger).clone()
      /Users/pachanga/go/pkg/mod/go.uber.org/[email protected]/logger.go:257 +0x7d
  go.uber.org/zap.(*Logger).Named()
      /Users/pachanga/go/pkg/mod/go.uber.org/[email protected]/logger.go:143 +0x49
  github.com/go-logr/zapr.(*zapLogger).WithName()
      /Users/pachanga/go/pkg/mod/github.com/go-logr/[email protected]/zapr.go:228 +0xc6
  github.com/go-logr/logr.Logger.WithName()
   ...

WithName and WithValues should preserve log level

WithName and WithValues methods don't preserve the parent log level - they always create a new logger with zap.InfoLevel (V-level 0)

package main

import (
	"github.com/go-logr/zapr"
	"go.uber.org/zap"
)

func main() {
	zl, _ := zap.NewProduction()
	l := zapr.NewLogger(zl)
	l.V(0).Info("level 0 should be logged")
	l.V(1).Info("level 1 should not be logged")
	l.V(1).WithValues("key", "value").Info("level 1 with values should not be logged")
	l.V(1).WithName("debugName").Info("level 1 with name should not be logged")
}

output:

{"level":"info","ts":1601586159.5837796,"caller":".local/run.go:11","msg":"level 0 should be logged"}
{"level":"info","ts":1601586159.583864,"caller":".local/run.go:13","msg":"level 1 with values should not be logged","key":"value"}
{"level":"info","ts":1601586159.5838814,"logger":"debugName","caller":".local/run.go:14","msg":"level 1 with name should not be logged"}

"stacktrace":"github.com/go-logr/zapr.(*zapLogger).Error

Team,

We have been facing this error while deploying OPA-Gatekeeper policies, Please feel free to close this issue if you think it is not related to this project.

resource","metaKind":"upgrade","name":"allow-only-ecr-registry","namespace":"","error":"Operation cannot be fulfilled on k8sallowedrepos.constraints.gatekeeper.sh "allow-only-ecr-registry": the object has been modified; please apply your changes to the latest version and try again","stacktrace":"github.com/go-logr/zapr.(*zapLogger).Error

Tag a new release

Just came here to report #6 but notice it's been fixed, just the change hasn't been release under a new tag.

Go Modules will by default install v0.1.1 which doesn't have these changes.

Workaround for now is to go get github.com/go-logr/zapr@master, but many people won't do this by default.

Could v0.1.2 be created?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.