Determine the log level

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…

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

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

@debug "Results are " results = collect_extra_diagnostics()

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

1 Like

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

To add to this, you can do

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

for complicated debug operations.

2 Likes

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

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.

x = begin
        y = #...
        # ... 
        x
    end

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

1 Like

Unfortunately, this is wrong. If return appears within a block, it does not return from the block but from the enclosing function. Using return inside a block passed to @debug will not have the desired outcome.

1 Like