Any way to keep printing out the info/error in log using internal logging?

question

#1

In julia 0.6 Base, we have logging function to log. But I did not find any way to keep printing out the information while logging.

julia> logging(open("/tmp/testlog.txt","w"))                 
                                                             
julia> info("test info")                                     
                                                             
julia> error("test error")                                   

#2

Do you mean that while logging to a file you want to print the logs to your screen as well?

You could try a Tee struct:

import Base.print
struct Tee <: IO
    ios :: Vector{IO}
end
print(tee::Tee, x::AbstractString) = for io in tee.ios print(io, x) end
print(tee::Tee, xs...) = for io in tee.ios print(io, xs...) end

And use Tee:

f = open("a.txt", "w")
tee = Tee([f1, STDERR])
logging(tee)
info("info")
warn("warn")

But note that error is a special function that throws an error instead of just doing some logging, so this simple implementation works well with just info and warn.

Or you can use the unix tee command like this (and no need for logging(f)):

$ julia --color=yes testlog.jl 2> >(tee testlog.txt)