Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Selectively disable logging for a module #17

Open
kdheepak opened this issue Oct 25, 2018 · 7 comments
Open

Selectively disable logging for a module #17

kdheepak opened this issue Oct 25, 2018 · 7 comments

Comments

@kdheepak
Copy link

kdheepak commented Oct 25, 2018

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?

@c42f
Copy link
Owner

c42f commented Oct 31, 2018

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.

@kdheepak
Copy link
Author

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?

@c42f
Copy link
Owner

c42f commented Oct 31, 2018

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 the shouldlog() and handle_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.

@kdheepak
Copy link
Author

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.

@c42f
Copy link
Owner

c42f commented Oct 31, 2018

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 JuliaLang/julia#29567 (comment)

@c42f
Copy link
Owner

c42f commented Oct 31, 2018

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.

@c42f
Copy link
Owner

c42f commented Dec 19, 2018

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants