# Determine the log level

**URL:** <https://discourse.julialang.org/t/determine-the-log-level/46718>\
**Category:** General Usage\
**Created:** [September 16, 2020, 4:51pm UTC](https://discourse.julialang.org/t/determine-the-log-level/46718 "2020-09-16T16:51:03Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![lyonsquark](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lyonsquark/32/4483_2.png) [@lyonsquark](https://discourse.julialang.org/u/lyonsquark)\
**Post date:** [September 16, 2020, 4:51pm UTC](https://discourse.julialang.org/t/determine-the-log-level/46718/1 "2020-09-16T16:51:03Z")

</div>

Hi - Is there a way to determine the current logging level in code? For example, I would like to do extra work (e.g. maybe record timing information) only if @debug logging is turned on for that module/file. E.g…

```julia
if loglevel == Debug # What goes here?
  # Do something extra
  results = collect_extra_diagnostics()
  @debug "Results are $results"
end

```

Or in general is doing this a bad idea? Thanks for any help/advice! - Adam

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [September 16, 2020, 4:55pm UTC](https://discourse.julialang.org/t/determine-the-log-level/46718/2 "2020-09-16T16:55:03Z")

</div>

I think you are “supposed” to do something like:

```julia
@debug "Results are " results = collect_extra_diagnostics()

```

and then that code only run if the debug code is executed.

---

<div class="post-metadata">

**Author:** ![lyonsquark](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lyonsquark/32/4483_2.png) [@lyonsquark](https://discourse.julialang.org/u/lyonsquark)\
**Post date:** [September 16, 2020, 4:57pm UTC](https://discourse.julialang.org/t/determine-the-log-level/46718/3 "2020-09-16T16:57:03Z")

</div>

Cool! I didn’t realize that would work! Will try it. Thanks!

---

<div class="post-metadata">

**Author:** ![tomerarnon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomerarnon/32/3170_2.png) [@tomerarnon](https://discourse.julialang.org/u/tomerarnon)\
**Post date:** [September 16, 2020, 7:52pm UTC](https://discourse.julialang.org/t/determine-the-log-level/46718/4 "2020-09-16T19:52:12Z")

</div>

To add to this, you can do

```julia
@debug begin
   # computations # 
   formatted_result = ...
end

```

for complicated debug operations.

---

<div class="post-metadata">

**Author:** ![lyonsquark](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lyonsquark/32/4483_2.png) [@lyonsquark](https://discourse.julialang.org/u/lyonsquark)\
**Post date:** [September 18, 2020, 5:10pm UTC](https://discourse.julialang.org/t/determine-the-log-level/46718/5 "2020-09-18T17:10:43Z")

</div>

Nice! I still don’t have an intuition for when begin & end can be tacked onto the end of a statement. This helps. Thanks - Adam

---

<div class="post-metadata">

**Author:** ![tomerarnon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomerarnon/32/3170_2.png) [@tomerarnon](https://discourse.julialang.org/u/tomerarnon)\
**Post date:** [September 18, 2020, 10:26pm UTC](https://discourse.julialang.org/t/determine-the-log-level/46718/6 "2020-09-18T22:26:15Z")

</div>

Basically always. `begin end` creates a “block” expression. The “return” of a block is the last statement ( ~~or an explicit `return` statement~~ not true, see reply). It behaves the same as any other julia expression, except the whole block is one expression (one standard “line”, up to the optional semicolon). You can do, e.g.

```julia
x = begin
        y = #...
        # ... 
        x
    end

```

(indenting conventions are a bit weird in these cases, the above is my preferred, but not everyones…)

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [September 18, 2020, 11:25pm UTC](https://discourse.julialang.org/t/determine-the-log-level/46718/7 "2020-09-18T23:25:46Z")

</div>

> [@tomerarnon](#):
>
> The “return” of a block is the last statement (or an explicit `return` statement).

Unfortunately, this is wrong. [If `return` appears within a block, it does not `return` from the block but from the enclosing function.](https://github.com/JuliaLang/julia/issues/30499#issuecomment-450009866) Using `return` inside a block passed to `@debug` will not have the desired outcome.
