Stream: helpdesk (published)

Topic: Enabling @debug


view this post on Zulip mbaz (May 26 2021 at 22:37):

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.

view this post on Zulip Kwaku Oskin (May 27 2021 at 05:30):

You can also use global_logger

view this post on Zulip Kwaku Oskin (May 27 2021 at 05:31):

Just create logger with the needed level and make it active with global_logger command

view this post on Zulip mbaz (May 27 2021 at 13:14):

The documentation on logging badly needs an example or two. I find it impossible to understand.

view this post on Zulip Fredrik Ekre (May 27 2021 at 13:16):

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)

view this post on Zulip mbaz (May 27 2021 at 13:23):

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?

view this post on Zulip Fredrik Ekre (May 27 2021 at 13:28):

  1. Please contribute!
  2. Global to that Julia session. This is inherited by all tasks. To use a logger locally you can use e.g.
with_logger(debuglogger) do
      # do stuff
end
  1. The default logger is 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.
  2. No, the level you give is the minimal level, so anything above Logging.Debug will go through.

view this post on Zulip Fredrik Ekre (May 27 2021 at 13:29):

I recommend having a look at LoggingExtras.jl, in particular the README which includes a lot of useful information.

view this post on Zulip Kwaku Oskin (May 27 2021 at 13:30):

  1. https://docs.julialang.org/en/v1/stdlib/Logging/#Log-event-structure

But it can be better, probably.

view this post on Zulip Kwaku Oskin (May 27 2021 at 13:31):

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.

view this post on Zulip Fredrik Ekre (May 27 2021 at 13:32):

The standard log levels could have their own docstrings, which would help in search I bet.

view this post on Zulip mbaz (May 27 2021 at 13:38):

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 @debugs scattered around. There may even be @debugs, @infos and @warns 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.

view this post on Zulip Fredrik Ekre (May 27 2021 at 13:44):

  1. Because the level is the minum: info, warn, and error levels are all larger or equal to that level so the messages are displayed

view this post on Zulip Fredrik Ekre (May 27 2021 at 13:45):

  1. You can not really do that with the Logging stdlib only so you need LoggingExtras.jl. There is an example in the README for filtering out everything from HTTP.jl (https://github.com/oxinabox/LoggingExtras.jl#filterout-any-messages-from-http)

view this post on Zulip mbaz (May 27 2021 at 13:46):

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 just Debug, 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).

view this post on Zulip mbaz (May 27 2021 at 13:47):

Oh! So Debug < Info. That is the piece I was missing.

view this post on Zulip mbaz (May 27 2021 at 13:50):

Thanks a lot, Frederik and Andrey :smile:

view this post on Zulip Kwaku Oskin (May 27 2021 at 14:06):

But it's
Debug < Info < Warn < Error

view this post on Zulip Kwaku Oskin (May 27 2021 at 14:14):

Just for the reference

https://github.com/JuliaLang/julia/blob/master/base/logging.jl#L135-L140

view this post on Zulip mbaz (May 27 2021 at 14:14):

Yeah -- just figured it out :sunglasses:

view this post on Zulip Fredrik Ekre (May 28 2021 at 08:50):

https://github.com/JuliaLang/julia/pull/40979

view this post on Zulip Fredrik Ekre (May 28 2021 at 09:00):

https://github.com/JuliaLang/julia/pull/40980

view this post on Zulip mbaz (May 28 2021 at 13:09):

Thanks @Fredrik Ekre , those additions are great.


Last updated: Dec 28 2024 at 04:38 UTC