MicroLogging.jl
MicroLogging.jl copied to clipboard
Selectively disable logging for a module
Let's say I have the following codebase, consisting of three files, modulea.jl, moduleb.jl and main.jl
# modulea.jl
using Logging
module ModuleA
export hello
function hello()
@info "hello"
end
end
# moduleb.jl
using Logging
module ModuleB
export world
function world()
@info "world"
end
end
# main.jl
import ModuleA
import ModuleB
using Logging
# Insert code here that sets ModuleB logging to error only but sets ModuleA logging to info
#
#
ModuleA.hello()
ModuleB.world()
If I want to show only @error outputs from ModuleB but only @info and above from ModuleA, is there a way of doing that by only adding code in the commented block and not changing the remaining codebase?
There's a way to do this with the older InteractiveLogger which is provided by MicroLogging. It's marked as deprecated, but I haven't folded all the functionality into the (currently) julia-1.0 compatible ConsoleLogger yet. Here's what you'd do:
module ModuleA
function hello()
@info "hello"
end
end
module ModuleB
using ..ModuleA
function world()
ModuleA.hello()
@info "world"
end
end
Thence
julia> using MicroLogging
julia> global_logger(InteractiveLogger(stdout))
julia> ModuleB.world()
I- hello Info REPL[8]:3
I- world Info REPL[14]:5
julia> configure_logging(ModuleA, min_level=MicroLogging.Warn)
julia> ModuleB.world()
I- world Info REPL[14]:5
Unfortunately in answering this question I've realized InteractiveLogger is actually broken on 0.7 (it was prototype code and never tested properly, unlike the newer code in the package). This is now fixed in latest master.
Thanks for the reply. If I were not using MicroLogging and only using standard Logging would you recommend copying over the config.jl file? What is the recommended best practice here?
The recommended practice is to help me improve the mess which is logging configuration and filtering ;-)
I'm afriad there's just no predefined way to do sophisticated log filtering and configuration in julia-1.0 because I hadn't figured it out in time for 0.7 and didn't want to merge something half baked. Having said that, the existing system is very flexible. What you can do is:
- Make your own implementations of
AbstractLogger, which allow you to create completely arbitrary filtering rules by controlling theshouldlog()andhandle_message()functions. - Install your own loggers using
global_logger().
Right now, I'm hoping people will dive in and do this in packages. Either here in MicroLogging, or elsewhere in the ecosystem. When we've settled on a nice way of configuring filtering and other logging rules we can put that in stdlib/Logging.
Haha got it. I'll play around with it and if I find any patterns emerge from my use I'll report back here and we can discuss where to submit PRs.
Excellent!
I'd rather keep extensions out of stdlib/Logging until we've got a somewhat complete solution. Only because the stdlib is currently versioned in lockstep with Base, and I'd hate to release something half baked in julia-1.1
See also https://github.com/JuliaLang/julia/issues/29567#issuecomment-429681748
Maybe we should just make BaseLogNext or some such package and converge on that for future work? Could even rename MicroLogging to that — essentially this is what MicroLogging has been so far.
Maybe we should just make BaseLogNext or some such package
I've come to the conclusion that we should probably just split stdlib/Logging into JuliaLang/Logging to decouple the release cycle from julia itself, and develop further from there. I just need to do the work to generalize whatever is being done with Pkg.