invenia / Memento.jl

A flexible logging library for Julia
https://invenia.github.io/Memento.jl/latest
Other
87 stars 14 forks source link

Julia warning triggers a MethodError when Memento config is set to substitute=true #181

Closed arnaudh closed 2 years ago

arnaudh commented 2 years ago

The following script:

a = 1
for i in [0]
    a = 2
end

produces the Julia warning

┌ Warning: Assignment to `a` in soft scope is ambiguous because a global variable by the same name exists: `a` will be treated as a new local. Disambiguate by using `local a` to suppress this warning or `global a` to assign to the existing global variable.
└ @ ~/src/logger_error_MWE.jl:3

However when setting Memento config to substitute global logging:

using Memento
Memento.config!("info"; substitute=true)
a = 1
for i in [0]
    a = 2
end

we then get the following error:

ERROR: LoadError: MethodError: no method matching Logger(::Nothing, ::Dict{Any, Handler}, ::String, ::Dict{AbstractString, Int64}, ::Type{DefaultRecord}, ::Bool)
Closest candidates are:
  Logger(::AbstractString, ::Dict, ::AbstractString, ::Dict, ::Type, ::Bool) at /Users/arnaudhenry/.julia/packages/Memento/IanT6/src/loggers.jl:29
  Logger(::Any; level, levels, record, propagate) at /Users/arnaudhenry/.julia/packages/Memento/IanT6/src/loggers.jl:51
Stacktrace:
 [1] Logger(name::Nothing; level::String, levels::Dict{AbstractString, Int64}, record::Type{DefaultRecord}, propagate::Bool)
   @ Memento ~/.julia/packages/Memento/IanT6/src/loggers.jl:53
 [2] Logger(name::Nothing)
   @ Memento ~/.julia/packages/Memento/IanT6/src/loggers.jl:53
 [3] getlogger(name::Nothing)
   @ Memento ~/.julia/packages/Memento/IanT6/src/loggers.jl:238
 [4] handle_message(::Memento.BaseLogger, cl_level::Base.CoreLogging.LogLevel, msg::String, mod::Nothing, group::Symbol, id::Symbol, filepath::String, line::Int64; kwargs::Base.Iterators.Pairs{Union{}, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
   @ Memento ~/.julia/packages/Memento/IanT6/src/stdlib.jl:29
 [5] handle_message(::Memento.BaseLogger, cl_level::Base.CoreLogging.LogLevel, msg::String, mod::Nothing, group::Symbol, id::Symbol, filepath::String, line::Int64)
   @ Memento ~/.julia/packages/Memento/IanT6/src/stdlib.jl:29
 [6] logmsg_shim(level::Any, message::Any, _module::Any, group::Any, id::Any, file::Any, line::Any, kwargs::Any)
   @ Base.CoreLogging ./logging.jl:345
in expression starting at /Users/arnaudhenry/src/dynamic-weights-squad/DynamicWeightsLab.jl/analysis/arnaud/logger_error_MWE.jl:4
arnaudh commented 2 years ago

Seems like the issue is that Memento does not accept logger name of Nothing, and that's what is passed in this case.

One fix might be to have a eg default "__nothing__" logger name be used in this case.