Coder Social home page Coder Social logo

Comments (3)

thockin avatar thockin commented on June 2, 2024

At best, these would be optional interfaces, so you'd have to do something like:

func myFunc(log logr.Logger) {
    if name, ok := log.GetName(); ok {
        if name != "something" {
            log = log.WithName("something")
        }
    }
}

Underneath, it would test the LogSink for support and tell you if it supported it or not.

Deleting a key would be the same, though I am more suspicious of that use-case.

A more DIY approach would be something like:

func MyFunc(log logr.Logger) {
    myFuncInternal(logr.WithName("something").WithValues("key", "value"))
}

func myFuncInternal(log logr.Logger) {
    // do stuff
    // recursion calls myFuncInternal
}

from logr.

pohly avatar pohly commented on June 2, 2024

I have duplicates of the key key with different values which makes the field useless.

I'm not so sure about this. My understanding of the "repeated key" semantic is that the last valye wins, so (in JSON) {"foo": "bar", "foo": "x"} would be equivalent to {"foo": "x"}. But later I learned that the JSON spec doesn't actually guarantee that. It's up to the parser which value it uses.

This makes it a problem of the log backend. The klog text backend doesn't de-duplicate logger.WithValues("foo", "var").WithValues("foo", "x"). It does de-duplicate logger.WithValues("foo", "bar").Info("something", "foo", "x"). My rationale was that WithValues is easier to keep under control such that keys are unique, whereas duplication of keys with log calls is currently unavoidable in Kubernetes (contextual logging and thus WithValues is optional). Given what you described it would make more sense to always de-duplicate everything, even if that causes overhead.

zap doesn't de-duplicate. It seems to rely on JSON parsers using the latest value.

from logr.

thockin avatar thockin commented on June 2, 2024

Strictly, JSON fields are reorderable. Practically, I think plenty of things depen on in-order processing.

FWIW: funcr does not de-dup keys, either. It could de-dup across successive WithValues calls, but de-duping across WithValues and Info/Error calls is harder and more expensive (WithValues is pre-rendered).

from logr.

Related Issues (20)

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.