diff --git a/base/logging.jl b/base/logging.jl index 6bcb82cdf6156b..87c325ef6eb946 100644 --- a/base/logging.jl +++ b/base/logging.jl @@ -298,12 +298,11 @@ function logmsg_code(_module, file, line, level, message, exs...) level = $level std_level = convert(LogLevel, level) if std_level >= _min_enabled_level[] - logstate = current_logstate() - if std_level >= logstate.min_enabled_level - logger = logstate.logger - _module = $_module + group = $group + _module = $_module + logger = current_logger_for_env(std_level, group, _module) + if !(logger === nothing) id = $id - group = $group # Second chance at an early bail-out, based on arbitrary # logger-specific logic. if shouldlog(logger, level, _module, group, id) @@ -313,9 +312,6 @@ function logmsg_code(_module, file, line, level, message, exs...) msg = $(esc(message)) handle_message(logger, level, msg, _module, group, id, file, line; $(kwargs...)) catch err - if !catch_exceptions(logger) - rethrow(err) - end logging_error(logger, level, _module, group, id, file, line, err) end end @@ -327,7 +323,10 @@ end # Report an error in log message creation (or in the logger itself). @noinline function logging_error(logger, level, _module, group, id, - filepath, line, err) + filepath, line, @nospecialize(err)) + if !catch_exceptions(logger) + rethrow(err) + end try msg = "Exception while generating log record in module $_module at $filepath:$line" handle_message(logger, Error, msg, _module, :logevent_error, id, filepath, line; exception=(err,catch_backtrace())) @@ -371,7 +370,16 @@ _global_logstate = LogState(NullLogger()) function current_logstate() logstate = current_task().logstate - (logstate != nothing ? logstate : _global_logstate)::LogState + return (logstate !== nothing ? logstate : _global_logstate)::LogState +end + +# helper function to get the current logger, if enabled for the specified message type +@noinline function current_logger_for_env(std_level::LogLevel, group, _module::Module) + logstate = current_logstate() + if std_level >= logstate.min_enabled_level || env_override_minlevel(group, _module) + return logstate.logger + end + return nothing end function with_logstate(f::Function, logstate) @@ -400,6 +408,42 @@ function disable_logging(level::LogLevel) _min_enabled_level[] = level + 1 end +let _debug_groups = Symbol[], + _debug_str::String = "" +global function env_override_minlevel(group, _module::Module) + debug = get(ENV, "JULIA_DEBUG", "") + if !(debug === _debug_str) + _debug_str = debug + empty!(_debug_groups) + for g in split(debug, ',') + isempty(g) && continue + if g == "all" + empty!(_debug_groups) + push!(_debug_groups, :all) + break + end + push!(_debug_groups, Symbol(g)) + end + end + if isempty(_debug_groups) + return false + end + if _debug_groups[1] == :all + return true + end + if isa(group, Symbol) && group in _debug_groups + return true + end + if nameof(_module) in _debug_groups + return true + end + if nameof(Base.moduleroot(_module)) in _debug_groups + return true + end + return false +end +end + """ global_logger() @@ -485,7 +529,7 @@ function handle_message(logger::SimpleLogger, level, message, _module, group, id for i in 2:length(msglines) println(iob, "│ ", msglines[i]) end - for (key,val) in pairs(kwargs) + for (key, val) in kwargs println(iob, "│ ", key, " = ", val) end println(iob, "└ @ ", _module, " ", filepath, ":", line)