How to save Logging output to a log file?

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

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
6 Likes