# Using @logmsg with begin/end blocks

**URL:** https://discourse.julialang.org/t/using-logmsg-with-begin-end-blocks/99290
**Category:** General Usage
**Tags:** logging
**Created:** [May 23, 2023, 2:58pm UTC](https://discourse.julialang.org/t/using-logmsg-with-begin-end-blocks/99290 "2023-05-23T14:58:14Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![florian](https://avatars.discourse-cdn.com/v4/letter/f/898d66/32.png) [@florian](https://discourse.julialang.org/u/florian)
#### Post date: [May 23, 2023, 2:58pm UTC](https://discourse.julialang.org/t/using-logmsg-with-begin-end-blocks/99290/1 "2023-05-23T14:58:15Z")

</div>

Is it somehow possible to do this:  
`@debug "Results of my calculations: " A=1:100 B=(1:100).*10`

In block form, meaning something like this:

```julia
@debug begin
    ## Do some stuff:
    A = 1:100
    B = A .* 10
    
    ## Create message
    "Results of my calculations: " A=A B=B
end

```

which is much more readable when I want to do some more complicated calculations in debug mode?

I’m aware of [this](https://discourse.julialang.org/t/use-logging-to-print-a-long-list-of-variables-in-a-single-print-block/40792/5) question, but I want the calculations to be performed only when the log level is actually set to debugging, which I am not sure the macro posted in that thread does.

Thanks!

---

<div class="post-metadata">

### Author: ![piechologist](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/piechologist/32/27662_2.png) [@piechologist](https://discourse.julialang.org/u/piechologist)
#### Post date: [May 23, 2023, 8:29pm UTC](https://discourse.julialang.org/t/using-logmsg-with-begin-end-blocks/99290/2 "2023-05-23T20:29:19Z")

</div>

You could call `@debug` like a function:

```julia
@debug("Results of my calculations:",
    A = 1:100,
    B = A .* 10,
)

```

The code inside the parentheses won’t be evaluated if debugging is off. Try:

```julia
expensive_calculation() = begin sleep(3); return 1:100 end

# Returns immediately:
@debug("Results of my calculations:",
    A = expensive_calculation(),
    B = A .* 10,
)

# Runs the expensive code:
ENV["JULIA_DEBUG"] = "all"
@debug("Results of my calculations:",
    A = expensive_calculation(),
    B = A .* 10,
)

```

---

<div class="post-metadata">

### Author: ![florian](https://avatars.discourse-cdn.com/v4/letter/f/898d66/32.png) [@florian](https://discourse.julialang.org/u/florian)
#### Post date: [May 24, 2023, 6:03am UTC](https://discourse.julialang.org/t/using-logmsg-with-begin-end-blocks/99290/3 "2023-05-24T06:03:16Z")

</div>

Thanks for your suggestion! But when I run this code, Julia complains that A is not defined:

```julia
┌ Error: Exception while generating log record in module Main at REPL[3]:1
│ exception =
│ UndefVarError: `A` not defined

```

Do you have an idea why? I have not yet taken a deep dive into how Julia’s macros work exactly, maybe I’ll have to do that now 😃

---

<div class="post-metadata">

### Author: ![piechologist](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/piechologist/32/27662_2.png) [@piechologist](https://discourse.julialang.org/u/piechologist)
#### Post date: [May 24, 2023, 7:10am UTC](https://discourse.julialang.org/t/using-logmsg-with-begin-end-blocks/99290/4 "2023-05-24T07:10:45Z")

</div>

Sorry, my bad. I must have had a global `A` left in the REPL when I tried that.

The log macros iterate over the key value pairs so, `A` goes out of scope before evaluating `B = ...`. We can’t do more complicated things than `A = function_call()`.

I use something like this in my code, generating a string and feeding it to the macro:

```julia
function debug_foo()
   A = 1:100
   B = A .* 10
   """Results of my calculations
      A = $A
      B = $B
   """
end
@debug debug_foo()

```
