# Writing Messages to a Log file

**URL:** <https://discourse.julialang.org/t/writing-messages-to-a-log-file/79266>\
**Category:** General Usage\
**Tags:** logging\
**Created:** [April 9, 2022, 4:35pm UTC](https://discourse.julialang.org/t/writing-messages-to-a-log-file/79266 "2022-04-09T16:35:30Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![TI36XPro](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ti36xpro/32/33658_2.png) [@TI36XPro](https://discourse.julialang.org/u/TI36XPro)\
**Post date:** [April 9, 2022, 4:35pm UTC](https://discourse.julialang.org/t/writing-messages-to-a-log-file/79266/1 "2022-04-09T16:35:30Z")

</div>

Hello,

I’d like to write a function that will print all logging statements to a file. I’ve tried the following based on the Logging [documentation](https://docs.julialang.org/en/v1/stdlib/Logging/#Logging) but nothing gets written to the file.

My naive first pass was to wrap my function call inside of a function that opens and closes the log file. The function `f` would be something like `my_function_call(args...)`. The body of `my_function_call` is sprinkled with `@debug`, `@warn`, etc.

```julia
function with_logfile(f)
    io = open("logfile.txt", "w+")
    logger = SimpleLogger(io, Logging.Debug)
    with_logger(logger) do
        f
    end
    flush(io)
    close(io)
end

```

I have also tried the following but the result is the same, `logfile.txt` is empty.

```julia
function with_logfile(f)
    io = open("logfile.txt", "w+")
    logger = SimpleLogger(io, Logging.Debug)
    global_logger(logger)
    f;
    close(io)
end

```

The only way it writes to the file is if I explicitly write the logging statements like this

```julia

function with_logfile(f)
    io = open("logfile.txt", "w+")
    logger = SimpleLogger(io, Logging.Debug)
    global_logger(logger)
    @debug "This gets written to a file"
    f # but nothing from here does
    close(io)
end

```

Note, I _can_ see the debug statements in the REPL when I run `with_logfile()`.

I would appreciate any hints. Sorry if this is something obvious. I have never used logging.

---

<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:** [April 9, 2022, 4:58pm UTC](https://discourse.julialang.org/t/writing-messages-to-a-log-file/79266/2 "2022-04-09T16:58:03Z")

</div>

You’re never calling `f` in your function (should be `f()`).

---

<div class="post-metadata">

**Author:** ![TI36XPro](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ti36xpro/32/33658_2.png) [@TI36XPro](https://discourse.julialang.org/u/TI36XPro)\
**Post date:** [April 10, 2022, 1:05pm UTC](https://discourse.julialang.org/t/writing-messages-to-a-log-file/79266/3 "2022-04-10T13:05:58Z")

</div>

Hm - I agree that seems like that should be the problem except when I did `f()` I then got an error about tuples not being callable. Somehow I think the result of `f` was getting produced sooner than I understood/expected? There must be something wrong with how I am using `f` in this context.

I did end up fixing this though. Not sure it is a good or long-term fix. But instead of having a separate function I added a keyword arg to `f` that creates a log file, something like as follows.

```julia
function f(args...;with_logfile=false)
    if with_logfile
        io = open("logfile.txt", "w+")
        logger = SimpleLogger(io, Logging.Debug)
        global_logger(logger)
    end
    # Other function calls here, to actual computations, hot inner loops, etc.
    if with_logfile
        close(io)
    end
    # Return result of f
    return a, b
end    

```
