Controlling the new Logger (@debug messages)

In 1.0 the new logging system has the @debug macro which only gets run if you turn on debug logging so no loss in performance. I can’t however figure out how to change the log level to get @debug messages to be printed. I’ve tried:

julia> using Logger

julia> disable_logging(LogLevel(-100000))
LogLevel(-99999)

julia> @debug "hi"

But still can’t turn on logging of @debug messages. I’ve tried searching github but can’t find anyone using @debug logging macros. I would like so see a few examples:

  1. How to turn on @debug macros for a single function body?
  2. How to turn it on globally?
  3. How to enable it in the REPL for all future commands?

Glen

1 Like

You need to establish a new logger, since they are immutable:


julia-1.0> logger=Logging.SimpleLogger(stderr,Logging.Debug)
Base.CoreLogging.SimpleLogger(Base.TTY(RawFD(0x0000000f) open, 0 bytes waiting), Debug, Dict{Any,Int64}())

julia-1.0> function foo(x)
           if x<5
           @debug "small"
           else
           @info "big"
           end
           end
foo (generic function with 1 method)

julia-1.0> Logging.with_logger(logger) do
           foo(10)
           foo(3)
           end
┌ Info: big
└ @ Main REPL[37]:5
┌ Debug: small
└ @ Main REPL[37]:3

You can swap out the one generally in use with global_logger (save the normal one with current_logger).

3 Likes

I had the same doubt, so I tried to modify the environment variable like this:

ENV["JULIA_DEBUG"] = "all"

(see doc here: https://docs.julialang.org/en/v1/stdlib/Logging/#Environment-variables-1)
and now the @debug messages are always visible.

4 Likes