I can’t repro this. Your code (slightly adapted to fit into one file)
module LoggerProblem
export log_example
function log_example()
@debug "This statement should appear at debug level."
@warn "This statement should appear at warn level."
end
end # module
using Logging
logger = ConsoleLogger(stdout, Logging.Debug)
old_logger = global_logger(logger)
LoggerProblem.log_example()
global_logger(old_logger)
LoggerProblem.log_example()
produces
┌ Debug: This statement should appear at debug level.
└ @ Main.LoggerProblem untitled-b1939cc8a3fc9b574cb2a0b15f6dd330:6
┌ Warning: This statement should appear at warn level.
└ @ Main.LoggerProblem untitled-b1939cc8a3fc9b574cb2a0b15f6dd330:7
┌ Warning: This statement should appear at warn level.
└ @ Main.LoggerProblem untitled-b1939cc8a3fc9b574cb2a0b15f6dd330:7
julia> logger = ConsoleLogger(stdout, Logging.Debug)
ConsoleLogger(Base.TTY(RawFD(0x00000018) open, 0 bytes waiting), Debug, Logging.default_metafmt, true, 0, Dict{Any,Int64}())
julia> old_logger = global_logger(logger)
ConsoleLogger(Base.TTY(RawFD(0x0000001a) open, 0 bytes waiting), Info, Logging.default_metafmt, true, 0, Dict{Any,Int64}())
julia> LoggerProblem.log_example()
┌ Debug: This statement should appear at debug level.
└ @ Main.LoggerProblem untitled-b1939cc8a3fc9b574cb2a0b15f6dd330:6
┌ Warning: This statement should appear at warn level.
└ @ Main.LoggerProblem untitled-b1939cc8a3fc9b574cb2a0b15f6dd330:7
julia> global_logger(old_logger)
ConsoleLogger(Base.TTY(RawFD(0x00000018) open, 0 bytes waiting), Debug, Logging.default_metafmt, true, 0, Dict{Any,Int64}())
julia> LoggerProblem.log_example()
┌ Warning: This statement should appear at warn level.
└ @ Main.LoggerProblem untitled-b1939cc8a3fc9b574cb2a0b15f6dd330:4774:7
in Juno. The REPL output is exactly the same:
julia> module LoggerProblem
export log_example
function log_example()
@debug "This statement should appear at debug level."
@warn "This statement should appear at warn level."
end
end # module
Main.LoggerProblem
julia> using Logging
julia> logger = ConsoleLogger(stdout, Logging.Debug)
ConsoleLogger(Base.TTY(RawFD(0x00000018) open, 0 bytes waiting), Debug, Logging.default_metafmt, true, 0, Dict{Any,Int64}())
julia> old_logger = global_logger(logger)
ConsoleLogger(Base.TTY(RawFD(0x0000001a) open, 0 bytes waiting), Info, Logging.default_metafmt, true, 0, Dict{Any,Int64}())
julia> LoggerProblem.log_example()
┌ Debug: This statement should appear at debug level.
└ @ Main.LoggerProblem REPL[1]:6
┌ Warning: This statement should appear at warn level.
└ @ Main.LoggerProblem REPL[1]:7
julia> global_logger(old_logger)
ConsoleLogger(Base.TTY(RawFD(0x00000018) open, 0 bytes waiting), Debug, Logging.default_metafmt, true, 0, Dict{Any,Int64}())
julia> LoggerProblem.log_example()
┌ Warning: This statement should appear at warn level.
└ @ Main.LoggerProblem REPL[1]:7
There is some weirdness when changing the global logger in one block though:
julia> begin
logger = ConsoleLogger(stdout, Logging.Debug)
old_logger = global_logger(logger)
LoggerProblem.log_example()
global_logger(old_logger)
LoggerProblem.log_example()
end
┌ Warning: This statement should appear at warn level.
└ @ Main.LoggerProblem untitled-b1939cc8a3fc9b574cb2a0b15f6dd330:7
┌ Warning: This statement should appear at warn level.
└ @ Main.LoggerProblem untitled-b1939cc8a3fc9b574cb2a0b15f6dd330:7
In general, I’d recommend using with_logger
instead of explicitly changing the global logger.
The problem here is that min_enabled_level
is only update once per logger, but relies on the global logger’s state in Juno. We could conceivably fix this by instead using
Logging.min_enabled_level(j::JunoProgressLogger) = Debug
(or whatever the minimal log level is), but that probably has performance implications.
Ideally we’d have a logger stack (a bit like the display system) for cases such as this (any thoughts on this, @c42f?), or just stop (ab-)using the logging system for progress bars.