Call @show in logging macro

Does anyone know how to call @show in logging macro, ex @info?

I tried:

julia> using Logging

julia> @info “hi”
[ Info: hi

julia> message = “hi”
“hi”

julia> @info message
[ Info: hi

julia> @info @show message
message = “hi”
[ Info: hi

But, I want to get this using both @show and @info:

[ info: message = “hi”

If you give some string as the first arg to @info you’ll end up with

julia> @info "" message
┌ Info: 
└   message = "hi"

which might be what you’re looking for?

3 Likes

Thank you!!. It works for me.
But it is perfect if the output becomes just one line, not two line…

The option of @pfitzseb is nice.
Another option is to define your own macro:

macro infoshow(expr) 
      quote 
              @info string($(QuoteNode(expr)), " = ", $expr) 
      end 
end

Then, you can use it directly:

julia> @infoshow message
[ Info: message= hola

Edit: In that case, you are limited to only use one variable at the same time, I do not know enough of macros to make it as good as original @show macro.

2 Likes

You can use OneLineTransformerLogger from LoggingFacilities.jl.

4 Likes

Thanks all. All answers are great. I dare say that @pfitzseb 's answer was the one I was looking for the most. So marked it as Solution.

1 Like