# How to log output to file

**URL:** <https://discourse.julialang.org/t/how-to-log-output-to-file/5219>\
**Category:** General Usage\
**Tags:** question\
**Created:** [August 4, 2017, 8:30am UTC](https://discourse.julialang.org/t/how-to-log-output-to-file/5219 "2017-08-04T08:30:09Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![innerlee](https://avatars.discourse-cdn.com/v4/letter/i/b19c9b/32.png) [@innerlee](https://discourse.julialang.org/u/innerlee)\
**Post date:** [August 4, 2017, 8:30am UTC](https://discourse.julialang.org/t/how-to-log-output-to-file/5219/1 "2017-08-04T08:30:10Z")

</div>

This question has troubled me for some time. Basically, I want log outputs into a file. It should:

- Not for logging outputs of `info()`, `warn()`, etc, but for all printed outputs.
- They should be printed on the screen as well.
- Do not use external tools like `tee`.

Anyone met same problem?

---

<div class="post-metadata">

**Author:** ![cormullion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cormullion/32/49131_2.png) [@cormullion](https://discourse.julialang.org/u/cormullion)\
**Post date:** [August 4, 2017, 10:23am UTC](https://discourse.julialang.org/t/how-to-log-output-to-file/5219/2 "2017-08-04T10:23:53Z")

</div>

Julia’s got lots of logging solutions: `Base.logging`, `Memento.jl`, `Logging.jl`, etc. Have a go and see what’s missing…

---

<div class="post-metadata">

**Author:** ![innerlee](https://avatars.discourse-cdn.com/v4/letter/i/b19c9b/32.png) [@innerlee](https://discourse.julialang.org/u/innerlee)\
**Post date:** [August 6, 2017, 12:11pm UTC](https://discourse.julialang.org/t/how-to-log-output-to-file/5219/3 "2017-08-06T12:11:53Z")

</div>

These logging solutions do not satisfy the first constraint. i.e., logging all outputs rather than `info`, `warn`, `error`, etc.

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [August 6, 2017, 12:18pm UTC](https://discourse.julialang.org/t/how-to-log-output-to-file/5219/4 "2017-08-06T12:18:38Z")

</div>

See also this Julep:

[https://github.com/JuliaLang/Juleps/pull/30](https://github.com/JuliaLang/Juleps/pull/30)

---

<div class="post-metadata">

**Author:** ![innerlee](https://avatars.discourse-cdn.com/v4/letter/i/b19c9b/32.png) [@innerlee](https://discourse.julialang.org/u/innerlee)\
**Post date:** [August 6, 2017, 4:02pm UTC](https://discourse.julialang.org/t/how-to-log-output-to-file/5219/5 "2017-08-06T16:02:35Z")

</div>

basically the need is to log all outputs from 'println’s. Existing loggers seem to aim at dealing with specific messages annotated with info, warn, etc. So they are not ideal.  
The redirect io in Base is not helpful because you either print into file, or show in console. But not both.

---

<div class="post-metadata">

**Author:** ![innerlee](https://avatars.discourse-cdn.com/v4/letter/i/b19c9b/32.png) [@innerlee](https://discourse.julialang.org/u/innerlee)\
**Post date:** [August 8, 2017, 1:31pm UTC](https://discourse.julialang.org/t/how-to-log-output-to-file/5219/6 "2017-08-08T13:31:21Z")

</div>

I guess I found the solution:

```julia
import Base.println, Suppressor.@suppress
@suppress println(xs...) = 
    open(f -> (println(f, xs...); println(STDOUT, xs...)), "bf.log", "a+")

```

---

<div class="post-metadata">

**Author:** ![innerlee](https://avatars.discourse-cdn.com/v4/letter/i/b19c9b/32.png) [@innerlee](https://discourse.julialang.org/u/innerlee)\
**Post date:** [August 8, 2017, 2:44pm UTC](https://discourse.julialang.org/t/how-to-log-output-to-file/5219/7 "2017-08-08T14:44:24Z")

</div>

I write this function into a package [PrintLog](https://github.com/innerlee/PrintLog.jl).

Here is the usage from README:

```julia
julia> using PrintLog

julia> @printlog "bf.log"
INFO: `print` and `println` will be logged into file `bf.log`

julia> println("fanfan")
fanfan

julia> @noprintlog
INFO: `print` and `println` are resumed.

```

Then a log file `bf.log` will be created, and contents appended.  
To suppress the `INFO: ...`, add a `silent` keyword in the macro

```julia
julia> using PrintLog

julia> @printlog "bf.log" silent

julia> println("fan")
fan

julia> @noprintlog silent

```

---

<div class="post-metadata">

**Author:** ![jacob-roth](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jacob-roth/32/1862_2.png) [@jacob-roth](https://discourse.julialang.org/u/jacob-roth)\
**Post date:** [July 26, 2019, 8:19pm UTC](https://discourse.julialang.org/t/how-to-log-output-to-file/5219/8 "2019-07-26T20:19:18Z")

</div>

@innerlee: A crude update for v1.1 [here](https://github.com/jacob-roth/PrintLog.jl)
