How can I enable/disable printing of @debug?
Apparently I need to set JULIA_DEBUG
, but I can't find what to set it to.
Update: I think I got it; it's set to the module name where debug messages are activated.
You can also use global_logger
Just create logger with the needed level and make it active with global_logger
command
The documentation on logging badly needs an example or two. I find it impossible to understand.
using Logging
# Create logger with Debug level printing to stderr
debuglogger = ConsoleLogger(stderr, Logging.Debug)
# Set this logger as the global logger
global_logger(debuglogger)
Thanks! I kind of see how it works, but:
1) Logging.Debug
is not in the docs as far as I can see
2) What does it mean for a logger to be "global"? If I run this in my module, will it enable debug messages in other modules?
3) How do I turn it off? Do I need to create a NullLogger
and set it as global?
4) Will debuglogger
ignore @info
messages?
with_logger(debuglogger) do
# do stuff
end
ConsoleLogger(stderr, Logging.Info)
(the current logger is also returned from global_logger
, so the code above could be oldlogger = global_logger(debuglogger)
and then global_logger(oldlogger)
to return to the previous state.Logging.Debug
will go through.I recommend having a look at LoggingExtras.jl, in particular the README which includes a lot of useful information.
But it can be better, probably.
I mean, it is written "There are several standard levels of type LogLevel; user-defined levels are also possible. Each is distinct in purpose:" and key words after that.
But maybe it would be better to write Logging.Debug
instead of just Debug
, since it is not exported anyway.
The standard log levels could have their own docstrings, which would help in search I bet.
If I can figure this stuff out, I'll certainly contribute to the docs. I'll also take a look at LoggingExtras.
More questions:
1) The default logger has level Logging.Info
, but only @debug
is ignored; @info
, @warn
and @error
all produce messages. How does this work?
2) I have a large module with @debug
s scattered around. There may even be @debug
s, @info
s and @warn
s in the same function/block of code. How do I turn @debug
on/off without affecting the others, using the Logging module functions? I can do this with JULIA_DEBUG
, so it must be possible, but I don't see how.
Andrey Oskin said:
I mean, it is written "There are several standard levels of type LogLevel; user-defined levels are also possible. Each is distinct in purpose:" and key words after that.
But maybe it would be better to write
Logging.Debug
instead of justDebug
, since it is not exported anyway.
Maybe it's just me, but I feel like the docs could be more explicit, or simply show a couple of examples to clarify. It was not obvious to me that Debug
is meant to be used as Logging.Debug
and that it corresponds to a number. The docs also imply that the level are ordered, but the order is not made explicit (although it makes sense to assume it is Info<Debug<Warn<Error
).
Oh! So Debug < Info
. That is the piece I was missing.
Thanks a lot, Frederik and Andrey :smile:
But it's
Debug < Info < Warn < Error
Just for the reference
https://github.com/JuliaLang/julia/blob/master/base/logging.jl#L135-L140
Yeah -- just figured it out :sunglasses:
https://github.com/JuliaLang/julia/pull/40979
https://github.com/JuliaLang/julia/pull/40980
Thanks @Fredrik Ekre , those additions are great.
Last updated: Nov 22 2024 at 04:41 UTC