Use Logging to print a long list of variables in a single print block

Hello everybody,

It is the first time for me to write in this forum.
I started using Julia recently, so I am sorry if my question might be very basic.

I am using the Logging package to simplify debugging my code.

In my case I would like printout multiple messages in a single @debug or @info block

Supposing my variables are a=1, b=2 and a=2.
The output I want can be achieved as follows:

@info “My info msg” a b c

The problem is that in my case instead of just 3 variables I would have around 20,
so the code would be come impossible to read, for example:

@info “My info msg” “message 1” var3 “message 2” “message 3” “…message n”

How can I print in single @info call without impacting the code readability?

I have tried also the following:

@info begin
“my msg”

But only the last variable (b in this case) is reads by the macro and considered as the message argument.

Thanks a lot in advance for your help

I am not sure I understand what you are trying to do here, but if you want to print 20 variables and 20 distinct messages, how could the code be any easier to read? You have to specify this information somehow.

Are you perhaps looking for

julia> a=1; b=2; c=3;

julia> @info """my msg
┌ Info: my msg
│ 1
│ 2
└ 3

The idea was to have the 20 variables (actually less in real) in the block (e.g. info or debug).
When passing all variable as parameter in the line of code soon becomes difficult to read.
If the variables could 1 per line than it would be, in my opinion, easier to handle

Thanks a lot @hendri54.
This actually a nice simple trick I did not think about, and pretty solves my problem.
Nice solution, and thanks again.

A little metaprogramming can help here:

macro myinfo(msg, block)
    block isa Expr && block.head == :block || error("Usage: @myinfo <msg> <block-with-variables>")
    variables = filter(x -> !(x isa LineNumberNode), block.args)
        @info $msg $(variables...)

How it works:

julia> a = 1; b = "hello"; c = rand();

julia> @myinfo "great stuffs" begin
┌ Info: great stuffs
│   a = 1
│   b = "hello"
└   c = 0.4109823300537361
1 Like