# Flushing output for Logging immediately without buffering

**URL:** <https://discourse.julialang.org/t/flushing-output-for-logging-immediately-without-buffering/26800>\
**Category:** General Usage\
**Created:** [July 25, 2019, 4:21pm UTC](https://discourse.julialang.org/t/flushing-output-for-logging-immediately-without-buffering/26800 "2019-07-25T16:21:21Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![scottstanie](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scottstanie/32/9805_2.png) [@scottstanie](https://discourse.julialang.org/u/scottstanie)\
**Post date:** [July 25, 2019, 4:21pm UTC](https://discourse.julialang.org/t/flushing-output-for-logging-immediately-without-buffering/26800/1 "2019-07-25T16:21:21Z")

</div>

I’ve got a problem similar to this question: [How to flush stdout](https://discourse.julialang.org/t/how-to-flush-stdout/10146) where I want to log output from a long running script, usually to stderr, and check on it with `tail -f `  
If I run it and redirect the output to a file with either `println` or `@info` from the Logging package, it will buffer all the output until the script finishes and write it all at the end.

I saw that this question [Issues with println buffering output when redirecting stdout to a txt file](https://discourse.julialang.org/t/issues-with-println-buffering-output-when-redirecting-stdout-to-a-txt-file/23738) had some ideas about making a FlushingIO type, which works for the `println` case.

Is there a similar way to extend Logging.info to always flush right away? Or alternatively, how can I make sure my logging output doesn’t wait until the script finishes?

You can test the buffering with this small script:

```julia
$ cat testprint.jl
using Logging

struct FlushingIO{T}
    io::T
end

function Base.println(io::FlushingIO, args...)
    println(io.io, args...)
    flush(io.io)
end

out = FlushingIO(stderr)

println(stderr, "a")
sleep(1)
println(out, "b")
sleep(1)
@info "c"
sleep(1)
@warn ("d")
sleep(1)

```

`julia testprint.jl &> test.log & tail -f test.log`

---

<div class="post-metadata">

**Author:** ![Benjia](https://avatars.discourse-cdn.com/v4/letter/b/9fc29f/32.png) [@Benjia](https://discourse.julialang.org/u/Benjia)\
**Post date:** [November 10, 2020, 5:13pm UTC](https://discourse.julialang.org/t/flushing-output-for-logging-immediately-without-buffering/26800/2 "2020-11-10T17:13:44Z")

</div>

Sorry to bump this, but I’ve got the same problem using Base logging (`@info` etc). It seems to buffer stdout for many lines before writing to the stream. I’d rather avoid having to put `flush(stdout)` after every log command. Any better ideas?

---

<div class="post-metadata">

**Author:** ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)\
**Post date:** [April 25, 2022, 8:52am UTC](https://discourse.julialang.org/t/flushing-output-for-logging-immediately-without-buffering/26800/3 "2022-04-25T08:52:10Z")

</div>

I have the same issue. Any suggestions?

---

<div class="post-metadata">

**Author:** ![sschmidhuber](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sschmidhuber/32/13349_2.png) [@sschmidhuber](https://discourse.julialang.org/u/sschmidhuber)\
**Post date:** [October 18, 2022, 8:01pm UTC](https://discourse.julialang.org/t/flushing-output-for-logging-immediately-without-buffering/26800/4 "2022-10-18T20:01:23Z")

</div>

I have the same problem. Is there a way to control the buffer or is it required to execute

```julia
flush(io)

```

after each log event? That doesn’t feel right 🤔

---

<div class="post-metadata">

**Author:** ![sschmidhuber](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sschmidhuber/32/13349_2.png) [@sschmidhuber](https://discourse.julialang.org/u/sschmidhuber)\
**Post date:** [October 26, 2022, 5:24pm UTC](https://discourse.julialang.org/t/flushing-output-for-logging-immediately-without-buffering/26800/5 "2022-10-26T17:24:26Z")

</div>

Just in case someone stumbles over this issue in future, the solution is the “FileLogger” and/or “FormatLogger” of the Package [GitHub - JuliaLogging/LoggingExtras.jl: Composable Loggers for the Julia Logging StdLib](https://github.com/JuliaLogging/LoggingExtras.jl)

Those loggers flush the buffer after each log message to the io stream.
