log15
log15 copied to clipboard
Add a CustomString type to allow unescaped output.
When trying to print multi-line (or otherwise formatted) messages, the fact that all strings are escaped becomes a problem. For example:
func PanicLog() {
if err := recover(); err != nil {
log.Crit("Panic", "stack", string(debug.Stack()))
}
}
The stack trace becomes a single line, with all \n's and \t's escaped. With this new type, the central line becomes:
log.Crit("Panic", "stack", log.CustomString(debug.Stack()))
and now it formats in a readable way. Any CustomString values are surrouneded by triple backticks to it easier to see where they start/end. An example output is viewable here: https://gist.github.com/kormat/10e09e4dcfef0115c65fcdc20c98297e
Triple back ticks are not part of what is currently understood as the logfmt format and are not understood by the existing Go logfmt parsers in github.com/kr/logfmt and github.com/go-logfmt/logfmt. I doubt that the logfmt parsers written in other languages will parse triple back ticks either.
A log15.CustomString that itself contains triple back ticks would result in an ambiguous log entry.
Using the proposed log15.CustomString type would help human readability, but harm machine readability.
Hmm, k. Looking at kr, i don't see any way to support making multi-line output readable for humans. Assuming that is the case, do you think it would be possible to get it merged anyway, with a warning saying that this won't work for machine parsing? We use multi-line logging a Lot in our code.
It wouldn't be hard to make it machine readable (e.g. by prefixing all of the lines with something like >
), just not within logfmt.
I've updated the PR and gist with a machine-readable representation.
Rebased and updated. We've been using this for the last 2 years, and have tooling which parses the output.
LGTM - I want an option to print un-escaped strings to the console for many reasons.