What is the best way to trigger debug messages in the logger?

I have been looking at the Logging and LoggingExtras packages to better understand how to use them. So I finally understand how to setup the logging messages and such.

One question that I was not clear on, was what is the best way to turn on logging for @debug messages. I can use the environment variable JULIA_DEBUG=all, but according to the docs, that is a quick and dirty way to do it. even for me, trying to export an environment variable and then remove that variable seems a bit risky?

So what is the correct way to do this. My use case is that while developing the package, I would like to run the package in debug mode or in standard mode interchangeably. I read a lot about the enhancements in the LoggingExtras package etc., but I was still not sure what the most standard or community approved method way to initiate debug messages.


Setting the JULIA_DEBUG environment variable is the standard way to trigger debug messages in normal usage as far as I’m aware. A few details:

  • You usually want to restrict it to some module or set of modules instead of all
  • You can set/unset JULIA_DEBUG in a script or in the REPL by setting ENV["JULIA_DEBUG"]
  • In code/the REPL, you can set ENV["JULIA_DEBUG"] to a module instead of a string (import Documenter, ENV["JULIA_DEBUG"] = Documenter).

All of these are documented in Logging · The Julia Language

In certain situations, you could also set up a custom logger with a specific log-level (possibly via LoggingExtras) . I routinely do this kind of thing for testing:

But any situation where I want to see @debug logs, I set the JULIA_DEBUG variable.

P.S.: I suppose with_logger(ConsoleLogger(stdout, Logging.Debug)) from the above example is a realistic alternative if you want to run a few lines of code in the REPL with @debug logging enabled.

@goerz Okay thanks for the info. I will continue to use the JULIA_DEBUG environment variable then. The docs from JuliaLogging had an exact line " Another “quick and dirty” way of enabling debug messages is to make use of the environment variable JULIA_DEBUG", hence I was confused. But yes, I can use the system as you suggest. Thanks for the example.