# Confused about logging

**URL:** <https://discourse.julialang.org/t/confused-about-logging/34945>\
**Category:** New to Julia\
**Created:** [February 20, 2020, 11:20pm UTC](https://discourse.julialang.org/t/confused-about-logging/34945 "2020-02-20T23:20:24Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![jonathan-durbin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jonathan-durbin/32/11468_2.png) [@jonathan-durbin](https://discourse.julialang.org/u/jonathan-durbin)\
**Post date:** [February 20, 2020, 11:20pm UTC](https://discourse.julialang.org/t/confused-about-logging/34945/1 "2020-02-20T23:20:24Z")

</div>

I’m trying to figure out how the Logging module works. I’ve browsed through the documentation and tried reading through [logging.jl](https://github.com/JuliaLang/julia/blob/cced577b79316ea38482d84b69e7be9666d14041/base/logging.jl), but not much of this makes sense to me, unfortunately.

I saw [this explanation](https://discourse.julialang.org/t/how-to-change-loglevel-with-logging-package/17501/2), which includes the following example (Below is my version of trying out the code at the above link)

```julia
julia> using Logging

julia> logger = SimpleLogger(stdout, Logging.Debug)
Base.CoreLogging.SimpleLogger(Base.TTY(RawFD(0x0000000d) open, 0 bytes waiting), Debug, Dict{Any,Int64}())

julia> @info "info"
[ Info: info

julia> @debug "debug"

julia> old_logger = global_logger(logger)
ConsoleLogger(Base.TTY(RawFD(0x0000000f) open, 0 bytes waiting), Info, Logging.default_metafmt, true, 0, Dict{Any,Int64}())

julia> @info "info"
┌ Info: info
└ @ Main REPL[7]:1

julia> @debug "debug"
┌ Debug: debug
└ @ Main REPL[8]:1

julia> 

```

It’s not clear to me what about the line `old_logger = global_logger(logger)` makes the debug message visible thereafter. A `SimpleLogger` was created at the `Logging.Debug` level, but the debug message wasn’t shown until the logger was passed into `global_logger`. The documentation doesn’t seem to cover strange details such as this.

I would imagine there would be a way to simply specify what logging level I prefer at (for example) the top of a file. Something like `set_loglevel!("Debug")`. I also don’t need to call `using Logging` for simple applications, right? It looks like the `@debug`, `@info`, etc. macros are available without any importing.

I hope my questions are clear. Do let me know if I need to clarify anything!

Edit: I just noticed that the format of the info and debug messages changed after `global_logger` was called. Why is that?

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [February 20, 2020, 11:56pm UTC](https://discourse.julialang.org/t/confused-about-logging/34945/2 "2020-02-20T23:56:31Z")

</div>

> [@jonathan-durbin](#):
>
> It’s not clear to me what about the line `old_logger = global_logger(logger)` makes the debug message visible thereafter.

The call `global_logger(logger)` set your new logger `logger` to be the global logger.

> [@jonathan-durbin](#):
>
> A `SimpleLogger` was created at the `Logging.Debug` level, but the debug message wasn’t shown until the logger was passed into `global_logger` . The documentation doesn’t seem to cover strange details such as this.

You were just creating a logger, but not telling Julia to use it.

> [@jonathan-durbin](#):
>
> I just noticed that the format of the info and debug messages changed after `global_logger` was called. Why is that?

The default logger is a `TerminalLogger` which has some different formatting compared to `SimpleLogger`.

---

<div class="post-metadata">

**Author:** ![jonathan-durbin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jonathan-durbin/32/11468_2.png) [@jonathan-durbin](https://discourse.julialang.org/u/jonathan-durbin)\
**Post date:** [February 21, 2020, 12:47am UTC](https://discourse.julialang.org/t/confused-about-logging/34945/3 "2020-02-21T00:47:20Z")

</div>

Thank you for the quick reply! I still have some unanswered questions.

How would a simple implementation of this look in a file? Say, for example, I have an independent julia file that I want to run with some debug and info messages. Would I put the following at the top of the file?

```julia
using Logging
logger = SimpleLogger(stdout, Logging.Debug)
global_logger(logger)

```

This feels too chunky to include at the start of a file any time I want to use both the `@debug` and `@info` macros (and others down the road, who knows).

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [February 21, 2020, 5:57am UTC](https://discourse.julialang.org/t/confused-about-logging/34945/4 "2020-02-21T05:57:14Z")

</div>

You can use `@info` and `@debug` and the other logging macros without changing the default logger provided you’ve done `using Logging`. You only have to set a different logger if you want that loggers’ features or pretty printing (as is the case with `SimpleLogger`). To execute a function with a specific logger without changing the global logger for concurrently executed code, you can use [`with_logger`](https://docs.julialang.org/en/v1/stdlib/Logging/#Logging.with_logger).

---

<div class="post-metadata">

**Author:** ![jonathan-durbin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jonathan-durbin/32/11468_2.png) [@jonathan-durbin](https://discourse.julialang.org/u/jonathan-durbin)\
**Post date:** [February 21, 2020, 8:12pm UTC](https://discourse.julialang.org/t/confused-about-logging/34945/5 "2020-02-21T20:12:24Z")

</div>

Ok, thanks for the help! I still don’t know how to set the logging level. I also looked in the command-line arguments for julia but didn’t see anything.

---

<div class="post-metadata">

**Author:** ![Alex\_Ellison](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alex_ellison/32/5749_2.png) [@Alex\_Ellison](https://discourse.julialang.org/u/Alex_Ellison)\
**Post date:** [February 25, 2020, 5:23pm UTC](https://discourse.julialang.org/t/confused-about-logging/34945/6 "2020-02-25T17:23:56Z")

</div>

Using those macros (`@info`, `@debug`) how do you affect the logging level? This documentation isn’t very helpful since it doesn’t explain, generally, how the environment parameter works: [Logging · The Julia Language](https://docs.julialang.org/en/v1/stdlib/Logging/#Environment-variables-1)  
Do I set `JULIA_DEBUG` to an integer as in some languages? The docs imply the value to which I set `JULIA_DEBUG` affects the scope in which it is applied - but something like `JULIA_DEBUG="log_test.jl"` doesn’t get `@debug` statements to come out of a file `log_test.jl`.

---

<div class="post-metadata">

**Author:** ![Skoffer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skoffer/32/378_2.png) [@Skoffer](https://discourse.julialang.org/u/Skoffer)\
**Post date:** [February 25, 2020, 5:42pm UTC](https://discourse.julialang.org/t/confused-about-logging/34945/7 "2020-02-25T17:42:45Z")

</div>

May be this would be helpful? [How to Change LogLevel with Logging Package? - #2 by Sukera](https://discourse.julialang.org/t/how-to-change-loglevel-with-logging-package/17501/2)

---

<div class="post-metadata">

**Author:** ![jonathan-durbin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jonathan-durbin/32/11468_2.png) [@jonathan-durbin](https://discourse.julialang.org/u/jonathan-durbin)\
**Post date:** [February 25, 2020, 6:05pm UTC](https://discourse.julialang.org/t/confused-about-logging/34945/8 "2020-02-25T18:05:38Z")

</div>

@Skoffer, that link is the same one I included at the beginning of this post. Appreciate the help though! One day I’ll get to the bottom of this 😄

@Alex_Ellison, I did notice this command-line option:

```julia
 -g, -g <level> Enable / Set the level of debug info generation (default level is 1 if unspecified or 2 if used without a level)

```

I wrote a simple script (called debug\_test.jl) that calls the four main logging macros line by line.

```julia
@debug "debug"
@info "info"
@warn "warn"
@error "error"

```

Then tested it without the `-g` option and with other values.

```julia
$ julia debug_test.jl 
[ Info: info
┌ Warning: warn
└ @ Main ~/code_stuff/julia_stuff/debug_test.jl:5
┌ Error: error
└ @ Main ~/code_stuff/julia_stuff/debug_test.jl:7
$ julia -g -- debug_test.jl 
[ Info: info
┌ Warning: warn
└ @ Main ~/code_stuff/julia_stuff/debug_test.jl:5
┌ Error: error
└ @ Main ~/code_stuff/julia_stuff/debug_test.jl:7
$ julia -g 3 -- debug_test.jl 
ERROR: julia: invalid argument to -g (3)
$ julia -g 0 -- debug_test.jl 
[ Info: info
┌ Warning: warn
└ @ Main ~/code_stuff/julia_stuff/debug_test.jl:5
┌ Error: error
└ @ Main ~/code_stuff/julia_stuff/debug_test.jl:7

```

What values does `-g` expect? From the [logging.jl source file](https://github.com/JuliaLang/julia/blob/master/base/logging.jl#L112), it looks like debug might be at level -1000, for some reason, but specifying this in the command line also doesn’t work. The mystery deepens!

---

<div class="post-metadata">

**Author:** ![Skoffer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skoffer/32/378_2.png) [@Skoffer](https://discourse.julialang.org/u/Skoffer)\
**Post date:** [February 25, 2020, 7:25pm UTC](https://discourse.julialang.org/t/confused-about-logging/34945/9 "2020-02-25T19:25:22Z")

</div>

Sorry about wrong link. Now, as for the logging, first of all it has been changed on `master` branch, but in julia 1.3.1, these lines are important: [julia/logging.jl at v1.3.1 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/blob/v1.3.1/base/logging.jl#L418)

It looks like environment control is rather rudimentary, the only thing you can do is to set `JULIA_DEBUG` to `all`

```julia
shell> JULIA_DEBUG=all julia debug_test.jl 
┌ Debug: debug
└ @ Main /tmp/debug_test.jl:1
[ Info: info
┌ Warning: warn
└ @ Main /tmp/debug_test.jl:3
┌ Error: error
└ @ Main /tmp/debug_test.jl:4

```

Key `-g` addresses debugger information, I presume and doesn’t affect logging at all.

In `master` branch of julia (v1.5 I guess) you can use more elaborated control for `JULIA_DEBUG` with include and exclude groups, so something like this should work:

In julia file you define

```julia
@debug "debug"
@info "info"
@warn "warn"
@error "error"
@warn _group=:g1 "another warning"

```

and in shell

```julia
shell> JULIA_DEBUG='all,!g1' julia debug_test.jl

```

it should produce all message including debug, but excluding last `@warn` which relates to `:g1` group. Unfortunatelt, I can’t verify it yet, cause I do not have v1.5 locally.

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [February 25, 2020, 7:45pm UTC](https://discourse.julialang.org/t/confused-about-logging/34945/10 "2020-02-25T19:45:24Z")

</div>

> [@Skoffer](#):
>
> Key `-g` addresses debugger information, I presume and doesn’t affect logging at all.

The `-g` command line flag is a compiler flag, not a logging flag:

```julia
base $ rg debug_level
options.jl
23: debug_level::Int8

util.jl
453: push!(addflags, "-g$(opts.debug_level)")

compiler/validation.jl
59: if JLOptions().debug_level == 2

compiler/ssair/driver.jl
129: if JLOptions().debug_level == 2

```

> [@Alex\_Ellison](#):
>
> Using those macros ( `@info` , `@debug` ) how do you affect the logging level?

The macros themselves create a logging event with the associated level. To create a logging message with a custom level, use `@logmsg level message` where `level` is the logging level and `message` is the message you want to appear with that level. See also [here](https://docs.julialang.org/en/v1/stdlib/Logging/#Creating-events-1).

> [@Alex\_Ellison](#):
>
> but something like `JULIA_DEBUG="log_test.jl"` doesn’t get `@debug` statements to come out of a file `log_test.jl` .

You’ve misunderstood the documentation - setting `JULIA_DEBUG=log_test` will only affect the default minimum log level for a module called `log_test`:

```julia
log $ cat test_log.jl
module test_log

using Logging

@debug "hello from debug"
@info "hello from info"

end
log $ cat no_log.jl
module no_log

using Logging

@debug "hello from debug"
@info "hello from info"

end
log $ JULIA_DEBUG=test_log julia test_log.jl
┌ Debug: hello from debug
└ @ Main.test_log /c/Users/valentin/Documents/Projects/juliaProjects/Discourse/log/test_log.jl:5
[ Info: hello from info
log $ JULIA_DEBUG=test_log julia no_log.jl
[ Info: hello from info

```

Though I agree that this is somewhat poorly documented and the documentation should be improved.

Note that the way LogLevels or Logging in general works is being considered for [some](https://github.com/JuliaLang/julia/issues/33418) or [more](https://github.com/JuliaLang/julia/issues?utf8=%E2%9C%93&q=is%3Aissue+is%3Aopen+label%3Alogging) rework, as that’s a leftover from the 0.x days.

---

<div class="post-metadata">

**Author:** ![Alex\_Ellison](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alex_ellison/32/5749_2.png) [@Alex\_Ellison](https://discourse.julialang.org/u/Alex_Ellison)\
**Post date:** [February 26, 2020, 8:30pm UTC](https://discourse.julialang.org/t/confused-about-logging/34945/11 "2020-02-26T20:30:52Z")

</div>

^ Solution.

```julia
$ JULIA_DEBUG=mymodule julia myfile.jl 
┌ Debug: thanks, Sukera
└ @ Main.mymodule ~/dwave_project/myfile.jl:3

```

with `myfile.jl` like

```julia
using Logging
module mymodule
@debug "thanks, Sukera"
end

```
