How to Change LogLevel with Logging Package?

First of all, welcome! Yes, this is the right place for such a question. :slight_smile:

Logging in Julia works by having a Logger with certain properties do the actual logging. In case of the SimpleLogger from Base, you have to specify the minimum LogLevel at creation of the logger:

julia> using Logging

julia> logger = SimpleLogger(stdout, Logging.Debug)
SimpleLogger(Base.TTY(RawFD(0x0000000d) open, 0 bytes waiting), Debug, Dict{Any,Int64}())

julia> @info "info level!"
[ Info: info level!

julia> @debug "debug level!"

julia> old_logger = global_logger(logger)    # change the global logger to show debug output!
ConsoleLogger(Base.TTY(RawFD(0x0000000f) open, 0 bytes waiting), Info, Logging.default_metafmt, true, 0, Dict{Any,Int64}())

julia> @debug "debug level!"
┌ Debug: debug level!
└ @ Main REPL[7]:1

The predefined Loggers ConsoleLogger and SimpleLogger log messages with a LogLevel greater than or equal to the one specified during their creation - the default active logger only shows level Info and above:

julia> with_logger(old_logger) do
           @info "Info is here"
           @debug "Debug is not"
       end
[ Info: Info is here

Note also that Logging.Debug is a predefined LogLevel object:

help?> Logging.Debug
  No documentation found.

  Base.CoreLogging.Debug is of type LogLevel.

  Summary
  ≡≡≡≡≡≡≡≡≡

  struct LogLevel <: Any

  Fields
  ≡≡≡≡≡≡≡≡

  level :: Int32

There’s also a few packages providing more complex loggers, for example logging to any kind of IO based on the LogLevel (IOLogging.jl) or e.g. the FilteredLogger allowing better filtering of log messages when creating them (LoggingExtras.jl).

12 Likes