Logging for later analysis

SimpleLogger takes an argument of type IO.

julia> using Logging

julia> iobuf = IOBuffer()
IOBuffer(data=UInt8[...], readable=true, writable=true, seekable=true, append=false, size=0, maxsize=Inf, ptr=1, mark=-1)

julia> logger = SimpleLogger(iobuf)
Base.CoreLogging.SimpleLogger(IOBuffer(data=UInt8[...], readable=true, writable=true, seekable=true, append=false, size=0, maxsize=Inf, ptr=1, mark=-1), Info, Dict{Any, Int64}())

julia> with_logger(logger) do
           logtest(100)
       end

julia> println(String(take!(iobuf)))
┌ Info: divisible by 7
│   times7[i] = 1.0
└ @ Main REPL[44]:4
┌ Info: divisible by 7
│   times7[i] = 4.0
└ @ Main REPL[44]:4
┌ Info: divisible by 7
│   times7[i] = 9.0
└ @ Main REPL[44]:4
┌ Info: divisible by 7
│   times7[i] = 16.0
└ @ Main REPL[44]:4
┌ Info: divisible by 7
│   times7[i] = 25.0
└ @ Main REPL[44]:4
┌ Info: divisible by 7
│   times7[i] = 36.0
└ @ Main REPL[44]:4
┌ Info: divisible by 7
│   times7[i] = 49.0
└ @ Main REPL[44]:4
┌ Info: divisible by 7
│   times7[i] = 64.0
└ @ Main REPL[44]:4
┌ Info: divisible by 7
│   times7[i] = 81.0
└ @ Main REPL[44]:4
┌ Info: divisible by 7
│   times7[i] = 100.0
└ @ Main REPL[44]:4
┌ Info: divisible by 7
│   times7[i] = 121.0
└ @ Main REPL[44]:4
┌ Info: divisible by 7
│   times7[i] = 144.0
└ @ Main REPL[44]:4
┌ Info: divisible by 7
│   times7[i] = 169.0
└ @ Main REPL[44]:4
┌ Info: divisible by 7
│   times7[i] = 196.0
└ @ Main REPL[44]:4

You could also just make a custom logger:
https://docs.julialang.org/en/v1/stdlib/Logging/#AbstractLogger-interface

1 Like