# Setting Up A Global Logger

**URL:** <https://discourse.julialang.org/t/setting-up-a-global-logger/17691>\
**Category:** General Usage\
**Created:** [November 18, 2018, 8:03pm UTC](https://discourse.julialang.org/t/setting-up-a-global-logger/17691 "2018-11-18T20:03:40Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![zekeriya.sari](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zekeriya.sari/32/13695_2.png) [@zekeriya.sari](https://discourse.julialang.org/u/zekeriya.sari)\
**Post date:** [November 18, 2018, 8:03pm UTC](https://discourse.julialang.org/t/setting-up-a-global-logger/17691/1 "2018-11-18T20:03:40Z")

</div>

I want to setup a global logger and write the logging messages into a file. Here is what I did for this purpose.

```julia
module Example

using Logging

function setlogger()
    io = open("/tmp/log.txt", "w+")
    logger = SimpleLogger(io)
    global_logger(logger)
    return nothing
end

function closelogger()
    close(global_logger().stream)
end

function test()
    setlogger()
    @info "Test message"
    @info "Test message"
    closelogger()
end

export setlogger, test, closelogger

end

```

and here is the console output when I tested logging

```julia
julia> using Example

julia> test()
[ Info: Test message
[ Info: Test message

```

The problem is that the logging messages are written into the console, not in the file `/tmp/log.txt` which is empty.  
Any ideas what I am doing wrong?

---

<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:** [November 18, 2018, 8:48pm UTC](https://discourse.julialang.org/t/setting-up-a-global-logger/17691/2 "2018-11-18T20:48:13Z")

</div>

Your example works as expected for me:

```julia
julia> test()

shell> cat /tmp/log.txt
┌ Info: Test message
└ @ Main.Example REPL[1]:18
┌ Info: Test message
└ @ Main.Example REPL[1]:19

```

---

<div class="post-metadata">

**Author:** ![zekeriya.sari](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zekeriya.sari/32/13695_2.png) [@zekeriya.sari](https://discourse.julialang.org/u/zekeriya.sari)\
**Post date:** [November 18, 2018, 8:58pm UTC](https://discourse.julialang.org/t/setting-up-a-global-logger/17691/3 "2018-11-18T20:58:10Z")

</div>

> [@fredrikekre](#):
>
> Your example works as expected for me:

Yes, it worked when I launched julia from terminal, using `Example` and calling `test`. But I realized that it does not work when I launch julia repl in Juno. Seems interesting to me. May be a kind of bug in Juno I think.

---

<div class="post-metadata">

**Author:** ![pfitzseb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pfitzseb/32/45566_2.png) [@pfitzseb](https://discourse.julialang.org/u/pfitzseb)\
**Post date:** [November 19, 2018, 8:30am UTC](https://discourse.julialang.org/t/setting-up-a-global-logger/17691/4 "2018-11-19T08:30:53Z")

</div>

Yeah, this is a Juno specific problem – we’re wrapping everything the user executes in a

```julia
with_logger(JunoProgressLogger(current_logger())) do 
  eval(usercode)
end

```

where the `JunoProgressLogger` delegates all log messages to the former `current_logger`. This means that the new global logger you set in your user code will never be used.

To solve that you could either set the global logger in an extra statement (which is obviously not a great solution) or just use `with_logger` in your code as well, which imho should be the preferred over messing around with `global_logger` anyways.

---

<div class="post-metadata">

**Author:** ![zekeriya.sari](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zekeriya.sari/32/13695_2.png) [@zekeriya.sari](https://discourse.julialang.org/u/zekeriya.sari)\
**Post date:** [November 19, 2018, 4:47pm UTC](https://discourse.julialang.org/t/setting-up-a-global-logger/17691/5 "2018-11-19T16:47:53Z")

</div>

Thank you for the reply. Yeah, `with_logger` is what I should use, though it is not a global logger, to work in Juno.  
Here is the last case.

```julia
module Example

using Logging

function setlogger()
    io = open("/tmp/log.txt", "w+")
    SimpleLogger(io)
end

function test()
    logger = setlogger()
    with_logger(logger) do
        @info "Test message"
        @info "Test message"
    end
    flush(logger.stream)
end

export setlogger, test

end

```
