Git Product home page Git Product logo

Comments (8)

inconshreveable avatar inconshreveable commented on June 3, 2024

Would log.Lazy with a context value work for you? Or is that too clumsy?

x := 1
l := log.New("x", log.Lazy{func() int { return x }})
l.Info("test") # x=1
x = 42
l.Info("lazy evaluation") # x=42

This works because the lazy function is evaluated every time the logger is invoked. This essentially lets you change context values on the fly.

from log15.

chendo avatar chendo commented on June 3, 2024

Hmmm.. Could work, but feels a bit clumsy. The attribute doesn't change
that often and it feels like the overhead could be avoided.

I'm happy to use that though, thanks!

On Wednesday, September 17, 2014, inconshreveable [email protected]
wrote:

Would log.Lazy with a context value work for you? Or is that too clumsy?

x := 1
l := log.New("x", log.Lazy{func() int { return x }})
l.Info("test") # x=1
x = 42
l.Info("lazy evaluation") # x=42

This works because the lazy function is evaluated every time the logger is
invoked. This essentially lets you change context values on the fly.


Reply to this email directly or view it on GitHub
#25 (comment)
.

Sent from my iPhone.

from log15.

inconshreveable avatar inconshreveable commented on June 3, 2024

What is the API you wished existed for this?

from log15.

chendo avatar chendo commented on June 3, 2024

Hmm, maybe something like:

l := log.New("attr", "123") # attr=123
...
l.SetContext("attr", "456") # replaces, attr=456

Or potentially a merge:

l := log.New("attr", "123")

l = l.New("attr", "456") # overwrites, attr=456

The first one is more explicit so I'd prefer that

from log15.

chendo avatar chendo commented on June 3, 2024

So I gave the log.Lazy a go, and it's giving showing me a LOG15_ERROR where it should be showing up.

    c.log = Log.New("connection", id.String(), "ip", host, "port", port,
        log.Lazy{func() string {
            switch c.ConnectionType {
            case ClientConnection:
                return "client"
            case DeviceConnection:
                return "device"
            }
            return ""
        }},
        log.Lazy{func() string {
            switch c.ConnectionType {
            case ClientConnection:
                return c.clientUUID
            case DeviceConnection:
                return c.deviceUUID
            }
            return ""
        }},
    )

Also, it's kind of hackish given that the connection doesn't know what type of connection it is at that point, so ideally it wouldn't even have a device=x or a client=x until it knows.

from log15.

inconshreveable avatar inconshreveable commented on June 3, 2024

You're missing key values for these values. Instead try this (I added keys for "id", "type" and "uuid"):

c.log = Log.New("connection", "id", id.String(), "ip", host, "port", port,
        "type", log.Lazy{func() string {
            switch c.ConnectionType {
            case ClientConnection:
                return "client"
            case DeviceConnection:
                return "device"
            }
            return ""
        }},
        "uuid", log.Lazy{func() string {
            switch c.ConnectionType {
            case ClientConnection:
                return c.clientUUID
            case DeviceConnection:
                return c.deviceUUID
            }
            return ""
        }},
    )

My favorite way to do this is if you control the connection class you can define two functions on it Conn.Type() and Conn.Id() and just use those with lazy instead. Like so:

c.log = Log.New("connection", "id", id.String(), "ip", host, "port", port, "type", log.Lazy{c.Type}, "uuid", log.Lazy{c.Id})

from log15.

chendo avatar chendo commented on June 3, 2024

Ah, I was hoping to have “client=” or “device=” depending on what type it is. Does Lazy not work for keys?

On Sep 19, 2014, at 2:22 PM, inconshreveable [email protected] wrote:

You're missing key values for these values. Instead try this (I added keys for "id", "type" and "uuid"):
c.log = Log.New("connection", "id", id.String(), "ip", host, "port", port,
"type", log.Lazy{func() string {
switch c.ConnectionType {
case ClientConnection:
return "client"
case DeviceConnection:
return "device"
}
return ""
}},
"uuid", log.Lazy{func() string {
switch c.ConnectionType {
case ClientConnection:
return c.clientUUID
case DeviceConnection:
return c.deviceUUID
}
return ""
}},
)
My favorite way to do this is if you control the connection class you can define two functions on itConn.Type()
andConn.Id()
and just use those with lazy instead. Like so:
c.log = Log.New("connection", "id", id.String(), "ip", host, "port", port, "type", log.Lazy{c.Type}, "uuid", log.Lazy{c.Id})


Reply to this email directly orview it on GitHub
.

from log15.

inconshreveable avatar inconshreveable commented on June 3, 2024

Correct, lazy only works for values. That’s probably something I should add to the documentation.

On Sep 18, 2014, at 9:24 PM, Jack Chen [email protected] wrote:

Ah, I was hoping to have “client=” or “device=” depending on what type it is. Does Lazy not work for keys?

On Sep 19, 2014, at 2:22 PM, inconshreveable [email protected] wrote:

You're missing key values for these values. Instead try this (I added keys for "id", "type" and "uuid"):
c.log = Log.New("connection", "id", id.String(), "ip", host, "port", port,
"type", log.Lazy{func() string {
switch c.ConnectionType {
case ClientConnection:
return "client"
case DeviceConnection:
return "device"
}
return ""
}},
"uuid", log.Lazy{func() string {
switch c.ConnectionType {
case ClientConnection:
return c.clientUUID
case DeviceConnection:
return c.deviceUUID
}
return ""
}},
)
My favorite way to do this is if you control the connection class you can define two functions on itConn.Type()
andConn.Id()
and just use those with lazy instead. Like so:
c.log = Log.New("connection", "id", id.String(), "ip", host, "port", port, "type", log.Lazy{c.Type}, "uuid", log.Lazy{c.Id})


Reply to this email directly orview it on GitHub
.


Reply to this email directly or view it on GitHub.

from log15.

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.