Comments (8)
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.
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=42This 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.
What is the API you wished existed for this?
from log15.
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.
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.
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.
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.
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)
- Add Illumos support HOT 1
- support log rotate HOT 3
- Expose the runtime.Callers skip HOT 1
- tag a new release HOT 1
- v2.12 Build Issues HOT 3
- Support for a kv-generating interface on log arguments
- Normal log15.Root() not colorized on Cygwin
- logfmt/json keys for msg/lvl/t are hard-coded HOT 3
- Allow logging in UTC HOT 2
- Use Semantic Version Tags compatible with the new Go modules HOT 1
- Please add a go.mod file HOT 1
- make NetHandler able to log to an URL?
- No way to close files opened when a new FileHandler is created.
- Default Lvl is LvlCrit HOT 1
- proposal: add Fatal() HOT 1
- Test failure while building v2.15
- Use semantic versioning to satisfy `go mod`
- Is this library maintained? HOT 3
- CallerFileHandler should include package name
- Different outputs for different functions
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from log15.