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

**URL:** <https://discourse.julialang.org/t/what-is-the-best-way-to-trigger-debug-messages-in-the-logger/108754>\
**Category:** General Usage\
**Tags:** package\
**Created:** [January 13, 2024, 2:10am UTC](https://discourse.julialang.org/t/what-is-the-best-way-to-trigger-debug-messages-in-the-logger/108754 "2024-01-13T02:10:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![00krishna](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/00krishna/32/8843_2.png) [@00krishna](https://discourse.julialang.org/u/00krishna)\
**Post date:** [January 13, 2024, 2:10am UTC](https://discourse.julialang.org/t/what-is-the-best-way-to-trigger-debug-messages-in-the-logger/108754/1 "2024-01-13T02:10:13Z")

</div>

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.

Thanks.

---

<div class="post-metadata">

**Author:** ![goerz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerz/32/3269_2.png) [@goerz](https://discourse.julialang.org/u/goerz)\
**Post date:** [January 14, 2024, 4:16pm UTC](https://discourse.julialang.org/t/what-is-the-best-way-to-trigger-debug-messages-in-the-logger/108754/2 "2024-01-14T16:16:04Z")

</div>

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](https://docs.julialang.org/en/v1/stdlib/Logging/)

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:

> <https://github.com/JuliaDocs/DocumenterInterLinks.jl/blob/a6801a8c1e2a0a5ef6c3fea929bf5b2d8cfbe7fa/test/test_expand_extrefs.jl#L268-L276>

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.

---

<div class="post-metadata">

**Author:** ![00krishna](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/00krishna/32/8843_2.png) [@00krishna](https://discourse.julialang.org/u/00krishna)\
**Post date:** [January 15, 2024, 3:54pm UTC](https://discourse.julialang.org/t/what-is-the-best-way-to-trigger-debug-messages-in-the-logger/108754/3 "2024-01-15T15:54:44Z")

</div>

@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`"](https://julialogging.github.io/how-to/enable-debug/), hence I was confused. But yes, I can use the system as you suggest. Thanks for the example.
