Comments (3)
Interesting proposal. If we adopted this, it would mean the following:
- Most (maybe all)
Handler
chains would need to end with a newHandler
that inserted the special keys (level, time, message) into the context Format
is still not composable, so it's still not easy to customize them without copying
Format
code from the individual implementations
It seems like regardless of whether we adopt this or not, we should extract out all the code which is shared between Format
s into Handler
s. (Putting the special keys into the context, normalizing values into strings via the TextMarshaller
interface, etc). The downside to this is that I suspect it will greatly increase the surface area of log15's API. This means both more APIs to support and it makes it more difficult to use because of the additional choices a developer will see. We could consider putting it into a format
sub-package, I suppose.
from log15.
@inconshreveable: Your comments above are thought provoking. I have several things to say in response, but I am going to break them into two posts. This post is a direct response to your comments. The second post will be a more abstract rethinking of the proposal.
RE: Handler Chains
As it stands now most Handler
chains end with one of the Handler
s that accepts a Format
argument which are FileHandler
, NetHandler
, or StreamHandler
. FileHandler
and NetHandler
are thin wrappers around StreamHandler
which contains the only call to Format.Format
within log15.
If we provide a Handler
that populates the context with the special keys, then, as a convenience, StreamHandler
could add it to the chain just as it currently does for LazyHandler
and SyncHandler
. However, it may be cleaner to simply put the special keys directly into the context.
RE: Composable Formats
I don't consider it a goal to make Format
composable. I am not sure what that buys us. Since Format
s convert structured data into unstructured []byte
s it seems rather difficult to create chains of them.
As mentioned in the proposal's rationale, Format
s would support customization by supporting the encoding.TextMarshaller
interface.
from log15.
@inconshreveable: While considering your comments it struck me that the Format
interface is really better thought of as an Encoder
along the lines of json.Encoder
, gob.Encoder
, and xml.Encoder
. This name convey the idea that the output should be machine readable and that individual values may implement the encoding.*Marshaller
interfaces to customize their layout. A typical logging pipeline would look as follows.
- Logger
- Handler
- Handler
- ....
- Handler
- Encoder -> *Marshallers
- io.Writer
We would declare Encoder as:
// An Encoder encodes log contexts.
type Encoder interface {
// Encode encodes the alternating pairs of string keys and values in ctx.
// Implementations should check for values that implement the interfaces
// of the standard library's encoding package.
Encode(ctx []interface{}) error
}
In this design the responsibilities are cleanly divided:
- Handler chains produce log contexts ([]interface{} with alternating key/value pairs).
- Encoders control the record format for a log context and provide default value formats.
- Values optionally control their format by implementing encoding.*Marshaller.
We give Encoders more implementation flexibility by not requiring them to return a []byte
. Also, a JsonEncoder or XmlEncoder can simply pass values on to a json.Encoder or xml.Encoder and take advantage of those packages's awareness of the encoding.TextMarshaller interface.
from log15.
Related Issues (20)
- Writing field's address instead of it's value HOT 1
- 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
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.