Logging: symbol evaluation under the hood

Hey everyone,

As mentioned in other posts, I’m working on a package called Term.jl that does a bunch of pretty terminal stuff like printing formatted error messages:

One of the features I’d like to add is formatted logging. Similar to the built-in logging system but with fancier formatting. Such that:

function foo()
    x = 1
    @info "this sis sodifsdfnso" 1+1 x 2x
    @tinfo "this sis sodifsdfnso" 1+1 x 2x
end

which would normally give
image

would give something with a bit more color:
image

However I can’t quite figure out how the logging code in Base evaluates symbols and expressions to produce the logging message. E.g if while handling that log message I try to evaluate 2x I get an error saying that x is not defined (as it isn’t within the @tinfo macro). I’ve looked in details at julia/logging.jl, but the code is fairly dense and I can’t quite figure out how they get the values associated with variables/symbols passed to the logging macros. I suspect that it’s handled by log_msgcode, but I can’t figure out what that function does exactly.

Any help would be appreciated!

3 Likes

Have you seen https://julialogging.github.io/? I believe that should help you with all your questions.

2 Likes

I have not! That seems to be very useful, I’ll need to dig in and see how to combine it with styling done by Term.jl.

Thank you :pray:

Logging.jl had all I needed, thank you @fredrikekre

image

@fredrikekre do you know if there’s something similar for error messages?

Something that takes care of all the hard work and allows me to just define some code to style the error messages?