log15 icon indicating copy to clipboard operation
log15 copied to clipboard

Add a CustomString type to allow unescaped output.

Open kormat opened this issue 7 years ago • 6 comments

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


This change is Reviewable

kormat avatar Aug 08 '16 09:08 kormat

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.

ChrisHines avatar Aug 08 '16 13:08 ChrisHines

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.

kormat avatar Aug 08 '16 14:08 kormat

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.

kormat avatar Aug 08 '16 14:08 kormat

I've updated the PR and gist with a machine-readable representation.

kormat avatar Aug 08 '16 15:08 kormat

Rebased and updated. We've been using this for the last 2 years, and have tooling which parses the output.

kormat avatar Aug 21 '19 15:08 kormat

LGTM - I want an option to print un-escaped strings to the console for many reasons.

davisford avatar Nov 23 '19 23:11 davisford