Git Product home page Git Product logo

Comments (8)

peterbourgon avatar peterbourgon commented on May 13, 2024

The obvious way is to canonize some levels into the interface definition, but I'd like to avoid that...

from kit.

peterbourgon avatar peterbourgon commented on May 13, 2024

We could define a new interface with canonized levels, with constructors that can wrap a logger...

from kit.

ChrisHines avatar ChrisHines commented on May 13, 2024

How would log.With compose with the new interface you propose?

from kit.

peterbourgon avatar peterbourgon commented on May 13, 2024

Not well :) Could probably dream up a special-case solution, like

type Foo interface {
    Info(keyvals ...interface{})
    Warn(keyvals ...interface{})
    Error(keyvals ...interface{})

    With(keyvals ...interface{}) Foo
}

But that feels awkward, opposite of the existing API.

from kit.

peterbourgon avatar peterbourgon commented on May 13, 2024

In general I'm struggling to embrace and extend log.With semantics...

from kit.

peterbourgon avatar peterbourgon commented on May 13, 2024

@ChrisHines, what do you think about the approach described in this README?

from kit.

ChrisHines avatar ChrisHines commented on May 13, 2024

@peterbourgon What do you think of this?

func With(keyvals ...interface{}) Context {
    c := Context{}
    return c.With(keyvals...)
}

type Context []interface{}

func (c Context) With(keyvals ...interface{}) Context {
    n := len(c)
    return append(c[:n:n], keyvals...)
}

func (c Context) LogTo(logger Logger) error {
    ctx := c
    if containsValuer(ctx) {
        ctx = append(Context{}, ctx...)
        bindValues(ctx)
    }
    return logger.Log(ctx...)
}

The above code would get used like this:

ctx := log.With("a", 123)
...
ctx = ctx.With("b", "c")
...
ctx.With("msg", "message").LogTo(logger)

from kit.

peterbourgon avatar peterbourgon commented on May 13, 2024

On first read I resisted the idea of manipulating a first-order Context object and logging it "to" a destination, but after rolling it around a little bit, it feels like it could reasonable. Any reason not to s/LogTo/Log/? Thinking out loud a little bit: we're flying pretty close to an encoder or writer API, I wonder if it doesn't make sense to make that more explicit, with something like

type Logger interface {
    Log(Context) error
}

func (c Context) Log(dst Logger) error {
    return dst.Log(c)
}

...which raises the question of where best to put the valuer stuff. It feels strange to have it only occur when you context.Log(logger), and get ignored if you logger.Log(context). Maybe your version, having the logger take a keyvals, makes that more explicit.

from kit.

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.