# How to save Logging output to a log file?

**URL:** <https://discourse.julialang.org/t/how-to-save-logging-output-to-a-log-file/14004>\
**Category:** General Usage\
**Created:** [August 24, 2018, 4:34pm UTC](https://discourse.julialang.org/t/how-to-save-logging-output-to-a-log-file/14004 "2018-08-24T16:34:36Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Diego\_Javier\_Zea](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/diego_javier_zea/32/1858_2.png) [@Diego\_Javier\_Zea](https://discourse.julialang.org/u/Diego_Javier_Zea)\
**Post date:** [August 24, 2018, 4:34pm UTC](https://discourse.julialang.org/t/how-to-save-logging-output-to-a-log-file/14004/1 "2018-08-24T16:34:36Z")

</div>

Hi!

I would like to save the output of Julia 0.7 `Logging` macros `@warn`, `@error`, `@debug` and `@info` into a log file. How can I do that?

Thanks in advance,

---

<div class="post-metadata">

**Author:** ![ExpandingMan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/expandingman/32/866_2.png) [@ExpandingMan](https://discourse.julialang.org/u/ExpandingMan)\
**Post date:** [August 24, 2018, 5:41pm UTC](https://discourse.julialang.org/t/how-to-save-logging-output-to-a-log-file/14004/2 "2018-08-24T17:41:13Z")

</div>

```julia
using Logging

logger = SimpleLogger(open("log1.txt", "w+"))

with_logger(logger) do
    @info(" here is some context specific logging")
end

global_logger(logger)

@info("All logs will use the global logger by default.")

```

see [here](https://docs.julialang.org/en/stable/stdlib/Logging/).

The docs could definitely use some improvement in this area, by the way. Took me a little while the first time I figured out the above.

---

<div class="post-metadata">

**Author:** ![ExpandingMan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/expandingman/32/866_2.png) [@ExpandingMan](https://discourse.julialang.org/u/ExpandingMan)\
**Post date:** [August 24, 2018, 6:41pm UTC](https://discourse.julialang.org/t/how-to-save-logging-output-to-a-log-file/14004/3 "2018-08-24T18:41:43Z")

</div>

Also a highly desirable default that seems to be missing is something like `MultiLogger` which spits out logs in multiple places. Right now it looks like you’d have to implement your own, which is unfortunate since this is probably one of the most common use cases (at least to `stdout` and a file).

If anyone figures out a really simple way to do this, please let me know.

---

<div class="post-metadata">

**Author:** ![jmgnve](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jmgnve/32/1906_2.png) [@jmgnve](https://discourse.julialang.org/u/jmgnve)\
**Post date:** [August 24, 2018, 8:45pm UTC](https://discourse.julialang.org/t/how-to-save-logging-output-to-a-log-file/14004/4 "2018-08-24T20:45:39Z")

</div>

That stuff seems really useful, but doesn’t write anything to the file on Julia v0.7.0 on my Mac though. Am I the only one with that issue?

_Version 0.7.0 (2018-08-08 06:46 UTC) Official [http://julialang.org/](http://julialang.org/) release x86\_64-apple-darwin14.5.0_

---

<div class="post-metadata">

**Author:** ![Tero\_Frondelius](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tero_frondelius/32/7629_2.png) [@Tero\_Frondelius](https://discourse.julialang.org/u/Tero_Frondelius)\
**Post date:** [August 26, 2018, 8:00am UTC](https://discourse.julialang.org/t/how-to-save-logging-output-to-a-log-file/14004/5 "2018-08-26T08:00:44Z")

</div>

> [@ExpandingMan](#):
>
> The docs could definitely use some improvement in this area, by the way. Took me a little while the first time I figured out the above.

Please, do everyone a favour and try to improve the documentation. This is something most of us need at some point.

---

<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:** [August 26, 2018, 1:09pm UTC](https://discourse.julialang.org/t/how-to-save-logging-output-to-a-log-file/14004/6 "2018-08-26T13:09:05Z")

</div>

Just as a complement to @ExpandingMan’s example; you need to `flush` or `close` before anything is printed to file, example:

```julia
julia> using Logging

julia> io = open("log.txt", "w+");

julia> logger = SimpleLogger(io);

julia> with_logger(logger) do
           @info(" here is some context specific logging")
       end

shell> cat log.txt

julia> flush(io);

shell> cat log.txt
┌ Info: here is some context specific logging
└ @ Main REPL[7]:2

julia> global_logger(logger);

julia> @info("All logs will use the global logger by default.");

shell> cat log.txt
┌ Info: here is some context specific logging
└ @ Main REPL[7]:2

julia> close(io);

shell> cat log.txt
┌ Info: here is some context specific logging
└ @ Main REPL[7]:2
┌ Info: All logs will use the global logger by default.
└ @ Main REPL[12]:1

```

---

<div class="post-metadata">

**Author:** ![Diego\_Javier\_Zea](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/diego_javier_zea/32/1858_2.png) [@Diego\_Javier\_Zea](https://discourse.julialang.org/u/Diego_Javier_Zea)\
**Post date:** [August 27, 2018, 7:08pm UTC](https://discourse.julialang.org/t/how-to-save-logging-output-to-a-log-file/14004/7 "2018-08-27T19:08:31Z")

</div>

Thanks @ExpandingMan and @fredrikekre! That works perfectly. I tested it using Ubuntu and Julia 1.0

I was thinking that maybe the logging macros should call `flush` any time they are invoked. Is that sensible?  
It will be sad to lose log messages if the program ends abruptly before it reaches the `close`, or to write a big program inside an enormous `with_logger` clause.

---

<div class="post-metadata">

**Author:** ![ExpandingMan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/expandingman/32/866_2.png) [@ExpandingMan](https://discourse.julialang.org/u/ExpandingMan)\
**Post date:** [August 27, 2018, 8:27pm UTC](https://discourse.julialang.org/t/how-to-save-logging-output-to-a-log-file/14004/8 "2018-08-27T20:27:41Z")

</div>

> [@Diego\_Javier\_Zea](#):
>
> I was thinking that maybe the logging macros should call `flush` any time they are invoked. Is that sensible?

Not really. That might make sense for files, but not other IO types.

The real problem here is that there are a handful of default loggers that really should be in `Logging` that aren’t. For example, a `FileLogger` would make sense (to make sure `flush` is always called), multi-logger, perhaps a few others. It should be easy enough to define these on your own but they should definitely be included by default.

Since nobody who developed `Logging` has chimed in on this yet, perhaps it would be appropriate to open an issue in the `julia` repo asking for more default loggers.

---

<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:** [August 28, 2018, 7:04am UTC](https://discourse.julialang.org/t/how-to-save-logging-output-to-a-log-file/14004/9 "2018-08-28T07:04:56Z")

</div>

I’m currently in the process of developing a package providing (at first) a simple FileLogger and a general IOLogger, with a File/IO distinction on a LogLevel-basis. I’d be willing to provide the code to base once I’m done with a basic functioning core, but from what I’ve seen in the issues/files, the Logging interface seems to be [not 100% finished](https://github.com/JuliaLang/julia/blob/3ab56f19a81a1d658a02750b48abbd63950ef248/stdlib/Logging/src/Logging.jl#L5), so maybe it being a package for now is best.

I still have to write tests, some documentation and fix bugs shown by testing, so it’ll be a while until it’s done.

---

<div class="post-metadata">

**Author:** ![hustf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hustf/32/2374_2.png) [@hustf](https://discourse.julialang.org/u/hustf)\
**Post date:** [September 3, 2018, 8:31pm UTC](https://discourse.julialang.org/t/how-to-save-logging-output-to-a-log-file/14004/10 "2018-09-03T20:31:31Z")

</div>

@Sukera, do you have a repository for IOLogging.jl? Leaving flushing up to the user is key to speed.

---

<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:** [September 3, 2018, 8:46pm UTC](https://discourse.julialang.org/t/how-to-save-logging-output-to-a-log-file/14004/11 "2018-09-03T20:46:45Z")

</div>

Not publicy available yet, probably sometime next week.

Only the files created by the FileLogger are automatically flushed after writing to them, since you theoretically want to read from them as soon as possible. General IOLogging is not flushed, since those are not generally flushable. There’s always the option to just give file IO to an IOLogger and do the flushing manually - the FileLogger is just for convenience and closing guarantees.

---

<div class="post-metadata">

**Author:** ![hustf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hustf/32/2374_2.png) [@hustf](https://discourse.julialang.org/u/hustf)\
**Post date:** [September 4, 2018, 8:16pm UTC](https://discourse.julialang.org/t/how-to-save-logging-output-to-a-log-file/14004/12 "2018-09-04T20:16:37Z")

</div>

Thanks, Sukera. I’d open an issue on the repo if it was available now. I’m looking forward to probably implementing IOLogging in my own code. There’s a custom logger for monitoring of multiple sockets / processes in JuliaWeb/WebSockets.jl. It’s there for historical reasons and was never quite up to Base standard. The ‘origin’ of a logging line is inelegantly dealt with without using macros.

If you are looking at what may be desirable of a logging API for building networked applications, the version for Julia 0.6 may possibly be of help as a use case. It does deal with colorful output where possible, truncating, timestamps, ‘origin’ of the logging line in case of shared output, and readable header pairs, while avoiding type piracy on the show methods. There’s also a PR under work for 0.7.
