JuliaLogging / TerminalLoggers.jl

Logging sinks and utilites for interactive terminals
MIT License
41 stars 10 forks source link

Easier way to prevent markdown parsing? #48

Open ssfrr opened 1 year ago

ssfrr commented 1 year ago

I’m logging an interpolated string, but underlines in the path are being interpreted as markdown by the logging:

julia> path = "some_path_foo"
"some_path_foo"

julia> @info "writing to $path"
[ Info: writing to somepathfoo

(you can’t see it, but path is underlined in the somepathfoo in the REPL).

My first idea was to add escapes to the _:

julia> @info replace("writing to $path", "_" => "\\_")
[ Info: writing to some_path_foo

This isn't super great because folks who are using the default ConsoleLogger will see the extra escape characters.

The other option would be to define a custom type that's not an AbstractString and defines the right show method, but this also seems clunky.

Is there an easier way to prevent markdown parsing?

ssfrr commented 1 year ago

For now I just defined

struct RawStr
    x::String
end
Base.show(io::IO, m::MIME"text/plain", s::RawStr) = show(io, m, s.x)

and I can do @info RawStr("writing to $path")

pepijndevos commented 1 year ago

There should really be a way to disable this IMHO as it breaks any multiline messages, making it unsuitable as a drop-in default logger or in combination with other loggers that don't do markdown

lmshk commented 10 months ago

I just had exactly the same problem. The fact that log messages are interpreted as Markdown by default surprised me, especially since if I actually wanted that I could just do:

using Markdown
@info md"Some _emphasized_ text."

Output: Some emphasized text.

Regarding the specific issue of interpolating paths: I usually quote values inside text output, especially if they are text themselves, e.g. in single quotes to signify which part of the message is variable. For TerminalLogger, quoting with backticks instead bypasses the issue:

@info "Some `path_with_underscores`."

Output: Some path_with_underscores.

This is not perfect because TerminalLogger just drops all styling, including the quotations, when the output stream does not support displaying it (like when redirecting standard error into a file) -- instead it should arguably output the source Markdown instead.

In my opinion, Markdown formatting should be opt-in.